Advertisement
vartik

detectnewhosts.sh

Nov 25th, 2020 (edited)
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.56 KB | None | 0 0
  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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement