Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.*;
- import com.adventnet.snmp.snmp2.*;
- import com.adventnet.snmp.snmp2.usm.USMUserEntry;
- import com.adventnet.snmp.snmp2.usm.USMUtils;
- public class TcpConMgr {
- public static void main(String args[]) {
- String remoteHost = "130.89.144.44";
- String OID = ".1.3.6.1.2.1.1.9";
- if (args.length == 2) {
- remoteHost = args[0];
- OID = args[1];
- }
- int oidLength = OID.length();
- SnmpAPI api;
- api = new SnmpAPI();
- api.start();
- api.setDebug(false);
- String imm = "imm";
- SnmpEngineEntry snmpEntry = new SnmpEngineEntry(remoteHost, 2161);
- byte[] engineID = snmpEntry.getEngineID();
- byte[] userName = imm.getBytes();
- USMUserEntry usmEntry = new USMUserEntry(userName, engineID);
- usmEntry.setSecurityLevel(Snmp3Message.AUTH_PRIV);
- byte[] authPassword = "s1mpl31snt1t".getBytes();
- usmEntry.setAuthPassword(authPassword);
- usmEntry.setAuthProtocol(USMUserEntry.SHA_AUTH);
- byte[] authKey = USMUtils.password_to_key(usmEntry.getAuthProtocol(), authPassword, authPassword.length, engineID);
- usmEntry.setAuthKey(authKey);
- SnmpSession session = new SnmpSession(api);
- session.setRemotePort(2161);
- session.setCommunity(imm);
- session.setUserName(imm.getBytes());
- try {
- session.open();
- } catch (SnmpException e) {
- System.err.println("Error opening socket: " + e);
- }
- session.setPeername(remoteHost);
- SnmpPDU pdu = new SnmpPDU();
- pdu.setCommand(api.GETNEXT_REQ_MSG);
- pdu.addNull(new SnmpOID(OID));
- String checkId = OID;
- do {
- try {
- pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG);
- pdu = session.syncSend(pdu);
- } catch (SnmpException e) {
- System.err.println("Error sending SNMP request: " + e);
- }
- SnmpVarBind snmpVar = pdu.getVariableBinding(0);
- String objId = snmpVar.getObjectID().toString();
- checkId = objId.substring(0, oidLength);
- objId = objId.substring(oidLength + 1, objId.length());
- if (OID.equals(checkId)) {
- System.out.println(pdu.printVarBinds());
- } else
- break;
- } while (OID.equals(checkId));
- session.close();
- api.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement