Skip to content

Instantly share code, notes, and snippets.

@rahmatov
Forked from matriphe/ssh-telegram.sh
Created January 1, 2018 11:18
Show Gist options
  • Save rahmatov/f682f3d4ea0ca511a0c87531a6022b8e to your computer and use it in GitHub Desktop.
Save rahmatov/f682f3d4ea0ca511a0c87531a6022b8e to your computer and use it in GitHub Desktop.

Revisions

  1. @matriphe matriphe revised this gist Jul 28, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,8 @@ URL="https://api.telegram.org/bot$KEY/sendMessage"
    DATE_EXEC="$(date "+%d %b %Y %H:%M")"
    TMPFILE='/tmp/ipinfo-$DATE_EXEC.txt'
    if [ -n "$SSH_CLIENT" ]; then
    IP=$(echo $SSH_CLIENT|awk '{print $1}')
    PORT=$(echo $SSH_CLIENT|awk '{print $3}')
    IP=$(echo $SSH_CLIENT | awk '{print $1}')
    PORT=$(echo $SSH_CLIENT | awk '{print $3}')
    HOSTNAME=$(hostname -f)
    IPADDR=$(hostname -I | awk '{print $1}')
    curl http://ipinfo.io/$IP -s -o $TMPFILE
  2. @matriphe matriphe revised this gist Jul 28, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,8 @@ TMPFILE='/tmp/ipinfo-$DATE_EXEC.txt'
    if [ -n "$SSH_CLIENT" ]; then
    IP=$(echo $SSH_CLIENT|awk '{print $1}')
    PORT=$(echo $SSH_CLIENT|awk '{print $3}')
    HOSTNAME=$(hostname -I | awk -F ' ' '{ print $1 }')
    IPADDR=$(hostname -i)
    HOSTNAME=$(hostname -f)
    IPADDR=$(hostname -I | awk '{print $1}')
    curl http://ipinfo.io/$IP -s -o $TMPFILE
    CITY=$(cat $TMPFILE | jq '.city' | sed 's/"//g')
    REGION=$(cat $TMPFILE | jq '.region' | sed 's/"//g')
  3. @matriphe matriphe revised this gist Jul 28, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ TMPFILE='/tmp/ipinfo-$DATE_EXEC.txt'
    if [ -n "$SSH_CLIENT" ]; then
    IP=$(echo $SSH_CLIENT|awk '{print $1}')
    PORT=$(echo $SSH_CLIENT|awk '{print $3}')
    HOSTNAME=$(hostname -f)
    HOSTNAME=$(hostname -I | awk -F ' ' '{ print $1 }')
    IPADDR=$(hostname -i)
    curl http://ipinfo.io/$IP -s -o $TMPFILE
    CITY=$(cat $TMPFILE | jq '.city' | sed 's/"//g')
  4. @matriphe matriphe revised this gist Jul 28, 2015. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,11 @@
    # get jq from here http://stedolan.github.io/jq/
    USERID="<target_user_id>"
    KEY="<bot_private_key>"
    TIMEOUT="10"
    URL="https://api.telegram.org/bot$KEY/sendMessage"
    TMPFILE='/tmp/ipinfo.txt'
    DATE_EXEC="$(date "+%d %b %Y %H:%M")"
    TMPFILE='/tmp/ipinfo-$DATE_EXEC.txt'
    if [ -n "$SSH_CLIENT" ]; then
    DATE_EXEC="$(date "+%d %b %Y %H:%M")"
    IP=$(echo $SSH_CLIENT|awk '{print $1}')
    PORT=$(echo $SSH_CLIENT|awk '{print $3}')
    HOSTNAME=$(hostname -f)
    @@ -17,6 +18,6 @@ if [ -n "$SSH_CLIENT" ]; then
    COUNTRY=$(cat $TMPFILE | jq '.country' | sed 's/"//g')
    ORG=$(cat $TMPFILE | jq '.org' | sed 's/"//g')
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - $ORG - $CITY, $REGION, $COUNTRY on port $PORT"
    curl --connect-timeout=30 -d -s "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    rm $TMPFILE
    fi
  5. @matriphe matriphe revised this gist Jul 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,6 @@ if [ -n "$SSH_CLIENT" ]; then
    COUNTRY=$(cat $TMPFILE | jq '.country' | sed 's/"//g')
    ORG=$(cat $TMPFILE | jq '.org' | sed 's/"//g')
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - $ORG - $CITY, $REGION, $COUNTRY on port $PORT"
    curl -s -d --connect-timeout=30 "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    curl --connect-timeout=30 -d -s "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    rm $TMPFILE
    fi
  6. @matriphe matriphe revised this gist Jul 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,6 @@ if [ -n "$SSH_CLIENT" ]; then
    COUNTRY=$(cat $TMPFILE | jq '.country' | sed 's/"//g')
    ORG=$(cat $TMPFILE | jq '.org' | sed 's/"//g')
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - $ORG - $CITY, $REGION, $COUNTRY on port $PORT"
    curl -s -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    curl -s -d --connect-timeout=30 "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    rm $TMPFILE
    fi
  7. @matriphe matriphe revised this gist Jul 2, 2015. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    # save it on /etc/profile/profile.d/
    # save it as /etc/profile.d/ssh-telegram.sh
    # use jq to parse JSON from ipinfo.io
    # get jq from here http://xmodulo.com/how-to-parse-json-string-via-command-line-on-linux.html
    # http://stedolan.github.io/jq/
    # get jq from here http://stedolan.github.io/jq/
    USERID="<target_user_id>"
    KEY="<bot_private_key>"
    URL="https://api.telegram.org/bot$KEY/sendMessage"
  8. @matriphe matriphe revised this gist Jul 2, 2015. 1 changed file with 19 additions and 9 deletions.
    28 changes: 19 additions & 9 deletions ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,23 @@
    # save it on /etc/profile/profile.d/
    USERID="<user_or_group_id>"
    KEY="<telegram_bot_api_key>"
    # use jq to parse JSON from ipinfo.io
    # get jq from here http://xmodulo.com/how-to-parse-json-string-via-command-line-on-linux.html
    # http://stedolan.github.io/jq/
    USERID="<target_user_id>"
    KEY="<bot_private_key>"
    URL="https://api.telegram.org/bot$KEY/sendMessage"
    TMPFILE='/tmp/ipinfo.txt'
    if [ -n "$SSH_CLIENT" ]; then
    DATE_EXEC="$(date "+%d %b %Y %H:%M")"
    IP=$(echo $SSH_CLIENT|awk '{print $1}')
    PORT=$(echo $SSH_CLIENT|awk '{print $3}')
    HOSTNAME=$(hostname -f)
    IPADDR=$(hostname -i)
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP on port $PORT"
    curl -s -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    DATE_EXEC="$(date "+%d %b %Y %H:%M")"
    IP=$(echo $SSH_CLIENT|awk '{print $1}')
    PORT=$(echo $SSH_CLIENT|awk '{print $3}')
    HOSTNAME=$(hostname -f)
    IPADDR=$(hostname -i)
    curl http://ipinfo.io/$IP -s -o $TMPFILE
    CITY=$(cat $TMPFILE | jq '.city' | sed 's/"//g')
    REGION=$(cat $TMPFILE | jq '.region' | sed 's/"//g')
    COUNTRY=$(cat $TMPFILE | jq '.country' | sed 's/"//g')
    ORG=$(cat $TMPFILE | jq '.org' | sed 's/"//g')
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - $ORG - $CITY, $REGION, $COUNTRY on port $PORT"
    curl -s -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    rm $TMPFILE
    fi
  9. @matriphe matriphe revised this gist Jun 27, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@ if [ -n "$SSH_CLIENT" ]; then
    IP=$(echo $SSH_CLIENT|awk '{print $1}')
    PORT=$(echo $SSH_CLIENT|awk '{print $3}')
    HOSTNAME=$(hostname -f)
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME from $IP on port $PORT"
    IPADDR=$(hostname -i)
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP on port $PORT"
    curl -s -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    fi
  10. @matriphe matriphe revised this gist Jun 27, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # save it on /etc/profile/profile.d/
    USERID="<user_or_group_id>"
    KEY="<telegram_bot_api_key>"
    URL="https://api.telegram.org/bot$KEY/sendMessage"
  11. @matriphe matriphe created this gist Jun 27, 2015.
    11 changes: 11 additions & 0 deletions ssh-telegram.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    USERID="<user_or_group_id>"
    KEY="<telegram_bot_api_key>"
    URL="https://api.telegram.org/bot$KEY/sendMessage"
    if [ -n "$SSH_CLIENT" ]; then
    DATE_EXEC="$(date "+%d %b %Y %H:%M")"
    IP=$(echo $SSH_CLIENT|awk '{print $1}')
    PORT=$(echo $SSH_CLIENT|awk '{print $3}')
    HOSTNAME=$(hostname -f)
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME from $IP on port $PORT"
    curl -s -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    fi