Skip to content

Instantly share code, notes, and snippets.

@fnzv
Created July 10, 2022 17:32
Show Gist options
  • Save fnzv/ed0c879c261c87190bcfbb1090bd32a4 to your computer and use it in GitHub Desktop.
Save fnzv/ed0c879c261c87190bcfbb1090bd32a4 to your computer and use it in GitHub Desktop.
Monitor LAN hosts and ship notifications to TG - example script
#!/bin/bash
#
#Script to monitor the network LAN ARP segment and send out notifications in case of changes
#Set it up via cronjob - e.g. */5 * * * * root cd /var/hostmon && bash hostmon.sh
#Save previous scan
cp scan.txt previousscan.txt
# Exception lists to be populated for flapping cases / dhcp / low wifi connection - Name or MAC address)
excluded_list=(Samsung FF:FF:46:FF:FF:FF)
#Gather the live hosts and save them into a temporary file
arp-scan --interface=ens192 --localnet | grep 192 | sort | grep -F -v -f <(printf "%s\n" "${excluded_list[@]}") > scan.txt
# Get the diff from previous scan
diff -c previousscan.txt scan.txt | grep "+ " > tmpfile.txt
# Send out notifications for each line in diff
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
echo "${LINE}"
/usr/bin/curl -s -X POST --output /dev/null https://api.telegram.org/YOUR_TG_TOKEN/sendMessage -d "text=$LINE" -d chat_id=YOUR_CHAT_ID
done < tmpfile.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment