簡體   English   中英

以編程方式啟用/禁用網絡交換機上的端口

[英]Programmatically Enable/Disable Port on Network Switch

問候!

我正在嘗試找到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堆棧很幸運。

對於簡單的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;
}

您可以嘗試閱讀文檔 ...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM