Skip to content

Instantly share code, notes, and snippets.

@arynyklas
Last active February 16, 2025 10:55
Show Gist options
  • Save arynyklas/47b5f77fa5999128ad04139d4a3fdac2 to your computer and use it in GitHub Desktop.
Save arynyklas/47b5f77fa5999128ad04139d4a3fdac2 to your computer and use it in GitHub Desktop.
Send IP to Telegram using Bot API as soon as internet connection established & make SSH tunnel
#!/bin/sh
case "$reason" in
BOUND|RENEW|STATIC)
/usr/local/bin/send_ip_telegram.sh &
;;
esac

Files

send_ip_telegram.service => /etc/systemd/system/send_ip_telegram.service

send_ip_telegram.sh => /usr/local/bin/send_ip_telegram.sh

99-send-ip-to-telegram => /lib/dhcpcd/dhcpcd-hooks/99-send-ip-to-telegram

autossh-tunnel.service => /etc/systemd/system/autossh-tunnel.service

Configure server

sudo adduser --disabled-password --gecos "" tunneluser
sudo usermod -s /usr/sbin/nologin tunneluser
sudo mkdir /home/tunneluser/.ssh
sudo chown tunneluser:tunneluser /home/tunneluser/.ssh
sudo chmod 700 /home/tunneluser/.ssh
sudo chown tunneluser:tunneluser /home/tunneluser/.ssh/authorized_keys
sudo chmod 600 /home/tunneluser/.ssh/authorized_keys
sudo ufw allow 2222/tcp

/etc/ssh/sshd_config:

...
GatewayPorts yes
AllowTcpForwarding yes
...
...
Match User tunneluser
    ForceCommand /usr/bin/echo 'This account is restricted to port forwarding only.'
    AllowTcpForwarding yes
sudo systemctl restart sshd

Connect

[Optional; cause service used] From source (tunnel issuer) - Initiate tunnel:

autossh -N -R 2222:localhost:22 tunneluser@<hostname> -I <identityfile>

From user (not from server and tunnel issuer):

ssh sourceuser@<hostname> -I <identityfile> -p 2222
[Unit]
Description=AutoSSH Tunnel Service
After=network-online.target
Wants=network-online.target
[Service]
User=pi
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -N -R 2222:localhost:22 tunneluser@<hostname> -I <identityfile>
Restart=always
[Install]
WantedBy=multi-user.target
[Unit]
Description=Send IP to Telegram when internet is available
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/bin/send_ip_telegram.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
#!/bin/bash
until curl -s --fail --head https://www.google.com -o /dev/null; do
echo "Waiting for internet connection..."
sleep 5
done
LAST_IP_FILE="/tmp/last_ip.txt"
CURRENT_TIME=$(date +%s)
THROTTLE_TIME=600 # 10 minutes
IP=$(hostname -I | awk '{print $1}')
if [ -f "$LAST_IP_FILE" ]; then
read LAST_IP LAST_TIME < "$LAST_IP_FILE"
if [ "$IP" = "$LAST_IP" ]; then
ELAPSED=$(( CURRENT_TIME - LAST_TIME ))
if [ $ELAPSED -lt $THROTTLE_TIME ]; then
echo "IP unchanged and only $ELAPSED seconds since last notification. No message sent."
exit 0
fi
fi
fi
BOT_TOKEN="..."
CHAT_ID="..."
MESSAGE="Device Local IP: $IP"
curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \
-d chat_id="${CHAT_ID}" \
-d text="${MESSAGE}"
echo "Message sent: ${MESSAGE}"
echo "$IP $CURRENT_TIME" > "$LAST_IP_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment