diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2014-05-26 11:11:55 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2014-05-26 11:11:55 +1200 |
commit | 64ff240537623fb4648b790b6b78044401446529 (patch) | |
tree | 75aa33648755b58160eb64e37f3ae82ad9fcc733 /bin | |
download | nwatch-64ff240537623fb4648b790b6b78044401446529.tar.gz nwatch-64ff240537623fb4648b790b6b78044401446529.zip |
First commit of project
Just the script, a man page, a README, and a LICENSE. Probably doesn't
need much else.
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/nwatch | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/bin/nwatch b/bin/nwatch new file mode 100755 index 0000000..2dcad63 --- /dev/null +++ b/bin/nwatch @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +# +# nwatch(1) -- Slightly friendlier version of the suggested script in the +# manual page for ndiff(1) from the Nmap suite, implemented in Bash. +# +# Usage: +# $ nwatch HOSTLIST CACHEDIR +# Example with root privileges: +# # nwatch /etc/nwatch.mynet /var/cachedir/nwatch/mynet +# +# Prints the results of the diff to stdout; intended to be called from cron(8): +# 0 0 * * 0 nwatch /etc/nwatch.mynet /var/log/nwatch/mynet +# +# I recommend you use croncrypt, so you don't leak your network information in +# plain text in your email: <https://github.com/tejr/croncrypt> +# +# Author: Tom Ryder <tom@sanctum.geek.nz> +# Copyright: 2014 +# License: GPLv2 (same as Nmap itself) +# + +# Some fixed values +self=nwatch +hostlist=${1:?} +cachedir=${2:?} +latest=$cachedir/${self}.scan.latest + +# Create the cachedir or give up +if ! mkdir -p "$cachedir" ; then + exit +fi + +# Today's values +date=$(date +%s) +cache=$cachedir/${self}.scan.${date} +diff=$cachedir/${self}.diff.${date} + +# Run the scan or give up +if ! nmap -Pn -T4 -v -iL "$hostlist" -oA "$cache" >/dev/null ; then + exit +fi + +# If the link to the XML file is legible, run the diff or give up +if [[ -r ${latest}.xml ]] ; then + if ! ndiff -- "$latest".xml "$cache".xml > "$diff" ; then + exit + fi +fi + +# Create or update the links +rm -f "${cache/$date/latest}".* +for cachetype in "$cache".* ; do + ln -s -- "$cachetype" "${cachetype/$date/latest}" +done + +# Write diff to stdout if it exists (not an error if it doesn't) +if [[ -r $diff ]] ; then + cat -- "$diff" +fi + |