- 论坛徽章:
- 42
|
新装环境.测试通过.- #/usr/bin/env perl
- #测试anyevent::snmp 并发
- use AnyEvent;
- use AnyEvent::SNMP;
- use Net::SNMP;
- my $cocurrent = 100; # 并发数
- #my @todoList = map { "192.168.6." . $_ } (1 .. 254); #
- my @todoList = ("192.168.6.87","192.168.6.25","192.168.6.45"); #
- my $oid=".1.3.6.1.2.1.1.1.0"; #sysDescr
- my $cv = AnyEvent->condvar;
- doit() foreach 1..$cocurrent;
- sub doit{
- my $ip = shift @todoList;
- return if not defined $ip;
- $cv->begin;
- # my $oid=".1.3.6.1.2.1.1.7.0";
- # my @oids=($oid);
- print "$ip.\n";
- my $sess=Net::SNMP->session (-hostname => $ip,
- -community => "public",
- -timeout => 1,
- -nonblocking => 1,
- -translate => [-timeticks => 0x0]
- )
- ->get_request (
- -varbindlist => [ $oid ],
- #-varbindlist => \@oids,
- -callback => sub { done(@_,$ip) }
- );
- }
- sub done {
- my ($session,$ip) = @_;
- my $result = $session->var_bind_list();
- if (!defined $result) {
- printf "ERROR: Get request failed for host '%s': %s.\n",
- $session->hostname(), $session->error();
-
- }
- else{
- printf "The sysDescr for host '%s' is %s.\n",
- $session->hostname(), $result->{$oid};
- }
- $cv->end();
- &doit();
- }
- $cv->recv();
复制代码 |
|