Skip to content

Commit

Permalink
Adding an SnmpManager as part of resolution of issue #6.
Browse files Browse the repository at this point in the history
  • Loading branch information
niau committed Mar 26, 2016
1 parent e25bb0c commit fb6d82e
Show file tree
Hide file tree
Showing 8 changed files with 1,306 additions and 121 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* SnmpTcpV1Manager.java
*
* This work is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This work is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Copyright (c) 2010-2016 iTransformers Labs. All rights reserved.
*/

package net.itransformers.snmp2xml4j.snmptoolkit;

import net.itransformers.snmp2xml4j.snmptoolkit.transport.TcpTransportMappingFactory;
import net.percederberg.mibble.MibLoader;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Target;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;

import java.io.IOException;

/**
* Created by niau on 3/24/16.
*/
public class SnmpTcpV1Manager extends SnmpManager {

protected String snmpCommunity;
protected TcpAddress tcpAddress;



public SnmpTcpV1Manager(MibLoader loader, String ipAddress, String snmpCommunity, int retries, int timeout, int maxSizeRequestPDU, int destinationPort) throws IOException {
super(loader, retries, timeout, maxSizeRequestPDU,destinationPort,new TcpTransportMappingFactory(),new TcpAddress("0.0.0.0/0"));
this.snmpCommunity = snmpCommunity;

this.tcpAddress = new TcpAddress(ipAddress+"/"+destinationPort);

}


@Override
protected void doInit() throws IOException {

}

protected Target getTarget() {

CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(snmpCommunity));
target.setAddress(this.tcpAddress);
target.setRetries(retries);
target.setTimeout(timeout);
target.setVersion(SnmpConstants.version1);

return target;
}
@Override
protected PDU createPDU() {
return new PDU();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* SnmpTcpV2Manager.java
*
* This work is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This work is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Copyright (c) 2010-2016 iTransformers Labs. All rights reserved.
*/

package net.itransformers.snmp2xml4j.snmptoolkit;

import net.itransformers.snmp2xml4j.snmptoolkit.transport.TcpTransportMappingFactory;
import net.percederberg.mibble.MibLoader;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Target;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;

import java.io.IOException;

/**
* Created by niau on 3/24/16.
*/
public class SnmpTcpV2Manager extends SnmpManager {

protected String snmpCommunity;
protected TcpAddress tcpAddress;



public SnmpTcpV2Manager(MibLoader loader, String ipAddress, String snmpCommunity,int retries, int timeout, int maxSizeRequestPDU, int destinationPort) throws IOException {
super(loader, retries, timeout, maxSizeRequestPDU,destinationPort,new TcpTransportMappingFactory(),new TcpAddress("0.0.0.0/0"));
this.snmpCommunity = snmpCommunity;

this.tcpAddress = new TcpAddress(ipAddress+"/"+destinationPort);

}


@Override
protected void doInit() throws IOException {

}

protected Target getTarget() {

CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(snmpCommunity));
target.setAddress(this.tcpAddress);
target.setRetries(retries);
target.setTimeout(timeout);
target.setVersion(SnmpConstants.version2c);

return target;
}
@Override
protected PDU createPDU() {
return new PDU();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* SnmpTcpV3Manager.java
*
* This work is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This work is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Copyright (c) 2010-2016 iTransformers Labs. All rights reserved.
*/

package net.itransformers.snmp2xml4j.snmptoolkit;

import net.itransformers.snmp2xml4j.snmptoolkit.transport.TcpTransportMappingFactory;
import net.percederberg.mibble.MibLoader;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Target;
import org.snmp4j.UserTarget;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.*;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;

import java.io.IOException;

/**
* Created by niau on 3/24/16.
*/
public class SnmpTcpV3Manager extends SnmpManager {


protected String ver3Username;
protected String ver3AuthPasscode;
protected int ver3mode;

protected String authenticationProtocol;
protected String privacyProtocol;
protected String privacyProtocolPassShare;
protected TcpAddress tcpAddress;



public SnmpTcpV3Manager(MibLoader loader, String ipAddress, int ver3mode, String ver3Username, String ver3AuthPasscode, String authenticationProtocol, String privacyProtocol, String privacyProtocolPassShare, int retries, int timeout, int maxSizeRequestPDU, int destinationPort) throws IOException {
super(loader, retries, timeout, maxSizeRequestPDU,destinationPort,new TcpTransportMappingFactory(),new TcpAddress("0.0.0.0/0"));
this.ver3mode = ver3mode;
this.ver3Username = ver3Username;
this.ver3AuthPasscode = ver3AuthPasscode;
this.authenticationProtocol = authenticationProtocol;
this.privacyProtocol = privacyProtocol;
this.privacyProtocolPassShare = privacyProtocolPassShare;
this.tcpAddress = new TcpAddress(ipAddress+"/"+destinationPort);

}

protected Target getTarget() {

UserTarget target = new UserTarget();

target.setAddress(tcpAddress);

target.setVersion(SnmpConstants.version3); // SnmpConstants.version3
target.setRetries(retries);
target.setTimeout(timeout);
target.setSecurityLevel(ver3mode); // SecurityLevel.AUTH_NOPRIV
target.setSecurityName(new OctetString(ver3Username));

return target;

}

@Override
protected PDU createPDU() {
return new ScopedPDU();

}


@Override
protected void doInit() {



USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
OID authenticationProtocolOID = null;

if ("MD5".equals(this.authenticationProtocol)) {
authenticationProtocolOID = AuthMD5.ID;
} else if ("SHA".equals(authenticationProtocol)) {
authenticationProtocolOID = AuthSHA.ID;
}
OID privacyProtocolOID = null;

if ("DES".equals(privacyProtocol)) {
privacyProtocolOID = PrivDES.ID;
} else if ("3DES".equals(privacyProtocol)) {
privacyProtocolOID = Priv3DES.ID;
}


if (ver3mode == SecurityLevel.NOAUTH_NOPRIV) {
snmp.getUSM().addUser(new OctetString(ver3Username),
new UsmUser(new OctetString(ver3Username), null, null, null, null));

}
else if (ver3mode == SecurityLevel.AUTH_NOPRIV) {
snmp.getUSM().addUser(new OctetString(ver3Username),
new UsmUser(new OctetString(ver3Username), authenticationProtocolOID, new OctetString(ver3AuthPasscode), null, null));


} else {

snmp.getUSM().addUser(new OctetString(ver3Username),
new UsmUser(new OctetString(ver3Username), authenticationProtocolOID, new OctetString(ver3AuthPasscode), privacyProtocolOID, new OctetString(privacyProtocolPassShare)));

}



}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* SnmpUdpV1Manager.java
*
* This work is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This work is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Copyright (c) 2010-2016 iTransformers Labs. All rights reserved.
*/

package net.itransformers.snmp2xml4j.snmptoolkit;

import net.itransformers.snmp2xml4j.snmptoolkit.transport.UdpTransportMappingFactory;
import net.percederberg.mibble.MibLoader;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Target;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;

import java.io.IOException;

/**
* Created by niau on 3/24/16.
*/
public class SnmpUdpV1Manager extends SnmpManager {

protected String snmpCommunity;
protected UdpAddress udpAddress;



public SnmpUdpV1Manager(MibLoader loader, String ipAddress, String snmpCommunity, int retries, int timeout, int maxSizeRequestPDU, int destinationPort) throws IOException {
super(loader, retries, timeout, maxSizeRequestPDU,destinationPort,new UdpTransportMappingFactory(),new UdpAddress("0.0.0.0/0"));
this.snmpCommunity = snmpCommunity;
this.udpAddress = new UdpAddress(ipAddress+"/"+destinationPort);
}

@Override
protected void doInit() {

}

protected Target getTarget() {

CommunityTarget target = new CommunityTarget();

target.setCommunity(new OctetString(snmpCommunity));

target.setAddress(this.udpAddress);


target.setRetries(retries);

target.setTimeout(timeout);

target.setVersion(SnmpConstants.version1);

return target;
}
@Override
protected PDU createPDU() {
return new PDU();

}

}
Loading

0 comments on commit fb6d82e

Please sign in to comment.