我想在Java中找到一種方法來以編程方式啓用/禁用通過SNMP的網絡交換機上的端口。我嘗試過使用SNMP4J,但無法在郵件列表上獲得如何使用它的幫助。只要它完成了工作,我並不太在意使用了什麼庫(開源與商業)。
我嘗試使用的交換機是Cisco 3750交換機。
問候, 詹姆斯
我想在Java中找到一種方法來以編程方式啓用/禁用通過SNMP的網絡交換機上的端口。我嘗試過使用SNMP4J,但無法在郵件列表上獲得如何使用它的幫助。只要它完成了工作,我並不太在意使用了什麼庫(開源與商業)。
我嘗試使用的交換機是Cisco 3750交換機。
問候, 詹姆斯
您可以使用下面簡單的代碼使用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();
}
}
我已經與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;
}
可能將需要知道開關MANUF。和型號 – 2010-09-02 18:23:16
請參閱http://serverfault.com/questions/177856/possible-to-configure-cisco-switch-ios-via-snmp – 2010-09-08 18:29:02