views:

74

answers:

3

Greetings!

I am trying to find a way in Java to programmatically enable/disable a port on a network switch over SNMP. I tried using SNMP4J but couldn't get much help on the mailing list on how to use it. I'm not too concerned what library is used (open source vs commercial) as long as it gets the job done.

The switch I am trying to work with is a Cisco 3750 switch.

Regards, James

A: 

You could try reading the docs...

A (nearly) complete example for the SNMP4J API usage is the console tool. It can be found in the org.snmp4j.tools.console.SnmpRequest class.

Byron Whitlock
I did. I couldn't get it to work with my use case and there was not much help on the list serv.
James
+1  A: 

I have had good luck with the Westhawk Java SNMP stack.

For a simple SNMP set, the syntax will look something like this:

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;
}
James Van Huis
A: 

You can use following simple code to enable/disable switch port using snmp4j.

It enables port 1 and disables port 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 6 down
            command.add(new VariableBinding(new OID("1.3.6.1.2.1.2.2.1.7.6"), new Integer32(1))); // port 1 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();
    }
}
digz6666