使用Java进行SNMP编程-SNMP4J-SNMPv1/v2-代码实例

使用SNMP4J可以很方便的实现一个SNMP NMS的功能。
对于SNMPv1/SNMPv2,简单的说,只需要以下几个步骤
1) 创建Snmp对象snmp

2) 创建CommunityTarget对象target,并指定community, version, address, timeout, retry等参数。

3) 创建PDU对象pdu,并指定操作类型(GET/GETNEXT/GETBULK/SET), 添加VariableBinding(也就是待操作的OID),
    如果是GETBULK操作,还可以指定MaxRepetitions和NonRepeaters。
 注意一定要指定MaxRepetitions,默认值是0,那样不会返回任何结果。

4) 调用snmp.send(pdu, target)方法,发送请求请返回结果。

此外,SNMP4J提供了TableUtils类,可以很轻松的实现一个SNMP Walk的功能,比如一次返回一个表格下面的所有MIB值。
TableUtils类还支持指定ower bound index 和 upper bound index, 得到更小范围的结果以提高性能。这一点对于从一个很多行的表中取一部分行的数据来说很重要。

Snmp还支持异步的send方法。

下面的代码实例实现了GET, GETNEXT, GETBULK, Walk, SET操作,
以及一个利用异步send方法实现的agent discovery: 对一个广播地址发送请求,等待agent返回的应答。
如果需要对一个范围内的IP进行扫描,可以稍加改进,反复调用异步send多次发送给不同IP。

import java.io.IOException;
import java.util.List;
import java.util.Vector;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

public class SNMPv2Test {
	public static void main(String[] args) throws IOException, InterruptedException {
		Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
		snmp.listen();
		
		CommunityTarget target = new CommunityTarget();
		target.setCommunity(new OctetString("public"));
		target.setVersion(SnmpConstants.version2c);
		target.setAddress(new UdpAddress("192.168.0.100/161"));
		target.setTimeout(3000);	//3s
		target.setRetries(1);
				
		sendRequest(snmp, createGetPdu(), target);
		sendRequest(snmp, createGetNextPdu(), target);
		sendRequest(snmp, createGetBulkPdu(), target);
		snmpWalk(snmp, target);
		
		target.setCommunity(new OctetString("private"));
		sendRequest(snmp, createSetPdu(), target);
		
		CommunityTarget broadcastTarget = new CommunityTarget();
		broadcastTarget.setCommunity(new OctetString("public"));
		broadcastTarget.setVersion(SnmpConstants.version2c);
		broadcastTarget.setAddress(new UdpAddress("192.168.0.255/161"));
		broadcastTarget.setTimeout(5000);	//5s
		sendAsyncRequest(snmp, createGetNextPdu(), broadcastTarget);
		Thread.sleep(6000);	//main thread wait 6s for the completion of asynchronous request 
	}
	
	private static PDU createGetPdu() {
		PDU pdu = new PDU();
		pdu.setType(PDU.GET);
		pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.0")));	//sysUpTime
		pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0")));	//sysName
		pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5")));	//expect an no_such_instance error
		return pdu;
	}
	
	private static PDU createGetNextPdu() {
		PDU pdu = new PDU();
		pdu.setType(PDU.GETNEXT);
		pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3")));	//sysUpTime
		pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5")));	//sysName

		return pdu;
	}
	
	private static PDU createGetBulkPdu() {
		PDU pdu = new PDU();
		pdu.setType(PDU.GETBULK);
		pdu.setMaxRepetitions(10);	//must set it, default is 0
		pdu.setNonRepeaters(0);
		pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1")));		//system
		return pdu;
	}
	
	private static PDU createSetPdu() {
		PDU pdu = new PDU();
		pdu.setType(PDU.SET);
		pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("sysname")));	//sysName
		return pdu;
	}
	
	private static void sendRequest(Snmp snmp, PDU pdu, CommunityTarget target)
	throws IOException {
		ResponseEvent responseEvent = snmp.send(pdu, target);
		PDU response = responseEvent.getResponse();
		
		if (response == null) {
			System.out.println("TimeOut...");
		} else {
			if (response.getErrorStatus() == PDU.noError) {
				Vector vbs = response.getVariableBindings();
				for (VariableBinding vb : vbs) {
					System.out.println(vb + " ," + vb.getVariable().getSyntaxString());
				}
			} else {
				System.out.println("Error:" + response.getErrorStatusText());
			}
		}
	}
	
	private static void sendAsyncRequest(Snmp snmp, PDU pdu, CommunityTarget target)
	throws IOException {
		snmp.send(pdu, target, null, new ResponseListener(){

			@Override
			public void onResponse(ResponseEvent event) {
				PDU response = event.getResponse();
				System.out.println("Got response from " + event.getPeerAddress());
				if (response == null) {
					System.out.println("TimeOut...");
				} else {
					if (response.getErrorStatus() == PDU.noError) {
						Vector vbs = response.getVariableBindings();
						for (VariableBinding vb : vbs) {
							System.out.println(vb + " ," + vb.getVariable().getSyntaxString());
						}
					} else {
						System.out.println("Error:" + response.getErrorStatusText());
					}
				}
			}});
	}
	
	private static void snmpWalk(Snmp snmp, CommunityTarget target) {
		TableUtils utils = new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK));//GETNEXT or GETBULK
		utils.setMaxNumRowsPerPDU(5);	//only for GETBULK, set max-repetitions, default is 10
		OID[] columnOids = new OID[] {
				new OID("1.3.6.1.2.1.1.9.1.2"),	//sysORID
				new OID("1.3.6.1.2.1.1.9.1.3"),	//sysORDescr
				new OID("1.3.6.1.2.1.1.9.1.5")	//wrong OID, expect an null in in VariableBinding array
		};
		// If not null, all returned rows have an index in a range (lowerBoundIndex, upperBoundIndex]
		List l = utils.getTable(target, columnOids, new OID("3"), new OID("10"));
		for (TableEvent e : l) {
			System.out.println(e);
		}
	}
}

你可能感兴趣的:(SNMP,Java)