2014-01-07 100 views
2

我想添加一個客戶端到我的snmp協議程序。我將此代碼爲我的主要方法:在SNMP協議NullPointerException

public static void main(String[] args) throws IOException{ 
     SimpleSnmpClient client = new SimpleSnmpClient("udp:10.0.0.50/161"); 
     String sysDescr = client.getAsString(new OID(".1.3.6.1.2.1.1.1.0")); 
     System.out.println(sysDescr); 
    } 

,我從中學到的網站說,輸出應該是關於某種大約運行該程序的設備信息。

我SimpleSnmpClient代碼:

public class SimpleSnmpClient { 

    private String address; 

    private Snmp snmp; 


    public static void main(String[] args) throws IOException{ 
     SimpleSnmpClient client = new SimpleSnmpClient("udp:10.0.0.50/161"); 
     String sysDescr = client.getAsString(new OID(".1.3.6.1.2.1.1.1.0")); 
     System.out.println(sysDescr); 
    } 

    public SimpleSnmpClient(String address) { 
     super(); 
     this.address = address; 
     try { 
      start(); 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    // Since snmp4j relies on asynch req/resp we need a listener 
    // for responses which should be closed 
    public void stop() throws IOException { 
     snmp.close(); 
    } 

    private void start() throws IOException { 
     TransportMapping transport = new DefaultUdpTransportMapping(); 
     snmp = new Snmp(transport); 
     // Do not forget this line! 
     transport.listen(); 
    } 

    public String getAsString(OID oid) throws IOException { 
     ResponseEvent event = get(new OID[]{oid}); 
     return event.getResponse().get(0).getVariable().toString(); 
    } 


    public void getAsString(OID oids,ResponseListener listener) { 
     try { 
      snmp.send(getPDU(new OID[]{oids}), getTarget(),null, listener); 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 


    private PDU getPDU(OID oids[]) { 
     PDU pdu = new PDU(); 
     for (OID oid : oids) { 
      pdu.add(new VariableBinding(oid)); 
     } 

     pdu.setType(PDU.GET); 
     return pdu; 
    } 

    public ResponseEvent get(OID oids[]) throws IOException { 
     ResponseEvent event = snmp.send(getPDU(oids), getTarget(), null); 
     if(event != null) { 
      return event; 
     } 
     throw new RuntimeException("GET timed out"); 
    } 

    private Target getTarget() { 
     Address targetAddress = GenericAddress.parse(address); 
     CommunityTarget target = new CommunityTarget(); 
     target.setCommunity(new OctetString("public")); 
     target.setAddress(targetAddress); 
     target.setRetries(2); 
     target.setTimeout(1500); 
     target.setVersion(SnmpConstants.version2c); 
     return target; 
    } 

    /** 
    * Normally this would return domain objects or something else than this... 
    */ 
    public List<List<String>> getTableAsStrings(OID[] oids) { 
     TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory()); 

     @SuppressWarnings("unchecked") 
      List<TableEvent> events = tUtils.getTable(getTarget(), oids, null, null); 

     List<List<String>> list = new ArrayList<List<String>>(); 
     for (TableEvent event : events) { 
      if(event.isError()) { 
       throw new RuntimeException(event.getErrorMessage()); 
      } 
      List<String> strList = new ArrayList<String>(); 
      list.add(strList); 
      for(VariableBinding vb: event.getColumns()) { 
       strList.add(vb.getVariable().toString()); 
      } 
     } 
     return list; 
    } 

    public static String extractSingleString(ResponseEvent event) { 
     return event.getResponse().get(0).getVariable().toString(); 
    } 
} 

但我收到NullPointerException異常:

Exception in thread "main" java.lang.NullPointerException 
    at org.bihe.SimpleSnmpClient.getAsString(SimpleSnmpClient.java:70) 
    at org.bihe.SimpleSnmpClient.main(SimpleSnmpClient.java:41) 

它指的是行:

return event.getResponse().get(0).getVariable().toString(); 

,我不知道爲什麼這是怎麼回事?任何人都可以幫我解決這個問題嗎?

回答

3

如果你超時,你可以得到一個空響應。從ResponseEvent文檔:「a PDU instance if a response has been received. If the request timed out then null will be returned.

也許你的服務器沒有啓動或不可用。嘗試訪問它與一些已知的應用程序,如snmpwalk只是爲了區分您的代碼與網絡問題。

+0

你能不能幫我,我怎麼能使用snmpwalk的?如果是我可以找到的應用程序?我使用的是Windows 8,我安裝了net-snmp,並且還從windows功能中添加了snmp。他們是否依賴這個問題? –

+0

看起來snmpwalk可能帶有net-snmp。看看提供的例子。 http://www.net-snmp.org/docs/man/snmpwalk.html –

+0

如何在Windows中運行此命令? %snmpwalk -mALL -v1 -cpublic snmp_agent_Ip_address系統? –

1

您嘗試獲取響應的設備是否具有社區字符串集?社區就像一個密碼,並被設置在一個設備上,以防止人們在未經授權的情況下從該設備獲取信息。如果社區字符串不正確,您將不會收到回覆。

0

確保您爲SNMP服務代理添加了相同的社區字符串。

窗戶... 開放服務 - >右鍵單擊SNMP - >安全選項卡下 - >添加團體字符串