Simple SNMP Query Tool - Open Source - C# (C-sharp)
Application Demo
// Author: Toomas Kaljus
// http://www.digigrupp.com
namespace DG
{
public class Application
{
private static string Title = "Simple SNMP Command Line Query Tool, Toomas Kaljus, January 2007";
private static string DefaultObjectID = "1.3.6.1.2.1.1"; // iso.org.dod.internet.mgmt.mib-2.system
public static string TitleForMibs = "Simple SNMP Command Line Query Tool";
public static string Url = "http://www.digigrupp.com/ssnmpq/";
private static string RFCUrl = "http://www.ietf.org/rfc/rfc{n}.txt";
private static int RFCFirstNumber = 1;
private static int RFCLastNumber = 5000;
private static int Timeout = 5;
public static void ShowHelp()
{
System.Console.WriteLine(Title);
System.Console.WriteLine();
System.Console.WriteLine("Command Line Switches:");
System.Console.WriteLine();
System.Console.WriteLine(" /h:<Host> Host name or IP address (default is localhost)");
System.Console.WriteLine(" /c:<Community> community name (default is public)");
System.Console.WriteLine(" /o:<ObjectID> object ID (default is subkeys from " + DefaultObjectID + ")");
System.Console.WriteLine(" /v[+|-] display values only (hide type and OID information)");
System.Console.WriteLine(" /n[+|-] numeric form only (do not load any MIBs)");
System.Console.WriteLine(" /s[+|-] surf subkeys");
System.Console.WriteLine(" /w[+|-] load all Windows MIBs from ...\\system32\\*.mib");
System.Console.WriteLine(" /r[+|-] load all RFC MIBs from ...\\mib\\*.mib (slow)");
System.Console.WriteLine(" /m:<Path> Path to additional MIB-files (multiple allowed)");
System.Console.WriteLine(" /d[+|-] download all missing RFCs and extract MIBs from them");
System.Console.WriteLine(" /t:<Timeout> Socket timeout for SNMP queries (default is 5 seconds)");
System.Console.WriteLine(" /x:<File> Export results to CSV file (requires /s)");
System.Console.WriteLine();
}
public static int Main(string[] args)
{
string ApplicationPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
string Host = "127.0.0.1";
string Community = "public";
string ObjectID = "";
string ObjectName = null;
string CustomMibs = "";
string SaveCSV = "";
bool ValuesOnly = false;
bool DontLoadMibs = false;
bool Subkeys = false;
bool WinMibs = false;
bool RfcMibs = false;
bool Download = false;
try
{
if (args.Length == 0)
{
ShowHelp();
return 1;
}
else
{
for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("/h:")) Host = args[i].Substring(3);
else if (args[i].StartsWith("/c:")) Community = args[i].Substring(3);
else if (args[i].StartsWith("/o:")) ObjectID = args[i].Substring(3);
else if (args[i].StartsWith("/m:")) CustomMibs += args[i].Substring(3) + "|";
else if (args[i].StartsWith("/x:")) SaveCSV = args[i].Substring(3);
else if (args[i].StartsWith("/t:")) try
{
Timeout = int.Parse(args[i].Substring(3));
}
catch
{
System.Console.Error.WriteLine("Invalid timeout value");
return 1;
}
else if (args[i].StartsWith("/v")) switch (args[i].Substring(2))
{
case "+": ValuesOnly = true; break;
case "-": ValuesOnly = false; break;
case "": ValuesOnly = !ValuesOnly; break;
}
else if (args[i].StartsWith("/n")) switch (args[i].Substring(2))
{
case "+": DontLoadMibs = true; break;
case "-": DontLoadMibs = false; break;
case "": DontLoadMibs = !DontLoadMibs; break;
}
else if (args[i].StartsWith("/s")) switch (args[i].Substring(2))
{
case "+": Subkeys = true; break;
case "-": Subkeys = false; break;
case "": Subkeys = !Subkeys; break;
}
else if (args[i].StartsWith("/w")) switch (args[i].Substring(2))
{
case "+": WinMibs = true; break;
case "-": WinMibs = false; break;
case "": WinMibs = !WinMibs; break;
}
else if (args[i].StartsWith("/r")) switch (args[i].Substring(2))
{
case "+": RfcMibs = true; break;
case "-": RfcMibs = false; break;
case "": RfcMibs = !RfcMibs; break;
}
else if (args[i].StartsWith("/d")) switch (args[i].Substring(2))
{
case "+": Download = true; break;
case "-": Download = false; break;
case "": Download = !Download; break;
}
else
{
ShowHelp();
return 1;
}
}
if (!ValuesOnly)
{
System.Console.WriteLine(Title);
System.Console.WriteLine();
}
}
if (ObjectID == "")
{
ObjectID = DefaultObjectID;
Subkeys = true;
}
if (Download)
{
Rfc2Mib Rfc2MibObject = new Rfc2Mib();
Rfc2MibObject.DownloadAndExtract(
ApplicationPath + "\\rfc",
ApplicationPath + "\\mib",
RFCUrl,
RFCFirstNumber,
RFCLastNumber,
false
);
}
MIB MibObject = new MIB();
if (!DontLoadMibs) if (!ValuesOnly)
{
bool MibsLoaded = false;
MibObject.LoadMibCache(ApplicationPath + "\\mibcache.txt");
if (RfcMibs) MibsLoaded = MibObject.Load(ApplicationPath + "\\mib") | MibsLoaded;
if (WinMibs) MibsLoaded = MibObject.Load(System.Environment.SystemDirectory) | MibsLoaded;
MibsLoaded = MibObject.Load(ApplicationPath) | MibsLoaded;
if (CustomMibs != "")
{
string[] A = CustomMibs.Split('|');
for (int i = 0; i < A.Length - 1; i++) MibsLoaded = MibObject.Load(A[i]) | MibsLoaded;
}
if (MibsLoaded) MibObject.SaveMibCache(ApplicationPath + "\\mibcache.txt");
}
System.Console.Write(("").PadRight(79) + "\r");
SNMPSocket Socket = new SNMPSocket();
Socket.SocketTimeout = Timeout;
SNMPPacket SNMP;
string Response = "";
string CurrentObjectID = ObjectID;
if (Subkeys)
{
System.IO.StreamWriter StreamWriter = null;
string Separator = "";
if (SaveCSV != "")
{
StreamWriter = new System.IO.StreamWriter(SaveCSV);
Separator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator;
StreamWriter.WriteLine("OID" + Separator + "Path" + Separator + "Type" + Separator + "Value" + Separator + "ValuePath");
}
if (!ValuesOnly)
{
if (!DontLoadMibs) ObjectName = MibObject.Oid2Name(ObjectID);
System.Console.WriteLine("Searching " + ObjectID + " ..." + ((ObjectName == null) ? "" : " (" + MibObject.Oid2Name(CurrentObjectID) + ")"));
System.Console.WriteLine();
}
while (true)
{
string FirstObjectID = CurrentObjectID;
try
{
try
{
SNMP = new SNMPPacket();
SNMP.PacketType = SnmpType.GetNextRequestPDU;
SNMP.Community = Community;
SNMP.ObjectID = CurrentObjectID;
SNMP = Socket.Send(Host, SNMP);
Response = System.Convert.ToString(SNMP.Value);
CurrentObjectID = SNMP.ObjectID;
}
catch
{
throw new System.Exception("No response");
}
if (FirstObjectID == CurrentObjectID) break;
if (!CurrentObjectID.StartsWith(ObjectID)) break;
if (!ValuesOnly)
{
if (!DontLoadMibs) ObjectName = MibObject.Oid2Name(CurrentObjectID);
System.Console.WriteLine("OID=" + CurrentObjectID);
if (ObjectName != null) System.Console.WriteLine(" Path=" + ObjectName);
string Type = SNMP.ValueType.ToString();
System.Console.WriteLine(" Type=" + Type);
System.Console.WriteLine(" Value='" + Response + "'");
string ValuePath = "";
if (!DontLoadMibs) if (Type == "ObjectIDentifier")
{
ValuePath = MibObject.Oid2Name(Response);
System.Console.WriteLine(" ValuePath=" + ValuePath);
}
if (SaveCSV != "") StreamWriter.WriteLine(CurrentObjectID + Separator + ObjectName + Separator + Type + Separator + "\"" + Response + "\"" + Separator + ValuePath);
}
else
{
System.Console.WriteLine(Response);
if (SaveCSV != "") StreamWriter.WriteLine(CurrentObjectID + Separator + Response);
}
}
catch
{
if (!ValuesOnly) System.Console.WriteLine("OID=" + CurrentObjectID);
else System.Console.WriteLine(Response);
throw;
}
}
if (SaveCSV != "") StreamWriter.Close();
}
else
{
try
{
try
{
SNMP = new SNMPPacket();
SNMP.PacketType = SnmpType.GetRequestPDU;
SNMP.Community = Community;
SNMP.ObjectID = CurrentObjectID;
SNMP = Socket.Send(Host, SNMP);
Response = System.Convert.ToString(SNMP.Value);
CurrentObjectID = SNMP.ObjectID;
}
catch
{
throw new System.Exception("No response");
}
if (!ValuesOnly)
{
if (!DontLoadMibs) ObjectName = MibObject.Oid2Name(CurrentObjectID);
System.Console.WriteLine("OID=" + CurrentObjectID);
if (ObjectName != null) System.Console.WriteLine(" Path=" + ObjectName);
string Type = SNMP.ValueType.ToString();
System.Console.WriteLine(" Type=" + Type);
System.Console.WriteLine(" Value='" + Response + "'");
if (Type == "ObjectIDentifier") System.Console.WriteLine(" ValuePath=" + MibObject.Oid2Name(Response));
}
else System.Console.WriteLine(Response);
}
catch
{
if (!ValuesOnly) System.Console.WriteLine("OID=" + CurrentObjectID);
else System.Console.WriteLine(Response);
throw;
}
}
}
catch (System.Exception E)
{
System.Console.Error.WriteLine(E.Message);
if (!ValuesOnly) System.Console.WriteLine();
return 1;
}
if (!ValuesOnly) System.Console.WriteLine();
return 0;
}
}
}