Get SNMP IP list

1.6k Views Asked by At

I am having 30 computer in my network and there are five network printers so how can i found only those five IP of network printers through SNMP...if i listing all IP and use snmp like

//OctetString community = new OctetString(arg1);
OctetString community = new OctetString("public");

AgentParameters param = new AgentParameters(community);
param.Version = SnmpVersion.Ver1;
//"1.3.6.1.2.1.1.1"
IpAddress agent = new IpAddress(serv1);
//in serv1 i pass my ip


UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 2);
// target.Timeout = 2000;
//target.Retry = 4;

Pdu pdu = new Pdu(PduType.Get);
pdu.VbList.Add("1.3.6.1.2.1.43.10.2.1.4.1.1");//counter
pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.9.1.1"); //black toner level
pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.9.1.2"); //Cyan toner level
pdu.VbList.Add("1.3.6.1.2.1.2.2.1.6.1"); //Mac Address
pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); 

SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

but searching in all 30 ip is very slow... so how can i find only those 5 ip of my network printers

1

There are 1 best solutions below

0
On

Right now you have at least 25 addresses that will not respond to SNMP. With a timeout of 2 seconds and 4 retries that means 250 seconds of waiting for responses. If you check all of them in parallel you will (theoretically) only have to wait 10 seconds. You can do this by creating threads yourself, but it is easier to use the built in parallel loops:

        List<IPAddress> addresses = new List<IPAddress>()
                                        {
                                            IPAddress.Parse("192.168.1.1"),
                                            IPAddress.Parse("192.168.1.5"),
                                            IPAddress.Parse("192.168.1.10"),
                                            IPAddress.Parse("192.168.1.11"),
                                        };
        Parallel.ForEach(addresses, ip =>
        {
            CheckPrinter(ip);
        });

or

        Parallel.For(1, 255, i =>
        {
            CheckPrinter("192.168.1." + i);
        });