head	1.7;
access;
symbols;
locks; strict;
comment	@# @;


1.7
date	99.02.10.15.28.55;	author trockij;	state Exp;
branches;
next	1.6;

1.6
date	98.12.28.05.56.10;	author trockij;	state Exp;
branches;
next	1.5;

1.5
date	98.12.28.05.00.46;	author trockij;	state Exp;
branches;
next	1.4;

1.4
date	98.12.28.04.21.12;	author trockij;	state Exp;
branches;
next	1.3;

1.3
date	98.12.28.04.01.12;	author trockij;	state Exp;
branches;
next	1.2;

1.2
date	98.12.27.23.58.55;	author trockij;	state Exp;
branches;
next	1.1;

1.1
date	98.12.27.19.54.46;	author trockij;	state Exp;
branches;
next	;


desc
@@


1.7
log
@*** empty log message ***
@
text
@#!/usr/local/bin/perl
#
# dump bridge tables for a list of switches
#
# $Id: btdump,v 1.6 1998/12/28 05:56:10 trockij Exp trockij $
#
# Copyright (C) 1998 Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use A3Com::BridgeTable;
use A3Com::ARP;
use A3Com::EtherDetail;
use Getopt::Std;
use Socket;

sub usage {
    print STDERR "$0: @@_\n" if @@_;
    die <<EOF;
usage: $0 [-a addr] [-c] [-f] [-n] [-m] [-s] switch [switch...]
    -f	do not use the cache (slow)
    -c	force the cache to not expire
    -d	show port detail
    -n	print nodes as IP addresses, not hostnames
    -m	print nodes as MAC addresses
    -s	do not cache port detail (autoneg, port state, etc.)
    -a	find an addr

EOF
}

sub lookup;

getopts ("a:cdfnms", \%opt) || usage ("argument error");

@@ARGV || usage ("no list of switches supplied");

%HOSTHASH = ();

if ($opt{"a"}) {
    if ($opt{"a"} =~ /:/) {
    	$TO_FIND = $opt{"a"};
    } elsif ($opt{"a"} =~ /^\d+\.\d+\.\d+\.\d+$/) {
    	$TO_FIND = $opt{"a"};
    } elsif ($opt{"a"} =~ /^[a-z][a-z0-9-]*$/) {
    	die "could not resolve host $opt{a}\n"
	    if (!defined ($TO_FIND = gethostbyname ($opt{"a"})));
	$TO_FIND = inet_ntoa ($TO_FIND);
    } else {
    	die "unidentified address type\n";
    }
}

#
# get ARP stuff
#
$helium = new A3Com::ARP;
$helium->switch ("helium");
$helium->cache (0) if ($opt{"f"});
$helium->cachetime(time) if ($opt{"c"});

$hydrogen = new A3Com::ARP;
$hydrogen->switch ("hydrogen");
$hydrogen->cache (0) if ($opt{"f"});
$hydrogen->cachetime(time) if ($opt{"c"});

if ($opt{"a"} && $TO_FIND =~ /\d+\./) {
    if (defined ($n = $hydrogen->mac ($TO_FIND))) {
    	$TO_FIND = $n;
    } elsif (defined ($n = $helium->mac ($TO_FIND))) {
    	$TO_FIND = $n;
    } else {
    	die "IP address not found in ARP tables\n";
    }
}

if ($opt{"d"}) {
    print "#\n# switch port portState actualPortMode autoNegMode addrs\n#\n";
} else {
    print "#\n# switch port addrs\n#\n";
}

#
# walk through switches and display bridge tables (with IP addresses)
#
foreach $switch (@@ARGV) {
    $sw = new A3Com::BridgeTable;
    $sw->switch ($switch);
    $sw->cache (0) if ($opt{"f"});
    $sw->cachetime (time) if ($opt{"c"});

    if ($opt{"d"}) {
	$swdetail = new A3Com::EtherDetail;
	$swdetail->switch ($switch);
	$swdetail->cache (0) if ($opt{"s"});
	$swdetail->cachetime (time) if ($opt{"c"});
    }

    @@ports = $sw->ports;
    die "error for $switch: " . $sw->error . "\n" if (@@ports == 0);

    foreach $port (sort { $a <=> $b} @@ports) {
	my $detail;
	if ($opt{"d"}) {
	    $detail = $swdetail->port_val ($port, "portState") .
		    " " . $swdetail->port_val ($port, "actualPortMode") .
		    " " . $swdetail->port_val ($port, "autoNegMode");
	}

	@@macs = $sw->port ($port);
	if (@@macs == ()) {
	    if ($opt{"d"}) {
		print "$switch $port $detail\n";
	    } else {
		print "$switch $port\n";
	    }
	    next;
	}

	if ($opt{"m"} && ! $opt{"a"}) {
	    if ($opt{"d"}) {
		print "$switch $port $detail @@macs\n";
	    } else {
		print "$switch $port @@macs\n";
	    }
	    next;
	}

	@@addrs = ();
	foreach $mac (@@macs) {
	    next if ($opt{"a"} && $mac ne $TO_FIND);

	    if ($opt{"m"}) {
	    	push @@addrs, $mac;
		next;
	    }

	    @@ips = $helium->ip ($mac);

	    if (!defined $ips[0]) {
		@@ips = $hydrogen->ip ($mac);
		if (!defined $ips[0]) {
		    @@ips = "$mac";
		}
	    }
	    push @@addrs, @@ips;
	}

	next if ($opt{"a"} && @@addrs == ());

	if (!$opt{"n"}) {
	    for (@@addrs) {
		$_ = lookup ($_) unless (/:/);
	    }
	}

	if ($opt{"d"}) {
	    print "$switch $port $detail @@addrs\n";
	} else {
	    print "$switch $port @@addrs\n";
	}

    }
}


