6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SSHでログインされたらSlackに投げる

Posted at

準備

ゼロからSlack POST API」のWebhook登録までやります。(雑ですいません)

sshrcを編集

/etc/ssh/sshrcを以下のように編集します。
APIのURLは適宜変えてください

SOURCE_IP=${SSH_CLIENT%% *}
TRUST_IP_LIST="127.0.0.1 0.0.0.0"

for HOST in $TRUST_IP_LIST; do
  if [ $HOST = $SOURCE_IP ]; then
    exit 0
  fi
done

TEXT="[SSH LoginAlert] \\\"$USER\\\" has logged in from $SOURCE_IP at `date +"%Y/%m/%d %H:%M:%S"`."
curl -X POST -H "Content-type: application/json" --data '{"text":"'"$TEXT"'"}' \
  "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX" \
  -kLso /dev/null

除外リスト

自宅のIPなどを信用し通知を出さない場合、上記のTRUST_IP_LIST="127.0.0.1 0.0.0.0"の末尾に空白区切りでIPを追加します。
(自宅のグローバルIPはここから確認できます。)

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?