首页>代码>java SNMP简单测试实例>/SNMP/src/com/voicevodes/snmp/demo/SnmpUtil_2.java
package com.voicevodes.snmp.demo;

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

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
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;

public class SnmpUtil_2 {
	private Snmp snmp = null;
	
	private Address targetAddress = null;

	private TransportMapping<UdpAddress> transport = null;

	public void initComm() throws IOException {
	          
	           // 设置Agent方的IP和端口
	           targetAddress = GenericAddress.parse("udp:127.0.0.1/161");
	           
	           //设置trap的方法和IP端口
	           transport = new DefaultUdpTransportMapping(new UdpAddress("192.168.6.121/162"));
	           
	           //TransportMapping transport = new DefaultUdpTransportMapping();
	           snmp = new Snmp(transport);
	           
	           CommandResponder trapRec = new CommandResponder() {
				
				public synchronized void processPdu(CommandResponderEvent e) {
					// TODO Auto-generated method stub
					//接收trap
					PDU command = e.getPDU();
					if (command != null) {
						System.out.println(command.toString());
					}
				}
			};
	           
	           snmp.addCommandResponder(trapRec);
	           
	           
	           transport.listen();
	    }

	public ResponseEvent sendPDU(PDU pdu) throws IOException {
		// 设置 target
		CommunityTarget target = new CommunityTarget();
		target.setCommunity(new OctetString("public"));
		target.setAddress(targetAddress);
		// 通信不成功时的重试次数
		target.setRetries(2);
		// 超时时间
		target.setTimeout(1500);
		target.setVersion(SnmpConstants.version1);
		// 向Agent发送PDU,并返回Response
		return snmp.send(pdu, target);
	}

	public void setPDU() throws IOException {
		// set PDU
		PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5,
				0 }), new OctetString("SNMPTEST")));
		pdu.setType(PDU.SET);
		sendPDU(pdu);
	}

	public void getPDU() throws IOException {
		// get PDU
		PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5,
				0 })));
		pdu.setType(PDU.GET);
		readResponse(sendPDU(pdu));
	}

	public void readResponse(ResponseEvent respEvnt) {
		// 解析Response
		if (respEvnt != null && respEvnt.getResponse() != null) {
			Vector<VariableBinding> recVBs = (Vector<VariableBinding>) respEvnt
					.getResponse().getVariableBindings();
			for (int i = 0; i < recVBs.size(); i++) {
				VariableBinding recVB = recVBs.elementAt(i);
				System.out
						.println(recVB.getOid() + " : " + recVB.getVariable());
			}
		}
	}
	
	public synchronized  void listen(){
		System.out.println("Waiting for traps..");
		try{
			this.wait();
		}catch (Exception ex) {
			System.out.println("Interrupted while waiting for traps:"+ex);
			System.exit(-1);
		}
	}

	public static void main(String[] args) {
		try {
			SnmpUtil_2 util2 = new SnmpUtil_2();
			util2.initComm();
			util2.listen();
			util2.setPDU();
			util2.getPDU();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
最近下载更多
honglonghua  LV1 2023年2月9日
yzh_falcon  LV3 2022年10月18日
qwaszx123123  LV1 2022年9月17日
gaotieyou  LV5 2022年4月19日
xingbing  LV9 2022年2月9日
余长城  LV4 2022年1月29日
巨魔  LV1 2021年9月6日
newbag  LV9 2021年8月9日
wc2021  LV1 2021年7月4日
lz88888  LV11 2021年6月4日
最近浏览更多
kenhomeliu  LV29 4月30日
yzh_falcon  LV3 2022年10月18日
zw050256  LV7 2022年9月30日
qwaszx123123  LV1 2022年9月17日
GZW012345  LV8 2022年5月21日
13165798872  LV11 2022年5月17日
zlfsgg  LV3 2022年4月26日
gaotieyou  LV5 2022年4月19日
xingbing  LV9 2022年2月9日
余长城  LV4 2022年1月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友