sub lookup {
    my $ip = shift;

    return $HOSTHASH{$ip} if (defined $HOSTHASH{$ip});
    my $IP = gethostbyaddr (inet_aton ($ip), AF_INET);
    if (!defined $IP) {
    	$HOSTHASH{$ip} = $ip;
	return $ip;
    }
    $IP =~ s/^([^.]+)\..*$/$1/;
    $HOSTHASH{$ip} = $IP;
}
@


1.6
log
@-c force cache
@
text
@d5 17
a21 1
# Jim Trocki
a22 1
# $Id: btdump,v 1.5 1998/12/28 05:00:46 trockij Exp trockij $
@


1.5
log
@hydrogen/helium problem
@
text
@d7 1
a7 1
# $Id: btdump,v 1.4 1998/12/28 04:21:12 trockij Exp trockij $
d17 1
a17 1
usage: $0 [-a addr] [-f] [-n] [-m] [-s] switch [switch...]
d19 1
d31 1
a31 1
getopts ("a:dfnms", \%opt) || usage ("argument error");
d57 1
d62 1
d87 1
d93 1
@


1.4
log
@*** empty log message ***
@
text
@d7 1
a7 1
# $Id: btdump,v 1.3 1998/12/28 04:01:12 trockij Exp trockij $
d62 1
a62 1
    if (defined ($n = $helium->mac ($TO_FIND))) {
@


1.3
log
@added search, corrected -m
@
text
@d7 1
a7 1
# $Id: btdump,v 1.2 1998/12/27 23:58:55 trockij Exp trockij $
d71 6
a93 6
    if ($opt{"d"}) {
	print "#\n# switch port portState actualPortMode autoNegMode addrs\n#\n";
    } else {
	print "#\n# switch port addrs\n#\n";
    }

d95 1
d97 1
a97 1
	    my $detail = $swdetail->port_val ($port, "portState") .
d103 6
a108 2
	if (!defined $macs[0]) {
	    print "$switch $port $detail\n";
d113 5
a117 1
	    print "$switch $port $detail @@macs\n";
d140 2
@


1.2
log
@added portmodes
@
text
@d7 1
a7 1
# $Id: btdump,v 1.1 1998/12/27 19:54:46 trockij Exp trockij $
d17 1
a17 1
usage: $0 [-f] [-n] [-m] [-s] switch [switch...]
d19 1
d23 1
d30 1
a30 1
getopts ("fnms", \%opt) || usage ("argument error");
d36 14
d61 10
d79 5
a83 3
    $swdetail = new A3Com::EtherDetail;
    $swdetail->switch ($switch);
    $swdetail->cache (0) if ($opt{"s"});
d88 6
a93 1
    print "#\n# switch port portState actualPortMode autoNegMode addrs\n#\n";
d95 5
a99 3
	my $detail = $swdetail->port_val ($port, "portState") .
	    	" " . $swdetail->port_val ($port, "actualPortMode") .
		" " . $swdetail->port_val ($port, "autoNegMode");
d107 1
a107 1
	if ($opt{"m"}) {
d112 1
a112 1
	@@iplist = ();
d114 7
d129 1
a129 1
	    push @@iplist, @@ips;
d133 1
a133 1
	    for (@@iplist) {
d138 5
a142 1
	print "$switch $port $detail @@iplist\n";
@


1.1
log
@Initial revision
@
text
@d7 1
a7 1
# $Id$
d10 1
d17 1
a17 1
usage: $0 [-f] switch [switch...]
d21 1
d28 1
a28 1
getopts ("fnm", \%opt) || usage ("argument error");
d53 4
d60 1
d62 4
d67 4
a70 1
	next if (!defined $macs[0]);
d73 1
a73 1
	    print "$switch $port @@macs\n";
d95 3
a97 1
	print "$switch $port $mac @@iplist\n";
@
