2 zh02054 zh02054 于 2014.01.16 14:41 提问

snmp4j向通信目标Target发送管理命令,相应的response为null是怎么回事?

本人刚接触snmp,用snmp4j获取并监控路由器外网口ip地址,路由器上已用厂商的手册命令配置了trap,本人拟在管理站用路由器发送来的trap,监控其外网口ip地址。问题是在管理站向被管理的路由器发送管理命令后,相应的response为null,不知是怎么回事,求熟悉snmp4j的大虾指导?该部代码如下:

public void processPdu(CommandResponderEvent respEvnt) {
    //获取trap端地址:Address
    Address trapAddress = respEvnt.getPeerAddress();

    if (respEvnt != null || respEvnt.getPDU() !=null) {
        PDU responsePdu = respEvnt.getPDU();
        System.out.println("responsePdu="+responsePdu);
    }       
    /**
     * 利用被管理设备地址,创建通信目标
     */
    CommunityTarget gateWayTarget = new CommunityTarget();
    Address address = GenericAddress.parse(trapAddress.toString());
    gateWayTarget.setAddress(address);
    gateWayTarget.setCommunity(new OctetString("public"));  
    gateWayTarget.setRetries(2); 
    gateWayTarget.setTimeout(5*60); 
    gateWayTarget.setVersion(SnmpConstants.version2c);
    /**
     * 生成协议数据单元
     */
    PDU requestPDU = new PDU();
    requestPDU.add(new VariableBinding(new OID("1.3.6.1.2.1.4.21.1.7")));//ipRouteNextHop
    requestPDU.setType(PDU.GETNEXT);//设置getnext命令
    String gatewayIpString = null;
    String getwayIp = null;
    try {
        //向被管理设备(通信目标)发送命令
         **ResponseEvent response = snmp.send(requestPDU, gateWayTarget);** 
         **PDU responsePDU = response.getResponse();** 
        if(responsePDU != null){
            VariableBinding getIp=responsePDU.get(0);
            gatewayIpString=getIp.toString();
        }
        getwayIp = gatewayIpString.substring(31);
        System.out.println("getwayIp=" + getwayIp);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

通信的完整代码如下:

public class GataWayIp2 implements CommandResponder{
private MultiThreadedMessageDispatcher dispatcher;
private Snmp snmp = null;
private Address listenAddress;
private ThreadPool threadPool;

public GataWayIp2() {
    // BasicConfigurator.configure();
}

/**
 * 根据管理机的地址建立UDP连接,即建立SNMP Seesion
 * @throws UnknownHostException
 * @throws IOException
 */
private void init() throws UnknownHostException, IOException {
    threadPool = ThreadPool.create("Trap", 2);
    dispatcher = new MultiThreadedMessageDispatcher(threadPool,
            new MessageDispatcherImpl());
    listenAddress = GenericAddress.parse(System.getProperty(
            "snmp4j.listenAddress", "udp:192.168.2.222/162")); // 本地IP与监听端口
    TransportMapping transport;
    // 对TCP与UDP协议进行处理
    if (listenAddress instanceof UdpAddress) {
        transport = new DefaultUdpTransportMapping(
                (UdpAddress) listenAddress);
    } else {
        transport = new DefaultTcpTransportMapping(
                (TcpAddress) listenAddress);
    }

    snmp = new Snmp(dispatcher, transport);
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3());
    USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3
            .createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(usm);

    snmp.listen();
}


public void run() {
    try {
        init();
        snmp.addCommandResponder(this);
        System.out.println("开始监听Trap信息!");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
/**
 * 实现CommandResponder的processPdu方法, 用于处理传入的请求、PDU等信息
 * 当接收到trap时,会自动进入这个方法
 * 
 * @param respEvnt
 */ 
public void processPdu(CommandResponderEvent respEvnt) {
    System.out.println("run here...");
    //获取trap端地址:Address
    Address trapAddress = respEvnt.getPeerAddress();
    if (respEvnt != null || respEvnt.getPDU() !=null) {
        PDU responsePdu = respEvnt.getPDU();
        System.out.println("responsePdu="+responsePdu);
    }       
    /**
     * 利用被管理设备地址,创建通信目标
     */
    CommunityTarget gateWayTarget = new CommunityTarget();
    Address address = GenericAddress.parse(trapAddress.toString());
    gateWayTarget.setAddress(address);
    gateWayTarget.setCommunity(new OctetString("public"));  
    gateWayTarget.setRetries(2); 
    gateWayTarget.setTimeout(5*60); 
    gateWayTarget.setVersion(SnmpConstants.version2c);
    /**
     * 生成协议数据单元
     */
    PDU requestPDU = new PDU();
    requestPDU.add(new VariableBinding(new OID("1.3.6.1.2.1.4.21.1.7")));//ipRouteNextHop
    requestPDU.setType(PDU.GETNEXT);//设置getnext命令
    String gatewayIpString = null;
    String getwayIp = null;
    try {
        //向被管理设备(通信目标)发送命令
        ResponseEvent response = snmp.send(requestPDU, gateWayTarget);
        PDU responsePDU = response.getResponse();
        if(responsePDU != null){
            VariableBinding getIp=responsePDU.get(0);
            gatewayIpString=getIp.toString();
        }
        getwayIp = gatewayIpString.substring(31);
        System.out.println("getwayIp=" + getwayIp);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

3个回答

oyljerry
oyljerry   Ds   Rxr 2015.01.02 19:53

对应路由设备是否支持你的请求,get请求的oid是否正确

lyyybz
lyyybz   Rxr 2016.12.16 14:51

同问啊,哥们你知道答案了么?

qq_29494043
qq_29494043   2018.03.16 09:20

这么多年了,刚好遇到同样的问题。首先1L说的对。注意一下version版本、IP地址、OID等等是否正确。MIB上能获取到OID就证明你的IP地址、端口号、版本号等信息是对的

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!