{"id":43,"date":"2011-05-20T11:56:14","date_gmt":"2011-05-20T10:56:14","guid":{"rendered":"http:\/\/www.phillips321.co.uk\/?p=43"},"modified":"2011-07-14T11:23:28","modified_gmt":"2011-07-14T10:23:28","slug":"subnet-monitoring-script","status":"publish","type":"post","link":"https:\/\/www.phillips321.co.uk\/2011\/05\/20\/subnet-monitoring-script\/","title":{"rendered":"Subnet monitoring script"},"content":{"rendered":"<p>I have wrote a little script (matts-monitor.sh) to monitor for new devices on your subnet and then perform an action against each new device.<\/p>\n<div class=\"codecolorer-container text vibrant\" style=\"overflow:auto;white-space:nowrap;width:100%;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<br \/>73<br \/>74<br \/>75<br \/>76<br \/>77<br \/>78<br \/>79<br \/>80<br \/>81<br \/>82<br \/>83<br \/>84<br \/>85<br \/>86<br \/>87<br \/>88<br \/><\/div><\/td><td><div class=\"text codecolorer\">#!\/bin\/bash<br \/>\n#<br \/>\n# matts-monitor.sh v1.1<br \/>\n# This tool allows you to monitor your current subnet and then runs a command against the new target!<br \/>\n# Create by Matthew Phillips<br \/>\n# New versions can be downloaded from www.phillips321.co.uk<br \/>\nVERSION=&quot;1.1&quot;<br \/>\n#<br \/>\n# This tool requires nmap to be installed and to be run as root<br \/>\n#<br \/>\n# ChangeLog....<br \/>\n# Version 1.1 - Improved sort code<br \/>\n# &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - Added better GATEWAY detection<br \/>\n# &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - Allowed script escape by pressing enter<br \/>\n# &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - Collated argument checking into 1 if statement<br \/>\n# &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - Reports version number within usage<br \/>\n# Version 1.0 - First Release<br \/>\n#################################################################<br \/>\n# CHECKING FOR ROOT<br \/>\n#################################################################<br \/>\nif [ `echo -n $USER` != &quot;root&quot; ]<br \/>\nthen<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE:&quot;<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE: ERROR: Please run as root!&quot;<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE:&quot;<br \/>\n&nbsp; &nbsp; exit 1<br \/>\nfi<br \/>\n<br \/>\n#################################################################<br \/>\n# CHECKING TO SEE IF INTERFACE AND INTERVAL PROVIDED<br \/>\n#################################################################<br \/>\nif [ -z ${1} ] || [ -z ${2} ]<br \/>\nthen<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE: Version number ${VERSION}&quot;<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE: Usage: `basename ${0}` [interface] [time between scans (secs)]&quot;<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE: Example #`basename ${0}` eth0 60&quot;<br \/>\n&nbsp; &nbsp; exit 1<br \/>\nelse<br \/>\n&nbsp; &nbsp; INTERFACE=&quot;`echo &quot;${1}&quot; | cut -c 1-6`&quot;<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE: Monitoring ${1} for new devices&quot;<br \/>\n&nbsp; &nbsp; INTERVAL=&quot;`echo &quot;${2}&quot; | tr -cd '[:digit:]' | cut -c 1-4`&quot;<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE: Scanning once every ${INTERVAL} seconds&quot;<br \/>\nfi<br \/>\n<br \/>\n#################################################################<br \/>\n# IDENTIFY IP, GATEWAY and SUBNET<br \/>\n#################################################################<br \/>\nIPADDR=`ifconfig ${INTERFACE} | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'`<br \/>\nSUBNET=`ifconfig ${INTERFACE} | grep 'Mask:'| grep -v '127.0.0.1' | cut -d: -f4`<br \/>\nGATEWAY=`route -n | grep ${INTERFACE} | grep UG | sed -e 's\/[ \\t][ \\t]*\/#\/g' | cut -d '#' -f 2`<br \/>\necho &quot;MESSAGE: interface=${INTERFACE} gateway=${GATEWAY} ip.addr=${IPADDR} subnet=${SUBNET}&quot;<br \/>\n<br \/>\n#################################################################<br \/>\n# PERFORMING FIRST SCAN TO CREATE WHITELIST<br \/>\n#################################################################<br \/>\narp-scan -l -I ${INTERFACE} | sed -e '1,2d' -e '\/^$\/,+2 d' -e 's\/[ \\t][ \\t]*\/#\/g' | cut -d '#' -f 1 &gt; WHITELIST.txt<br \/>\nif [[ -s WHITELIST.txt ]] ; then<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE: The following devices were found and will be excluded from this monitor.&quot;<br \/>\n&nbsp; &nbsp; cat WHITELIST.txt<br \/>\nelse<br \/>\n&nbsp; &nbsp; echo &quot;MESSAGE: No IPs found during arp-scan, are you sure your interface is up?.&quot;<br \/>\n&nbsp; &nbsp; exit 1<br \/>\nfi ;<br \/>\n<br \/>\n#################################################################<br \/>\n# THIS IS THE MONITORING BIT<br \/>\n#################################################################<br \/>\necho &quot;MESSAGE: Press enter to exit the scanner&quot;<br \/>\nwhile true; do<br \/>\n&nbsp; &nbsp; arp-scan -l -I ${INTERFACE} | sed -e '1,2d' -e '\/^$\/,+2 d' -e 's\/[ \\t][ \\t]*\/#\/g' | cut -d '#' -f 1 &gt; SCAN.txt<br \/>\n&nbsp; &nbsp; sort SCAN.txt -o SCAN.txt<br \/>\n&nbsp; &nbsp; NEWIP=`diff -a SCAN.txt WHITELIST.txt | grep \\&lt; | sed -e 's\/&lt; \/\/'`<br \/>\n&nbsp; &nbsp; if [ ! -z ${NEWIP} &nbsp;]; then<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; echo &quot;MESSAGE: New IP detected!!! ${NEWIP}&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; echo ${NEWIP} &gt;&gt; WHITELIST.txt<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; #################################################################<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; # To run a command when new device found please enter it here<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; xterm -e &quot;echo we have found a new ip ${NEWIP} ; sleep 10&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; ################################################################# &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; fi<br \/>\n&nbsp; &nbsp; sort WHITELIST.txt -o WHITELIST.txt<br \/>\n&nbsp; &nbsp; read -t ${INTERVAL} &amp;&amp; break<br \/>\ndone<br \/>\n<br \/>\n#################################################################<br \/>\n# DELETE FILES CREATED DURING MONITORING<br \/>\n#################################################################<br \/>\nrm -rf SCAN.txt WHITELIST.txt<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I have wrote a little script (matts-monitor.sh) to monitor for new devices on your subnet and then perform an action against each new device. 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788#!\/bin\/bash # # matts-monitor.sh v1.1 # This tool allows you to monitor your current subnet and then runs a command against the new target! # Create by Matthew Phillips # New [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,4],"tags":[33,34,32,31],"_links":{"self":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts\/43"}],"collection":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":7,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts\/43\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}