aboutsummaryrefslogtreecommitdiff
path: root/bin/nwatch
diff options
context:
space:
mode:
Diffstat (limited to 'bin/nwatch')
-rwxr-xr-xbin/nwatch61
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
+