/// <summary> /// Feeds the sequence. /// </summary> /// <param name="targetInstance">The target instance.</param> /// <param name="childrenType">Type of the children.</param> /// <param name="rootId">The OID root identifier.</param> /// <param name="datas">The datas.</param> private void FeedSequence(object targetInstance, Type childrenType, Oid rootId, Dictionary <Oid, AsnType> datas) { if (datas != null && datas.Count > 0) { foreach (var obj in datas) { string instanceId = string.Join(".", Oid.GetChildIdentifiers(rootId, obj.Key).Skip(1)); IDictionary objects = targetInstance as IDictionary; if (objects[instanceId] == null && childrenType.IsClass) { objects[instanceId] = Activator.CreateInstance(childrenType); } if (childrenType.IsClass) { FeedObject(objects[instanceId], obj.Key, datas.Where(i => obj.Key.IsRootOf(i.Key)).ToDictionary(k => k.Key, v => v.Value)); } else { objects[instanceId] = GetValue(obj.Value, childrenType); } } } }
public void GetTable(string OID) { this.param.Version = SnmpVersion.Ver2; Oid startOid = new Oid(OID); startOid.Add(1); Console.WriteLine(startOid); Pdu bulkPdu = Pdu.GetBulkPdu(); bulkPdu.VbList.Add(startOid); bulkPdu.NonRepeaters = 0; bulkPdu.MaxRepetitions = 100; Oid curOid = (Oid)startOid.Clone(); while (startOid.IsRootOf(curOid)) { SnmpPacket res = null; try { res = target.Request(bulkPdu, param); } catch (Exception ex) { Console.WriteLine("Request failed: {0}", ex.Message); target.Close(); return; } if (res.Version != SnmpVersion.Ver2) { Console.WriteLine("Received wrong SNMP version response packet."); target.Close(); return; } if (res.Pdu.ErrorStatus != 0) { Console.WriteLine("SNMP agent returned error {0} for request Vb index {1}", res.Pdu.ErrorStatus, res.Pdu.ErrorIndex); target.Close(); return; } foreach (Vb v in res.Pdu.VbList) { curOid = (Oid)v.Oid.Clone(); if (startOid.IsRootOf(v.Oid)) { uint[] childOids = Oid.GetChildIdentifiers(startOid, v.Oid); uint[] instance = new uint[childOids.Length - 1]; Array.Copy(childOids, 1, instance, 0, childOids.Length - 1); String strInst = InstanceToString(instance); uint column = childOids[0]; if (!tableColumns.Contains(column)) { tableColumns.Add(column); } if (results.ContainsKey(strInst)) { results[strInst][column] = (AsnType)v.Value.Clone(); } else { results[strInst] = new Dictionary <uint, AsnType>(); results[strInst][column] = (AsnType)v.Value.Clone(); } } else { break; } } if (startOid.IsRootOf(curOid)) { bulkPdu.VbList.Clear(); bulkPdu.VbList.Add(curOid); bulkPdu.NonRepeaters = 0; bulkPdu.MaxRepetitions = 100; } } }
static void Main(string[] args) { // Delete file if it exists. if (File.Exists(@".\\SnmpDump.txt")) { File.Delete(@".\\SnmpDump.txt"); } if (args.Length != 3) { Console.WriteLine("Syntax: SnmpTable.exe <host> <community> <table oid>"); return; } Dictionary <String, Dictionary <uint, AsnType> > result = new Dictionary <String, Dictionary <uint, AsnType> >(); List <uint> tableColumns = new List <uint>(); AgentParameters param = new AgentParameters(SnmpVersion.Ver2, new OctetString(args[1])); IpAddress peer = new IpAddress(args[0]); if (!peer.Valid) { Console.WriteLine("Unable to resolve name or error in address for peer: {0}", args[0]); return; } UdpTarget target = new UdpTarget((IPAddress)peer); Oid startOid = new Oid(args[2]); startOid.Add(1); Pdu bulkPdu = Pdu.GetBulkPdu(); bulkPdu.VbList.Add(startOid); bulkPdu.NonRepeaters = 0; bulkPdu.MaxRepetitions = 100; Oid curOid = (Oid)startOid.Clone(); while (startOid.IsRootOf(curOid)) { SnmpPacket res = null; try { res = target.Request(bulkPdu, param); } catch (Exception ex) { Console.WriteLine("Request failed: {0}", ex.Message); target.Close(); return; } if (res.Version != SnmpVersion.Ver2) { Console.WriteLine("Received wrong SNMP version response packet."); target.Close(); return; } if (res.Pdu.ErrorStatus != 0) { Console.WriteLine("SNMP agent returned error {0} for request Vb index {1}", res.Pdu.ErrorStatus, res.Pdu.ErrorIndex); target.Close(); return; } foreach (Vb v in res.Pdu.VbList) { curOid = (Oid)v.Oid.Clone(); if (startOid.IsRootOf(v.Oid)) { uint[] childOids = Oid.GetChildIdentifiers(startOid, v.Oid); uint[] instance = new uint[childOids.Length - 1]; Array.Copy(childOids, 1, instance, 0, childOids.Length - 1); String strInst = InstanceToString(instance); uint column = childOids[0]; if (!tableColumns.Contains(column)) { tableColumns.Add(column); } if (result.ContainsKey(strInst)) { result[strInst][column] = (AsnType)v.Value.Clone(); } else { result[strInst] = new Dictionary <uint, AsnType>(); result[strInst][column] = (AsnType)v.Value.Clone(); } } else { break; } } if (startOid.IsRootOf(curOid)) { bulkPdu.VbList.Clear(); bulkPdu.VbList.Add(curOid); bulkPdu.NonRepeaters = 0; bulkPdu.MaxRepetitions = 100; } } target.Close(); if (result.Count <= 0) { Console.WriteLine("No results returned.\n"); } else { foreach (uint column in tableColumns) { //Console.Write("\tColumn id {0}", column); } Console.WriteLine(""); foreach (KeyValuePair <string, Dictionary <uint, AsnType> > kvp in result) { //Console.WriteLine("{0}", kvp.Key); string Entry = ""; foreach (uint column in tableColumns) { if (kvp.Value.ContainsKey(column)) { //Console.WriteLine("\t{0} ({1})", kvp.Value[column].ToString(),SnmpConstants.GetTypeName(kvp.Value[column].Type)); Entry += kvp.Value[column].ToString() + ";"; } else { Console.Write("\t-"); } } using (StreamWriter sw = File.AppendText(path)) { Console.WriteLine(Entry); sw.WriteLine(Entry); } } } }
public static void GetTable(string host, string community, string oid, DataGridView table) { string type; Program.mibObjectsTypes.TryGetValue(Program.mibObjects.FirstOrDefault(x => x.Value == oid).Key, out type); if (type != "T") { MessageBox.Show("Selected position is not a table!"); return; } table.Rows.Clear(); table.Refresh(); Dictionary <String, Dictionary <uint, AsnType> > result = new Dictionary <String, Dictionary <uint, AsnType> >(); List <uint> tableColumns = new List <uint>(); List <string> tableRows = new List <string>(); AgentParameters param; IpAddress peer; try { param = new AgentParameters(SnmpVersion.Ver2, new OctetString(community)); peer = new IpAddress(host); } catch (Exception e) { MessageBox.Show("Invalid IP/port or community settings"); return; } if (!peer.Valid) { MessageBox.Show("Unable to resolve name or error in address for peer: {0}", host); return; } UdpTarget target = new UdpTarget((System.Net.IPAddress)peer); Oid startOid = new Oid(oid); startOid.Add(1); Pdu getNextPdu = Pdu.GetNextPdu(); Oid curOid = (Oid)startOid.Clone(); List <string> columnNames = new List <string>(); string searchOid = "." + startOid.ToString(); foreach (KeyValuePair <string, string> kvp in Program.mibObjects) { if (kvp.Value.Contains(searchOid) && !kvp.Value.Equals(searchOid)) { columnNames.Add(kvp.Key); } } while (startOid.IsRootOf(curOid)) { SnmpPacket res = null; try { res = target.Request(getNextPdu, param); } catch (Exception ex) { MessageBox.Show("Request failed: {0}", ex.Message); target.Close(); return; } if (res.Pdu.ErrorStatus != 0) { MessageBox.Show("SNMP agent returned error " + res.Pdu.ErrorStatus + " for request Vb index " + res.Pdu.ErrorIndex); target.Close(); return; } foreach (Vb v in res.Pdu.VbList) { curOid = (Oid)v.Oid.Clone(); if (startOid.IsRootOf(v.Oid)) { uint[] childOids = Oid.GetChildIdentifiers(startOid, v.Oid); uint[] instance = new uint[childOids.Length - 1]; Array.Copy(childOids, 1, instance, 0, childOids.Length - 1); String strInst = InstanceToString(instance); uint column = childOids[0]; if (!tableColumns.Contains(column)) { tableColumns.Add(column); } if (result.ContainsKey(strInst)) { result[strInst][column] = (AsnType)v.Value.Clone(); } else { result[strInst] = new Dictionary <uint, AsnType>(); result[strInst][column] = (AsnType)v.Value.Clone(); } } else { break; } } if (startOid.IsRootOf(curOid)) { getNextPdu.VbList.Clear(); getNextPdu.VbList.Add(curOid); } } target.Close(); if (result.Count <= 0) { MessageBox.Show("No results returned."); } else { table.ColumnCount = tableColumns.Count + 1; table.Columns[0].Name = "Instance"; for (int i = 0; i < tableColumns.Count; i++) { table.Columns[i + 1].Name = columnNames[i]; //table.Columns[i + 1].Name = "Column id " + tableColumns[i]; } foreach (KeyValuePair <string, Dictionary <uint, AsnType> > kvp in result) { tableRows.Add(kvp.Key); foreach (uint column in tableColumns) { if (kvp.Value.ContainsKey(column)) { tableRows.Add(kvp.Value[column].ToString()); } else { tableRows.Add(""); } } table.Rows.Add(tableRows.ToArray()); tableRows.Clear(); } } }
/// <summary> /// Funkcja zwracająca , zaczynając od obiektu o oid podanym w argumencie. /// </summary> /// <param name="version"></param> /// <param name="startOid"></param> /// <returns></returns> public Dictionary <String, Dictionary <uint, AsnType> > GetTableRequest(SnmpVersion version, string _startOid) { Dictionary <String, Dictionary <uint, AsnType> > resultDictionary = new Dictionary <String, Dictionary <uint, AsnType> >(); //To jest OID tabeli na wejściu funkcji Oid startOid = new Oid(_startOid); /* * // Not every row has a value for every column so keep track of all columns available in the table * List<uint> tableColumns = new List<uint>(); * * //Każda tabela OID ma na końcu .1 dla wpisu OID, trzeba go dodać do tabeli * startOid.Add(1); * * //Przygotowanie PDU do zapytania * Pdu pdu = new Pdu(PduType.GetNext); * * //Dodanie startOid do VarBindList PDU * pdu.VbList.Add(startOid); */ Oid currentOid = (Oid)startOid.Clone(); AgentParameters param = new AgentParameters( version, new OctetString(snmp.Community)); //Dopoki nie osiagniemy konca tabeli while (startOid.IsRootOf(currentOid)) { SnmpPacket result = null; try { result = this.GetNextRequest(SnmpVersion.Ver2, currentOid.ToString(), this.snmp.PeerIP); } catch (Exception e) { Console.WriteLine("GetTableRequest(): request failed. " + e.Message); return(null); } if (result.Pdu.ErrorStatus != 0) { Console.WriteLine("SNMP Agent returned error: " + result.Pdu.ErrorStatus + " for request with Vb of index: " + result.Pdu.ErrorIndex); return(null); } foreach (Vb v in result.Pdu.VbList) { currentOid = (Oid)v.Oid.Clone(); //upewniamy sie ze jestesmy w tabeli if (startOid.IsRootOf(v.Oid)) { //Otrzymanie ID childa z OID uint[] childOids = Oid.GetChildIdentifiers(startOid, v.Oid); // Get the value instance and converted it to a dotted decimal // string to use as key in result dictionary uint[] instance = new uint[childOids.Length - 1]; Array.Copy(childOids, 1, instance, 0, childOids.Length - 1); String strInst = InstanceToString(instance); // Column id is the first value past <table oid>.entry in the response OID uint column = childOids[0]; if (resultDictionary.ContainsKey(strInst)) { resultDictionary[strInst][column] = (AsnType)v.Value.Clone(); } else { resultDictionary[strInst] = new Dictionary <uint, AsnType>(); resultDictionary[strInst][column] = (AsnType)v.Value.Clone(); } } else { // We've reached the end of the table. No point continuing the loop break; } } } return(resultDictionary); }
private void GetTable(String host, AgentParameters param, String tableOID, DataGridView table) { table.Rows.Clear(); table.Refresh(); Dictionary <String, Dictionary <uint, AsnType> > result = new Dictionary <String, Dictionary <uint, AsnType> >(); List <uint> tableColumns = new List <uint>(); List <string> tableRows = new List <string>(); IpAddress peer = new IpAddress(host); if (!peer.Valid) { MessageBox.Show("Unable to resolve name or error in address for peer: {0}", host); return; } UdpTarget target = new UdpTarget((IPAddress)peer); Oid startOid = new Oid(tableOID); startOid.Add(1); Pdu nextPdu = Pdu.GetNextPdu(); nextPdu.VbList.Add(startOid); Oid curOid = (Oid)startOid.Clone(); List <string> colNames = new List <string>(); string searchOid = "." + startOid.ToString(); foreach (KeyValuePair <string, string> kvp in Program.MIbsList) { if (kvp.Value.Contains(searchOid) && !kvp.Value.Equals(searchOid)) { colNames.Add(kvp.Key); } } while (startOid.IsRootOf(curOid)) { SnmpPacket res = null; try { res = target.Request(nextPdu, param); } catch (Exception e) { MessageBox.Show("Request failed: {0}", e.Message); target.Close(); return; } if (res.Pdu.ErrorStatus != 0) { MessageBox.Show("SNMP agent returned error " + res.Pdu.ErrorStatus + " for request Vb index " + res.Pdu.ErrorIndex); target.Close(); return; } foreach (Vb v in res.Pdu.VbList) { curOid = (Oid)v.Oid.Clone(); if (startOid.IsRootOf(v.Oid)) { uint[] childOids = Oid.GetChildIdentifiers(startOid, v.Oid); uint[] instance = new uint[childOids.Length - 1]; Array.Copy(childOids, 1, instance, 0, childOids.Length - 1); String strInst = InstanceToString(instance); uint column = childOids[0]; if (!tableColumns.Contains(column)) { tableColumns.Add(column); } if (result.ContainsKey(strInst)) { result[strInst][column] = (AsnType)v.Value.Clone(); } else { result[strInst] = new Dictionary <uint, AsnType>(); result[strInst][column] = (AsnType)v.Value.Clone(); } } else { break; } } if (startOid.IsRootOf(curOid)) { nextPdu.VbList.Clear(); nextPdu.VbList.Add(curOid); } } target.Close(); if (result.Count <= 0) { MessageBox.Show("No results returned."); } else { table.ColumnCount = tableColumns.Count + 1; table.Columns[0].Name = "Instance"; for (int i = 0; i < tableColumns.Count; i++) { //table.Columns[i + 1].Name = "Column id " + tableColumns[i]; table.Columns[i + 1].Name = colNames[i]; } foreach (KeyValuePair <string, Dictionary <uint, AsnType> > kvp in result) { tableRows.Add(kvp.Key); foreach (uint column in tableColumns) { if (kvp.Value.ContainsKey(column)) { tableRows.Add(kvp.Value[column].ToString()); } else { tableRows.Add(""); } } table.Rows.Add(tableRows.ToArray()); tableRows.Clear(); } } }
private void GetTable(Oid oid) { SnmpV2Packet localResult = new SnmpV2Packet(); //table[n] wskazuje kolumny //table[n][m] wskazuje konkretne komórki List <List <String> > table = new List <List <String> >(); List <string> columnOID = new List <string>(); // This is the table OID supplied on the command line Oid startOid = oid; // Each table OID is followed by .1 for the entry OID. Add it to the table OID // Add Entry OID to the end of the table OID startOid.Add(1); // Current OID will keep track of the last retrieved OID and be used as // indication that we have reached end of table Oid currentOid = (Oid)startOid.Clone(); Oid prevOid = null; // Keep looping through results until end of table // startOid.IsRootOf(curOid) // Compares the passed object identifier against self to determine if self is the root of the passed object. // If the passed object is in the same root tree as self then a true value is returned. // Otherwise a false value is returned from the object. while (true) { //make GetNext localResult = _mainForm.GetNextRequest(currentOid); //set prev before changing current prevOid = currentOid; uint[] prevChildOids = Oid.GetChildIdentifiers(startOid, prevOid); //set OID result to currentOid currentOid = localResult.Pdu.VbList[0].Oid; // Make sure we are dealing with an OID that is part of the table if (startOid.IsRootOf(currentOid)) { //get every childOid start from startOID to currentOID // if startOid = .1.1 and currentOid = .1.1.2.1 result will be [0] = 2 [1] = 1 uint[] currentChildOids = Oid.GetChildIdentifiers(startOid, currentOid); //first column if ((prevOid == startOid)) { table.Add(new List <string>()); Oid columnOid = (Oid)startOid.Clone(); columnOid.Add(currentChildOids[0]); columnOID.Add(columnOid.ToString()); } //next columns if (currentChildOids != null && prevChildOids != null) { //the first element is always name of column (new parameter in table) if ((currentChildOids[0] > prevChildOids[0])) { //it means, currentOid is showing another column (parameter) //adding another column! table.Add(new List <string>()); Oid columnOid = (Oid)startOid.Clone(); columnOid.Add(currentChildOids[0]); columnOID.Add(columnOid.ToString()); } } //saving the values table[table.Count - 1].Add(localResult.Pdu.VbList[0].Value.ToString()); } else { // We've reached the end of the table. No point continuing the loop //wychodzi z pętli tylko wtedy, gdy wyszlismy poza korzeń czyli długość curOid jest taka sama jak start i nie sa równe break; } }//end of while /* * startOid - początkowe .1.0 * prevOid - Oid z poprzedniego wywołania (służy do patrzenia, czy jesteśmy jeszcze w tej samej kolumnie czy już w nastepnej * currentOid - otrzymane OID z metody GetNext * * curOid musi zawierać całe startOid i być dłuższe aby while działał * */ ShowTable(columnOID, table); }