Skip to content

Instantly share code, notes, and snippets.

@HopHouse
Last active July 9, 2019 19:56
Show Gist options
  • Save HopHouse/9142a526f05e587859046495e7701bf0 to your computer and use it in GitHub Desktop.
Save HopHouse/9142a526f05e587859046495e7701bf0 to your computer and use it in GitHub Desktop.

Revisions

  1. HopHouse revised this gist Jul 9, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions telegram_notif.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    #!/bin/bash

    # Edit /etc/pam.d/sshd and put the following line:
    # session optional pam_exec.so /root/telegram.sh

    USERID="<USER ID>"
    KEY="<KEY>"
    TIMEOUT="10"
  2. HopHouse revised this gist Jul 9, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions telegram_notif.sh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ KEY="<KEY>"
    TIMEOUT="10"
    URL="https://api.telegram.org/bot$KEY/sendMessage"
    DATE_EXEC="$(date "+%d %b %Y %H:%M")" #Collect date & time.
    TMPFILE='/tmp/ipinfo-$DATE_EXEC.txt' #Create a temporary file to keep data in.
    TMPFILE='$(mktemp)' #Create a temporary file to keep data in.

    if [[ "$PAM_TYPE" = "open_session" ]]; then
    IP=$(echo $SSH_CLIENT | awk '{print $1}') #Get Client IP address.
    @@ -20,4 +20,4 @@ if [[ "$PAM_TYPE" = "open_session" ]]; then
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - $ORG - $CITY, $REGION, $COUNTRY port $PORT"
    curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&te xt=$TEXT" $URL > /dev/null
    rm $TMPFILE #clean up after
    fi
    fi
  3. HopHouse revised this gist Jul 9, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion telegram_notif.sh
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,6 @@ if [[ "$PAM_TYPE" = "open_session" ]]; then
    COUNTRY=$(cat $TMPFILE | sed -n 's/^ "country":[[:space:]]*//p' | sed 's/"// g')
    ORG=$(cat $TMPFILE | sed -n 's/^ "org":[[:space:]]*//p' | sed 's/"//g')
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - $ORG - $CITY, $REGION, $COUNTRY port $PORT"
    curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&te xt=$TEXT" $URL > /tmp/log.txt
    curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&te xt=$TEXT" $URL > /dev/null
    rm $TMPFILE #clean up after
    fi
  4. @qro1337 qro1337 created this gist Jul 9, 2019.
    23 changes: 23 additions & 0 deletions telegram_notif.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    #!/bin/bash

    USERID="<USER ID>"
    KEY="<KEY>"
    TIMEOUT="10"
    URL="https://api.telegram.org/bot$KEY/sendMessage"
    DATE_EXEC="$(date "+%d %b %Y %H:%M")" #Collect date & time.
    TMPFILE='/tmp/ipinfo-$DATE_EXEC.txt' #Create a temporary file to keep data in.

    if [[ "$PAM_TYPE" = "open_session" ]]; then
    IP=$(echo $SSH_CLIENT | awk '{print $1}') #Get Client IP address.
    PORT=$(echo $SSH_CLIENT | awk '{print $3}') #Get SSH port
    HOSTNAME=$(hostname -f) #Get hostname
    IPADDR=$(hostname -I | awk '{print $1}')
    curl https://ipinfo.io/$IP -s -o $TMPFILE #Get info on client IP.
    CITY=$(cat $TMPFILE | sed -n 's/^ "city":[[:space:]]*//p' | sed 's/"//g') #C lient IP info parsing
    REGION=$(cat $TMPFILE | sed -n 's/^ "region":[[:space:]]*//p' | sed 's/"//g' )
    COUNTRY=$(cat $TMPFILE | sed -n 's/^ "country":[[:space:]]*//p' | sed 's/"// g')
    ORG=$(cat $TMPFILE | sed -n 's/^ "org":[[:space:]]*//p' | sed 's/"//g')
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - $ORG - $CITY, $REGION, $COUNTRY port $PORT"
    curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&te xt=$TEXT" $URL > /tmp/log.txt
    rm $TMPFILE #clean up after
    fi