diff options
author | Tar Committer <tar@ocjtech.us> | 2004-01-12 03:17:26 +0000 |
---|---|---|
committer | Tar Committer <tar@ocjtech.us> | 2004-01-12 03:17:26 +0000 |
commit | 989312339ea2e16579803a48700628c5469e327a (patch) | |
tree | 967ee77195819d70f2dc675444e92340bb98e0d6 /share/getipacctg | |
parent | ff168ecfe045c690c24d5bbc5a3062bf9d64120c (diff) | |
download | rancid-989312339ea2e16579803a48700628c5469e327a.tar.gz rancid-989312339ea2e16579803a48700628c5469e327a.tar.xz rancid-989312339ea2e16579803a48700628c5469e327a.zip |
Imported from rancid-2.3.rc1.tar.gz.rancid-2.3.rc1
Diffstat (limited to 'share/getipacctg')
-rw-r--r-- | share/getipacctg | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/share/getipacctg b/share/getipacctg new file mode 100644 index 0000000..9a5eb20 --- /dev/null +++ b/share/getipacctg @@ -0,0 +1,121 @@ +#! /bin/sh +## +## $Id: getipacctg,v 1.7 2004/01/11 03:43:50 heas Exp $ +## +## Copyright (C) 1997-2004 by Terrapin Communications, Inc. +## All rights reserved. +## +## This software may be freely copied, modified and redistributed +## without fee for non-commerical purposes provided that this license +## remains intact and unmodified with any RANCID distribution. +## +## There is no warranty or other guarantee of fitness of this software. +## It is provided solely "as is". The author(s) disclaim(s) all +## responsibility and liability with respect to this software's usage +## or its effect upon hardware, computer systems, other software, or +## anything else. +## +## Except where noted otherwise, rancid was written by and is maintained by +## Henry Kilmer, John Heasley, Andrew Partan, Pete Whiting, and Austin Schutz. +## +# +# getipacctg uses clogin to login to a cisco router, collect the o/p of +# show ip accounting, and sort by the greatest number of bytes. If a +# second argument is supplied, it is a number indicating the top N producers. +# a third (3 to N) argument(s) specify a prefix(es) to match/select src/dst +# IPs, while others will be filtered. +# +# usage: getipacctg <router name> [<number of lines off the top>] \ +# [<src/dest prefix filter> [...]] +# example: +# getipacctg router 25 192.168.0.0/24 +# will display the top 25 for src or dst ip's within prefix +# 192.168.0.0/24 +# +# Contributed to rancid by Steve Neighorn of SCN Reasearch. + +TMP="/tmp/ipacct.$$.prefixes" +TMP2="/tmp/ipacct.$$.sorted" +TMP3="/tmp/ipacct.$$.pl" + +if [ $# -eq 0 ] ; then + echo "usage: getipacctg router_name [<number of lines off the top>] [<src/dest prefix filter> [...]]" >&2 + exit 1; +fi + +trap 'rm -fr /tmp/ipacct.$$ $TMP $TMP2 $TMP3;' 1 2 15 +clogin -c 'show ip accounting' $1 > /tmp/ipacct.$$ + +if [ $? -ne 0 ] ; then + echo "clogin failed." >&2 + exit 1 +fi +# rest of the command-line options +exec 6>$TMP +HEAD="cat" +shift +while [ $# -ne 0 ] ; do + echo $1 | grep '/' > /dev/null + if [ $? -eq 1 ] ; then + HEAD="head -$1" + else + echo $1 1>&6 + fi + shift +done +6>&- + +egrep '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ +[0-9]+\.[0-9]+\.' /tmp/ipacct.$$ | \ + sed -e 's/^ *//' -e 's/ */ /g' -e 's/.$//' | \ + awk '{print $4":"$0;}' | sort -nr | \ + sed -e 's/^[^:]*://' > $TMP2 + +if [ -s $TMP ] ; then +cat > $TMP3 <<PERL + my(@prefs); + my(\$nprefs) = 0; + sub ip_to_int { + # Given xxx.xxx.xxx.xxx return corresponding integer. + my(\$int); + my(\$ip) = shift; + \$ip=~s/\s*//g; + \$ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\$/; + my (\$a,\$b,\$c,\$d) = (int(\$1),int(\$2),int(\$3),int(\$4)); + return 0 + ((\$a << 24) + (\$b << 16) + (\$c << 8) + \$d) ; + } + open(PREFS, "< \$ARGV[0]") || die "could not open \$ARGV[0]\n"; + while (<PREFS>) { + chomp; + s/\s*//g; + /(.*)\/(.*)\$/; + my(\$ip) = \$1; my(\$mask) = \$2; + \$ip = ip_to_int(\$ip); + \$mask = (~0) << (32 - \$mask); + \$ip = \$ip & (\$mask); + \$prefs[\$nprefs++] = \$ip; + \$prefs[\$nprefs++] = \$mask; + } + close(PREFS); + open(DATA, "< \$ARGV[1]") || die "could not open \$ARGV[1]\n"; + while (<DATA>) { + chomp; + @A = split(/ /); + \$A[0] = ip_to_int(\$A[0]); + \$A[1] = ip_to_int(\$A[1]); + for (\$f = 0; \$f < \$nprefs; \$f += 2) { + if ((\$A[0] & \$prefs[\$f + 1]) == \$prefs[\$f] || + (\$A[1] & \$prefs[\$f + 1]) == \$prefs[\$f]) { + print "\$_\n"; + break; + } + } + } +PERL + perl $TMP3 $TMP $TMP2 | $HEAD +else + $HEAD $TMP2 +fi + +rm -fr /tmp/ipacct.$$ $TMP $TMP2 $TMP3 +trap ';' 1 2 15 +exit 0 |