Created
February 17, 2022 10:31
-
-
Save MOAMIndustries/b314ad4ea1a689b031864338893bcc25 to your computer and use it in GitHub Desktop.
Publish IP Address of raspberry pi to webhook
This file contains 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 | |
# Announces the IP addresses of connected Interfaces to slack | |
# loops until curl is able to successfully POST to the webook | |
WEBHOOK="https://hooks.slack.com/services/FOO/BAR" | |
IPs=$(ip -br address | awk -F "[ ]+" '/UP/{print " ", $1, $3}') | |
SERIAL=$(cat /proc/cpuinfo | awk -F "[: ]+" '/Serial/{print " ", $2}') | |
HOSTNAME=$(hostname) | |
until curl -s -X POST -H 'Content-type: application/json' --data "{\"text\":\"\n*Raspberry Pi now online*\n_Serial:_ $SERIAL\n_Host:_ $HOSTNAME\n_Interface addresses:_\n$IPs\"}" $WEBHOOK | |
do | |
sleep 3 | |
IPs=$(ip -br address | awk -F "[ ]+" '/UP/{print " ", $1, $3}') # Refresh IP Information | |
done | |
exit 0 |
This file contains 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 | |
# Creates a new service that executes the script above | |
PATH_TO_ANNOUNCE="/home/ubunutu/announce_ip.sh" # Update with correct path to your script | |
chomd +x $PATH_TO_ANNOUNCE # allow root to execute script | |
# Create service file | |
cat << EOF > ./ip_alert.service | |
[Unit] | |
Description=Sends message with IP to Slack Webhook | |
[Service] | |
Type=simple | |
ExecStart=/bin/bash $PATH_TO_ANNOUNCE | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
sudo mv ip_alert.service /etc/systemd/system/ip_alert.service | |
sudo systemctl daemon-reload | |
sudo systemctl enable ip_alert.service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment