vartik

detectnewhosts.sh

Nov 25th, 2020 (edited)
738
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. LASTFILE=/var/lib/detectnewhosts.last
  3. SUBNET=192.168.0.0/24
  4. EMAILADDR=user@example.org
  5. SLEEPTIME=60
  6.  
  7. echo -n "Detecting new hosts"
  8. while true ; do
  9.     echo -n "."
  10.     nmap -n -sn -PR -oX - "$SUBNET" > "$LASTFILE.new"
  11.     if [ -e "$LASTFILE" ] ; then
  12.         DIFF=$(ndiff "$LASTFILE" "$LASTFILE.new" | grep '^\+.*:$')
  13.         if [ ! -z "$DIFF" ] ; then
  14.             echo
  15.             echo "$(date): " $DIFF
  16.             if [ ! -z "$EMAILADDR" ] ; then
  17.                 echo "$DIFF" | mail -s "New hosts detected" "$EMAILADDR"
  18.             fi
  19.         fi
  20.     fi
  21.     mv -f "$LASTFILE.new" "$LASTFILE"
  22.     sleep "$SLEEPTIME"
  23. done
  24.  
RAW Paste Data