Created
November 7, 2017 17:33
-
-
Save sbliven/315abb0da476d741652fcbc98b1648bd to your computer and use it in GitHub Desktop.
Raspberry Pi setup to send IP changes via telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /lib/dhcpcd/dhcpcd-hooks/99-telegram | |
# | |
# Notify telegram chat of ip changes | |
# man dhcpcd-run-hooks for variables | |
TELEGRAM=/home/pi/bin/telegram | |
if $if_up; then | |
case "$reason" in | |
BOUND|BOUND6) | |
if [ "${new_ip_address}" != "${old_ip_address}" ]; then | |
$TELEGRAM "$(hostname) (Raspberry) got ${new_ip_address} on ${interface}. Was ${old_ip_address}." | |
fi | |
;; | |
REBOOT) | |
$TELEGRAM "$(hostname) (Raspberry) got ${new_ip_address} on ${interface} after reboot. Uptime: `uptime`" | |
;; | |
*) | |
$TELEGRAM "$(hostname) (Raspberry) got ${new_ip_address} on ${interface}. Was ${old_ip_address}. Reason: ${reason}" | |
;; | |
esac | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# usage: telegram <message> | |
# strict mode | |
set -euo pipefail | |
# Fill these | |
API_KEY='' | |
CHAT_ID='' | |
function sendMessage { | |
echo "sending" | |
# local MSG="${1:?No message given}" | |
curl -i -X GET -G \ | |
--data-urlencode "chat_id=${CHAT_ID}" \ | |
--data-urlencode "text=${1:?No message given}" \ | |
"https://api.telegram.org/bot${API_KEY}/sendMessage"; | |
} | |
sendMessage "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment