#!/usr/bin/perl
#this script does traceroutes to hosts specified in ./hostseed
#records all the intermediate IPs and then tests each against
#the $commstring community string (default public). If any router
#responds back with a valid snmp response for $commstring, it
#STDOUTS the  sysObjectID from the router.
# syntax: snmptest hostseed [communitystring] [OID]
# omer 10/21
use Net::SNMP;
$|++;

$seedfile=shift || 'hostseed';
$commstring=shift || 'public';
$oid=shift || '.1.3.6.1.2.1.1.2.0';
open(SEEDFILE,"<$seedfile");
foreach (<SEEDFILE>)	{
	$host = $_; chomp $host;
@intermediate_ips=`traceroute -n -q 2 $host 2>/dev/null | awk '{print \$2}'| egrep "^[0-9]?[0-9]?[0-9]\."`;  
	#@intermediate_ips = `traceroute -n -q 2 $host 2>/dev/null | awk '{print \$2}' | egrep "^[0-9]?[0-9]?[0-9]\."`;
		foreach $hop (@intermediate_ips)	{
		chomp $hop;
		#print "hopper: $hop\n";
		($session, $err) = Net::SNMP->session(
                        	Hostname        =>      $hop,
                        	Community       =>      $commstring,
                        	Port            =>      161
                                );
		       if (!defined($session)) {
           			printf("ERROR: %s\n", $err);
           			last;
        					}
			$response = $session->get_request($oid);
			print "$hop\t";
			print ",$commstring\t";
			print ",sysObjectID=$response->{$oid}\n";        
						}
			}
