Skip to content

Instantly share code, notes, and snippets.

@luckman212
Last active January 9, 2023 10:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckman212/e5df683d8b11dd68aef3255efc22e611 to your computer and use it in GitHub Desktop.
Save luckman212/e5df683d8b11dd68aef3255efc22e611 to your computer and use it in GitHub Desktop.
hopfinder.sh - find good candidates for gateway monitoring (dpinger) in pfSense
#!/bin/sh
# hopfinder for pfSense
# finds nearby upstream targets for gateway monitoring
# gist: e5df683d8b11dd68aef3255efc22e611
list_ifs() {
/usr/local/bin/php <<EOP
<?php
require_once('interfaces.inc');
\$a_gateways = return_gateways_array();
foreach (\$a_gateways as \$idx => \$gw) {
if ((\$gw['ipprotocol'] == 'inet') && (substr(\$gw['interface'],0,4) != "ovpn")) {
printf("%s%s%s%s%s\n", \$gw['interface'], ' - ', \$gw['name'], ' - ', \$gw['gateway']);
}
}
?>
EOP
}
# requires interface as parameter
if [ -z "$1" ]; then
echo "specify an interface:"
list_ifs
exit
fi
#clear the temp file that will store the trace results
o=/tmp/hops
:>$o
targets='
8.8.8.8
8.8.4.4
1.1.1.1
1.0.0.1
9.9.9.9
google.com
yahoo.com
cnn.com
github.com
nytimes.com
facebook.com
instagram.com
twitter.com
amazon.com
netflix.com
'
for t in $targets; do
echo tracing $t
#skip 1st hop (-M2) max 15 hops (-m15) use ICMP (-P icmp) 2 queries (-q2) 1s wait (-w1)
#send stderr to bitbucket and filter out *s (no response)
traceroute -i $1 -M2 -m15 -P icmp -q1 -n -w1 $t 2>/dev/null | awk '$2 != "*" { print $2 }' >>$o
done
#filter and reverse-sort results by frequency
sort $o | uniq -c | awk '$1 > 1 { print }' | sort -rn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment