public static void GetRequest(string[] argv) { int commlength, miblength, datatype, datalength, datastart; int uptime = 0; string output; SNMP conn = new SNMP(); byte[] response = new byte[1024]; System.Web.HttpContext.Current.Response.Write("Device SNMP information:"); // Send sysName SNMP request response = conn.get("get", argv[0], argv[1], ".1.3.6.1.2.1.1.4.0"); if (response[0] == 0xff) { System.Web.HttpContext.Current.Response.Write("No response from " + argv[0]); return; } // If response, get the community name and MIB lengths commlength = Convert.ToInt16(response[6]); miblength = Convert.ToInt16(response[23 + commlength]); // Extract the MIB data from the SNMP response datatype = Convert.ToInt16(response[24 + commlength + miblength]); datalength = Convert.ToInt16(response[25 + commlength + miblength]); datastart = 26 + commlength + miblength; output = Encoding.ASCII.GetString(response, datastart, datalength); System.Web.HttpContext.Current.Response.Write(" - sysName : " + output); // Send a SysUptime SNMP request response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.3.0"); if (response[0] == 0xff) { System.Web.HttpContext.Current.Response.Write("No response from " + argv[0]); return; } // Get the community and MIB lengths of the response commlength = Convert.ToInt16(response[6]); miblength = Convert.ToInt16(response[23 + commlength]); // Extract the MIB data from the SNMp response datatype = Convert.ToInt16(response[24 + commlength + miblength]); datalength = Convert.ToInt16(response[25 + commlength + miblength]); datastart = 26 + commlength + miblength; // The sysUptime value may by a multi-byte integer // Each byte read must be shifted to the higher byte order while (datalength > 0) { uptime = (uptime << 8) + response[datastart++]; datalength--; } System.Web.HttpContext.Current.Response.Write(" - sysUptime : " + uptime); }
public static void Main(string[] argv) { int commlength, miblength, datastart, datalength; string nextmib, value; SNMP conn = new SNMP(); string mib = "1.3.6.1.2.1.17.4.3.1.1"; int orgmiblength = mib.Length; byte[] response = new byte[1024]; nextmib = mib; while (true) { response = conn.get("getnext", argv[0], argv[1], nextmib); commlength = Convert.ToInt16(response[6]); miblength = Convert.ToInt16(response[23 + commlength]); datalength = Convert.ToInt16(response[25 + commlength + miblength]); datastart = 26 + commlength + miblength; value = BitConverter.ToString(response, datastart, datalength); nextmib = conn.getnextMIB(response); if (!(nextmib.Substring(0, orgmiblength) == mib)) { break; } Console.WriteLine("{0} = {1}", nextmib, value); } }
public static void Main(string[] argv) { int commlength, miblength, datatype, datalength, datastart; int uptime = 0; string output; SNMP conn = new SNMP(); byte[] response = new byte[1024]; Console.WriteLine("Device SNMP information:"); // Send sysName SNMP request response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.5.0"); if (response[0] == 0xff) { Console.WriteLine("No response from {0}", argv[0]); return; } // If response, get the community name and MIB lengths commlength = Convert.ToInt16(response[6]); miblength = Convert.ToInt16(response[23 + commlength]); // Extract the MIB data from the SNMP response datatype = Convert.ToInt16(response[24 + commlength + miblength]); datalength = Convert.ToInt16(response[25 + commlength + miblength]); datastart = 26 + commlength + miblength; output = Encoding.ASCII.GetString(response, datastart, datalength); Console.WriteLine(" sysName - Datatype: {0}, Value: {1}", datatype, output); // Send a sysLocation SNMP request response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.6.0"); if (response[0] == 0xff) { Console.WriteLine("No response from {0}", argv[0]); return; } // If response, get the community name and MIB lengths commlength = Convert.ToInt16(response[6]); miblength = Convert.ToInt16(response[23 + commlength]); // Extract the MIB data from the SNMP response datatype = Convert.ToInt16(response[24 + commlength + miblength]); datalength = Convert.ToInt16(response[25 + commlength + miblength]); datastart = 26 + commlength + miblength; output = Encoding.ASCII.GetString(response, datastart, datalength); Console.WriteLine(" sysLocation - Datatype: {0}, Value: {1}", datatype, output); // Send a sysContact SNMP request response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.4.0"); if (response[0] == 0xff) { Console.WriteLine("No response from {0}", argv[0]); return; } // Get the community and MIB lengths commlength = Convert.ToInt16(response[6]); miblength = Convert.ToInt16(response[23 + commlength]); // Extract the MIB data from the SNMP response datatype = Convert.ToInt16(response[24 + commlength + miblength]); datalength = Convert.ToInt16(response[25 + commlength + miblength]); datastart = 26 + commlength + miblength; output = Encoding.ASCII.GetString(response, datastart, datalength); Console.WriteLine(" sysContact - Datatype: {0}, Value: {1}", datatype, output); // Send a SysUptime SNMP request response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.3.0"); if (response[0] == 0xff) { Console.WriteLine("No response from {0}", argv[0]); return; } // Get the community and MIB lengths of the response commlength = Convert.ToInt16(response[6]); miblength = Convert.ToInt16(response[23 + commlength]); // Extract the MIB data from the SNMp response datatype = Convert.ToInt16(response[24 + commlength + miblength]); datalength = Convert.ToInt16(response[25 + commlength + miblength]); datastart = 26 + commlength + miblength; // The sysUptime value may by a multi-byte integer // Each byte read must be shifted to the higher byte order while (datalength > 0) { uptime = (uptime << 8) + response[datastart++]; datalength--; } Console.WriteLine(" sysUptime - Datatype: {0}, Value: {1}", datatype, uptime); }