2010-09-02 26 views
0

問候!以編程方式啓用/禁用網絡交換機上的端口

我想在Java中找到一種方法來以編程方式啓用/禁用通過SNMP的網絡交換機上的端口。我嘗試過使用SNMP4J,但無法在郵件列表上獲得如何使用它的幫助。只要它完成了工作,我並不太在意使用了什麼庫(開源與商業)。

我嘗試使用的交換機是Cisco 3750交換機。

問候, 詹姆斯

+0

可能將需要知道開關MANUF。和型號 – 2010-09-02 18:23:16

+0

請參閱http://serverfault.com/questions/177856/possible-to-configure-cisco-switch-ios-via-snmp – 2010-09-08 18:29:02

回答

2

您可以使用下面簡單的代碼使用SNMP4J啓用/禁用交換機端口。

它使端口1和禁用端口6.

package com.mobinet.snmp; 

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.Integer32; 
import org.snmp4j.smi.OID; 
import org.snmp4j.smi.OctetString; 
import org.snmp4j.smi.VariableBinding; 
import org.snmp4j.transport.DefaultTcpTransportMapping; 

/** 
* 
* @author batbayar 
*/ 
public class SnmpTest { 
    private String address = "192.168.1.254/161"; // switch address and snmp port 
    private String writeCommunity = "myCommunityWrite"; // write community name 

    private Snmp snmp; 
    private CommunityTarget target; 

    public SnmpTest() { 
     try { 
      TransportMapping transport = new DefaultTcpTransportMapping(); 
      snmp = new Snmp(transport); 

      Address targetAddress = GenericAddress.parse(address); 
      target = new CommunityTarget(); 
      target.setCommunity(new OctetString(writeCommunity)); 
      target.setAddress(targetAddress); 
      target.setRetries(2); 
      target.setTimeout(1500); 
      target.setVersion(SnmpConstants.version2c); 

      PDU command = new PDU(); 
      command.setType(PDU.SET); 
      command.add(new VariableBinding(new OID("1.3.6.1.2.1.2.2.1.7.1"), new Integer32(2))); // port 1 down 
      command.add(new VariableBinding(new OID("1.3.6.1.2.1.2.2.1.7.6"), new Integer32(1))); // port 6 up 
      ResponseEvent response = snmp.send(command, target); 
      System.out.println("response: " + response); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     SnmpTest test = new SnmpTest(); 
    } 
} 
+0

這使得端口1停機並開啓端口6,對吧? – Sameet 2011-02-22 17:01:22

+0

沒有。 1 - 上,2 - 下。我測試過了。 – digz6666 2011-03-01 02:10:23

+0

仍然是6和1相反。 2使端口1下降,1使端口6上升。如果我錯了,請回復;) – Burkhard 2012-02-26 17:11:29

0

你可以嘗試閱讀docs ...

A(幾乎)完整的示例爲 SNMP4J API使用的是控制檯工具。 它可以在 org.snmp4j.tools.console.SnmpRequest 類中找到。

+0

我做過。我無法讓它與我的用例一起工作,並且對列表serv沒有太多幫助。 – James 2010-09-02 18:27:21

1

我已經與Westhawk Java SNMP stack祝你好運。

對於一個簡單的SNMP設置,語法將是這個樣子:

public static boolean setOid(String hostAddress, int portNumber, String communityName, String oidToSet, String valueToSet) { 
    SnmpContextPool context = null; 
    try { 

     context = new SnmpContextPool(hostAddress, portNumber, SnmpContextFace.STANDARD_SOCKET); 
     context.setCommunity(communityName); 

     SetPdu oneSetPdu = new SetPdu(context); 
     AsnObject obj = new AsnOctets(valueToSet); // use AsnInteger here if you are setting an integer value 
     oneSetPdu.addOid(oidToSet, obj); 

     return oneSetPdu.send(); 

    } catch (Exception e) { 
     //TODO: Handle exceptions properly 
     e.printStackTrace(); 
    } finally { 
     if (context != null) { 
      context.destroy(); 
     } 
    } 
    return false; 
} 
相關問題