#!/bin/sh
# the next line restarts using scotty -*- tcl -*- \
exec scotty "$0" "$@"

package require Tnm 2.1

##
## Walk a MIB subtree starting by label using the SNMP session s.
##

proc walk { s label } {
    $s walk vbl $label {
	set vb    [lindex $vbl 0]
	set oid   [lindex $vb  0]
	set type  [lindex $vb  1]
	set value [lindex $vb  2]
	puts [format "%-16s : %s" [mib name $oid] $value]
    }
}

##
## Walk a MIB subtree starting by label. We try to use the host argument
## as an SNMP alias. This allows us to talk with the correct parameters
## needed to various SNMPv1/SNMPv2 agents.
##

proc start { host label } {

    if {[lsearch [snmp alias] $host] >= 0} {
	puts stderr "Trying SNMP alias $host..."
	set s [snmp session -alias $host]
    } 
    if [info exists s] {
	set code [catch {walk $s $label} msg]
	$s destroy
	if {$code} {
	    puts stderr $msg
	}
	return
    }

    foreach version "SNMPv1 SNMPv2C" {
	puts stderr "Trying $version community public on $host..."
	if {[catch {snmp session -address $host -version $version} s]} {
	    puts stderr $s
	    return
	}
	set code [catch {walk $s $label} msg]
	$s destroy
	if {$code} {
	    puts stderr $msg
	} else {
	    return
	}
    }
}

##
## Check the command line and start the MIB walk.
##

proc usage {} {
    puts stderr "usage: snmpwalk host subtree"
    exit
}

if {[llength $argv] != 2} { usage } else {
    start [lindex $argv 0] [lindex $argv 1]
}

exit
