• Hey! :) I have a question, after creating the bot I received a token from the BotFather, something like this: 123456789:ASFSdfsdf43sfaSERAFASD-PBFF4f54c (kind of :P) The KEY="<The_key_of_your_telegram_bot>" value is all this string or I'm missibg something? Thanks!

  • You might want to use jq for easier JSON parsing.

    Also, using a single, fixed filename to store the ipinfo.io response is risky (it's fixed as it's using the current date which won't change for a whole minute): say there is a first SSH login (login1) but the curl to ipinfo.io suffers some network latency, and that during the wait for it to complete, a second SSH login (login2) occurs. This would cause a to mix of the two information:

    • First telegram notification (notif1) would be sent using IP $IP of login1 but ipinfo.io response ($CITY, ...) of login2.
    • Second telegram notification (notif2) would be sent using IP $IP of login2 but ipinfo.io response of ($CITY, ...) login1.

    (It could be even worse if login1's curl complete between one of you cat+sed parsing: the city in notif1 would be said to be in country of notif2 ;)

    You should use mktemp to create a true temporary, unique, non-shared file (dedicated to you script).

    Here is some update:

    #!/bin/bash
    # ^Shebang is good^
    
    USERID="<Enter_the_user_id_of_the_recipient>" 
    KEY="<The_key_of_your_telegram_bot>" 
    TIMEOUT="10"
    URL="https://api.telegram.org/bot$KEY/sendMessage"
    DATE_EXEC="$(date "+%d %b %Y %H:%M")" #Collect date & time.
    TMPFILE="$(mktemp)" #Create a temporary file to keep data in. # True temporary file
    if [ -n "$SSH_CLIENT" ] && [ -z "$TMUX" ]; then #Trigger
    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.
    IP_INFOS="$(jq -r '.org + " - " + .city + ", " + .region + ", " + .country' "$TMPFILE")" #Client IP info parsing via jq
    TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - ${IP_INFOS} port $PORT"
    curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    rm $TMPFILE #clean up after
    fi

    In fact, you could even curl+jq without creating a temporary file:

    # [...]
    IP_INFOS="$(curl https://ipinfo.io/$IP -s -o $TMPFILE | jq -r '.org + " - " + .city + ", " + .region + ", " + .country')"
    # [...]
  • @tokunag Sorry I didn't realize people were commenting here. In case you still would like to know it should be like this. KEY="123456789:ASFSdfsdf43sfaSERAFASD-PBFF4f54c"

  • @CDuv This is excellent! Id rather do without the jq dependency though.

  • Hi, thanks for the script, @8192.

    I add a new version based on the one shared by @CDuv. The only change is that the temporary file is removed even if the condition is not met.

    Greetings.

    #!/bin/bash
    # ^Shebang is good^
    
    USERID="<Enter_the_user_id_of_the_recipient>"
    KEY="<The_key_of_your_telegram_bot>"
    TIMEOUT="10"
    URL="https://api.telegram.org/bot$KEY/sendMessage"
    DATE_EXEC="$(date "+%d %b %Y %H:%M")" #Collect date & time.
    TMPFILE="$(mktemp)" #Create a temporary file to keep data in. # True temporary file
    if [ -n "$SSH_CLIENT" ] && [ -z "$TMUX" ]; then #Trigger
            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.
            IP_INFOS="$(jq -r '.org + " - " + .city + ", " + .region + ", " + .country' "$TMPFILE")" #Client IP info parsing via jq
            TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - ${IP_INFOS} port $PORT"
            curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
    fi
    rm $TMPFILE #clean up after
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment