int SnmpScan(char *pAddr,char *pwd)
{
//通过密码pwd扫描ip地址为pAddr的snmp设备,成功返回1,失败返回0
Snmp::socket_startup(); // Initialize socket subsystem
//---------[ make a GenAddress and Oid object to retrieve ]---------------
UdpAddress address(pAddr); // make a SNMP++ Generic address
if ( !address.valid()) { // check validity of address
cout << "Invalid Address or DNS Name, " << pAddr << "\n";
return 0;
}
//---------[ determine options to use ]-----------------------------------
snmp_version version=version1; // default is v1
int retries=1; // default retries is 1
int timeout=TIMEOUT/10; // default is 1 second
u_short port=161; // default snmp port is 161
OctetStr community(pwd); // community name
//----------[ create a SNMP++ session ]-----------------------------------
int status;
// bind to any port and use IPv6 if needed
Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));
if ( status != SNMP_CLASS_SUCCESS) {
cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
return 0;
}
//--------[ build up SNMP++ object needed ]-------------------------------
address.set_port(port);
// cout << " Community=" << community.get_printable() << endl << flush;
UdpAddressCollection addresses;
// do multiple loops as requested from "retries"
for (int loops=1; loops<= retries + 1; ++loops)
{
status = snmp.broadcast_discovery(addresses,
(timeout + 99) / 100,
address, version, &community);
if (status == SNMP_CLASS_SUCCESS)
{
cout << "SNMP++ Success sending broadcast " << loops << "." << endl;
}
else
{
cout << "SNMP++ Broadcast Error, " << snmp.error_msg( status)
<< " (" << status <<")" << endl;
}
}
// filter out duplicates
UdpAddressCollection filtered_addrs;
int dummy_pos;
for (int n=0; n < addresses.size(); ++n)
if (filtered_addrs.find(addresses[n], dummy_pos) == FALSE)
filtered_addrs += addresses[n];
int count=filtered_addrs.size();
Snmp::socket_cleanup(); // Shut down socket subsystem
if(count==0)return 0;else return 1;
}