Participant
April 1, 2021
Question
SNMP query with cfx_SNMP.java
- April 1, 2021
- 2 replies
- 348 views
Good afternoon. I am currently using cfx_SNMP.java to enable SNMP queries/etc from my Coldfusion Webpages. This code was built back in 2009. I believe it is using SNMP v1. I am introducing new devices into my system that will not respond with SNMP v1 so I am needing to incorporate this code into using v2c. I am not sure how to do this and was wondering if I could get somme pointers. I ahve attached my cfc_SNMP.java code file to this request. Thank you.
// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov Date: 5/13/2010 4:59:22 PM
// Home Page: http://members.fortunecity.com/neshkov/dj.html http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: cfx_SNMP.java
import com.allaire.cfx.*;
import java.net.InetAddress;
import java.util.StringTokenizer;
import snmp.*;
public class cfx_SNMP
implements CustomTag
{
public cfx_SNMP()
{
}
public void processRequest(Request request, Response response)
throws Exception
{
try
{
if(!request.attributeExists("COMMUNITY") || !request.attributeExists("HOST") || !request.attributeExists("OID") || !request.attributeExists("R_QRESULTS"))
throw new Exception("Missing attribute(s) (COMMUNITY,HOST,R_QRESULTS AND OID are required attributes for this tag)");
String s = request.getAttribute("R_QRESULTS");
String s1 = request.getAttribute("COMMUNITY");
String s3 = request.getAttribute("HOST");
String s4 = request.getAttribute("OID");
String s5 = "";
int i = 30;
if(request.attributeExists("TIMEOUT"))
{
String s6 = request.getAttribute("TIMEOUT");
i = Integer.parseInt(s6);
}
i *= 1000;
String as[] = {
"OID", "Value", "Type", "Hex"
};
Query query = response.addQuery(s, as);
int j = 1;
byte byte0 = 2;
byte byte1 = 3;
byte byte2 = 4;
int k = 0;
InetAddress inetaddress = InetAddress.getByName(s3);
SNMPv1CommunicationInterface snmpv1communicationinterface = new SNMPv1CommunicationInterface(k, inetaddress, s1);
snmpv1communicationinterface.setSocketTimeout(i);
String s7 = "";
String s9 = s4;
String s11 = s9;
String s13 = s4;
String as1[] = readTokens(s13, ",");
String s14 = "";
label0:
for(int l = 0; l < as1.length; l++)
{
String s8 = "";
String s10 = as1[l];
String s12 = s10;
try
{
do
{
if(s10.equals(s8) || !s10.startsWith(s12) && !s10.equals(s12))
continue label0;
s8 = s10;
SNMPVarBindList snmpvarbindlist = snmpv1communicationinterface.getNextMIBEntry(s8);
SNMPSequence snmpsequence = (SNMPSequence)snmpvarbindlist.getSNMPObjectAt(0);
SNMPObjectIdentifier snmpobjectidentifier = (SNMPObjectIdentifier)snmpsequence.getSNMPObjectAt(0);
SNMPObject snmpobject = snmpsequence.getSNMPObjectAt(1);
s10 = snmpobjectidentifier.toString();
if(!s10.startsWith(s12) && !s10.equals(s12))
break;
String s15 = snmpobject.getClass().getName();
s14 = s10;
if(s15.equals("snmp.SNMPOctetString"))
{
String s17 = snmpobject.toString();
int i1 = s17.indexOf('\0');
if(i1 >= 0)
s17 = s17.substring(0, i1);
int i2 = query.addRow();
query.setData(i2, j, snmpobjectidentifier.toString());
query.setData(i2, byte0, s17);
query.setData(i2, byte1, s15.substring(9, s15.length()));
query.setData(i2, byte2, ((SNMPOctetString)snmpobject).toHexString());
} else
{
String s18 = snmpobject.toString();
int j1 = s18.indexOf('\0');
if(j1 >= 0)
s18 = s18.substring(0, j1);
int j2 = query.addRow();
query.setData(j2, j, snmpobjectidentifier.toString());
query.setData(j2, byte0, s18);
query.setData(j2, byte1, s15.substring(9, s15.length()));
query.setData(j2, byte2, "");
}
} while(true);
SNMPVarBindList snmpvarbindlist1 = snmpv1communicationinterface.getMIBEntry(s8);
SNMPSequence snmpsequence1 = (SNMPSequence)snmpvarbindlist1.getSNMPObjectAt(0);
SNMPObjectIdentifier snmpobjectidentifier1 = (SNMPObjectIdentifier)snmpsequence1.getSNMPObjectAt(0);
SNMPObject snmpobject1 = snmpsequence1.getSNMPObjectAt(1);
s10 = snmpobjectidentifier1.toString();
if(s10.equals(s14))
continue;
String s16 = snmpobject1.getClass().getName();
if(s16.equals("snmp.SNMPOctetString"))
{
String s19 = snmpobject1.toString();
int k1 = s19.indexOf('\0');
if(k1 >= 0)
s19 = s19.substring(0, k1);
int k2 = query.addRow();
query.setData(k2, j, snmpobjectidentifier1.toString());
query.setData(k2, byte0, s19);
query.setData(k2, byte1, s16.substring(9, s16.length()));
query.setData(k2, byte2, ((SNMPOctetString)snmpobject1).toHexString());
continue;
}
String s20 = snmpobject1.toString();
int l1 = s20.indexOf('\0');
if(l1 >= 0)
s20 = s20.substring(0, l1);
int l2 = query.addRow();
query.setData(l2, j, snmpobjectidentifier1.toString());
query.setData(l2, byte0, s20);
query.setData(l2, byte1, s16.substring(9, s16.length()));
query.setData(l2, byte2, "");
}
catch(Exception exception1) { }
}
snmpv1communicationinterface.closeConnection();
}
catch(Exception exception)
{
if(exception.toString().indexOf("snmp") != -1 || exception.toString().indexOf("timeout") != -1)
{
if(request.attributeExists("R_SNMP_ERRORS"))
{
String s2 = request.getAttribute("R_SNMP_ERRORS");
response.setVariable(s2, exception.toString().substring(exception.toString().indexOf(": ") + 1, exception.toString().length()));
}
} else
{
throw new Exception(exception);
}
}
}
public static String[] readTokens(String s, String s1)
{
StringTokenizer stringtokenizer = new StringTokenizer(s, s1);
int i = stringtokenizer.countTokens();
String as[] = new String[i];
for(int j = 0; j < i; j++)
as[j] = stringtokenizer.nextToken();
return as;
}
}