diff options
author | Joachim Tingvold <joachim@tingvold.com> | 2014-04-06 03:11:04 +0200 |
---|---|---|
committer | Joachim Tingvold <joachim@tingvold.com> | 2014-04-06 03:11:04 +0200 |
commit | 2a0c0a3dbbdf7fa5040953c0b0d88ad6f62c011e (patch) | |
tree | 92c7cbf54272466b46f64e5dc8d1ddb429858836 /tools/fetch-portlist.sh | |
parent | fe0be5960aac1f9bb600dbf853d862a9f4e60de8 (diff) |
Initial commit. Source; TG13-goodiebag.
Diffstat (limited to 'tools/fetch-portlist.sh')
-rwxr-xr-x | tools/fetch-portlist.sh | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tools/fetch-portlist.sh b/tools/fetch-portlist.sh new file mode 100755 index 0000000..94ca41c --- /dev/null +++ b/tools/fetch-portlist.sh @@ -0,0 +1,42 @@ +print_range() { + FIRST=$1 + LAST=$2 + if [ "$1" = "$2" ]; then + echo $FIRST + else + echo $FIRST-$LAST + fi +} + +walk_ports() { + IP=$1 + COMMUNITY=$2 + + FIRST_PORT= + LAST_PORT= + + for PORT in $( snmpwalk -Os -m IF-MIB -v 2c -c $COMMUNITY $IP ifDescr 2>/dev/null | grep -E 'GigE|Ethernet' | cut -d. -f2 | cut -d" " -f1 ); do + if ! snmpget -m IF-MIB -v 2c -c $COMMUNITY $IP ifHCInOctets.$PORT 2>/dev/null | grep -q 'No Such Instance'; then + if [ "$LAST_PORT" ] && [ `expr $LAST_PORT + 1` = $PORT ]; then + LAST_PORT=$PORT + else + if [ "$LAST_PORT" ]; then + print_range $FIRST_PORT $LAST_PORT + fi + FIRST_PORT=$PORT + LAST_PORT=$PORT + fi + fi + done + + print_range $FIRST_PORT $LAST_PORT +} + +COMMUNITY=$1 +IP=$2 +SYSNAME=$3 +PORTS=$( walk_ports $IP $COMMUNITY | tr "\n" "," | sed 's/,$//' ) + +echo "insert into switchtypes values ('$SYSNAME','$PORTS',true);" +echo "insert into switches values (default,'$IP','$SYSNAME','$SYSNAME',null,default, default, '1 minute', '$COMMUNITY');" + |