Skip to content

Instantly share code, notes, and snippets.

@akirad
Created May 22, 2013 05:03
Show Gist options
  • Save akirad/5625349 to your computer and use it in GitHub Desktop.
Save akirad/5625349 to your computer and use it in GitHub Desktop.
A perl script which detects running nodes in networks. It also can get MIB. It is necessary to specify conf file(detectNodes.conf) and binary of Snmpwalk.java.
IPaddr: 192.168.0.1-20
IPaddr: 192.168.0.30-40
oid: .1.3.6.1.2.1.1.3
com: public
use strict;
use warnings;
use Net::Ping;
#----------------------------
# Global defs.
#----------------------------
my %CONF;
my $SNMP_WALK_CMD = 'Snmpwalk';
#----------------------------
# Main
#----------------------------
print "### detectNodes Starts. ###\n";
%CONF = readConfFile('detectNodes.conf');
my @targetIpAddrs = makeTargetIpAddrList($CONF{'ipAddrRanges'});
my @aliveNodes = execPing(@targetIpAddrs);
execSnmpwalk(@aliveNodes);
print "### detectNodes Ends. ###\n";
#----------------------------
# sub functions
#----------------------------
sub readConfFile{
my $confFile = shift;
open(FILE, $confFile) or die "$!";
my %conf;
my @ipAddrRanges;
while (my $line = <FILE>) {
if($line =~ /^\s*#/){
next;
}
chomp($line);
if($line =~ /IPaddr:/){
my @work = split(/:/, $line);
push(@ipAddrRanges, $work[1]);
}
if($line =~ /oid:/){
my @work = split(/:/, $line);
$conf{'oid'} = $work[1];
}
if($line =~ /com:/){
my @work = split(/:/, $line);
$conf{'com'} = $work[1];
}
}
$conf{'ipAddrRanges'} = \@ipAddrRanges;
close(FILE);
return %conf;
}
sub makeTargetIpAddrList{
my $ref_ipAddrRanges = shift;
my @ipAddrRanges = @$ref_ipAddrRanges;
my @targetIpAddrs;
foreach my $ipAddrRange (@ipAddrRanges){
my @octets = split(/\./, $ipAddrRange);
my @fourthOctetRange = split(/-/, $octets[3]);
my $startIP = $fourthOctetRange[0];
my $endIP = $fourthOctetRange[1];
for (my $i = $startIP; $i <= $endIP; $i++){
my $targetIpAddr = $octets[0]."\.".$octets[1]."\.".$octets[2]."\.".$i;
# Remove spaces for ping execution.
$targetIpAddr =~ s/^\s*(.*?)\s*$/$1/;
push(@targetIpAddrs, $targetIpAddr);
}
}
return @targetIpAddrs;
}
sub execPing{
my @ipAddrs = @_;
my @aliveNodes;
my @downNodes;
my $pingTimeout=2;
my $pingObj = Net::Ping->new("icmp"); # cmd is need to be started as admin for ping execution.
print("Exec ping...\n");
foreach my $ipAddr (@ipAddrs){
if($pingObj->ping($ipAddr, $pingTimeout)){
print "$ipAddr is running.\n";
push(@aliveNodes, $ipAddr);
}
else{
push(@downNodes, $ipAddr);
}
}
$pingObj->close();
print("\n");
return @aliveNodes;
}
sub execSnmpwalk{
my @aliveNodes = @_;
print("Exec snmpwalk...\n");
foreach my $aliveNode (@aliveNodes){
print(qq[------- Execute "$SNMP_WALK_CMD -c $CONF{'com'} $aliveNode $CONF{'oid'}" -------\n]);
#system("$SNMP_WALK_CMD -c $CONF{'com'} $aliveNode $CONF{'oid'}");
system("java -cp lib/snmp4j-2.1.0.jar;. $SNMP_WALK_CMD -c $CONF{'com'} $aliveNode $CONF{'oid'}");
print("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment