Skip to content

Instantly share code, notes, and snippets.

@MOAMIndustries
Created February 17, 2022 10:31
Show Gist options
  • Save MOAMIndustries/b314ad4ea1a689b031864338893bcc25 to your computer and use it in GitHub Desktop.
Save MOAMIndustries/b314ad4ea1a689b031864338893bcc25 to your computer and use it in GitHub Desktop.
Publish IP Address of raspberry pi to webhook
#!/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
#!/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