public static Hashtable getValues(Pdu pdu) { Hashtable htResult = new Hashtable(); int i = 0; foreach (Vb vb in pdu) { i++; SnmpSyntax val = vb.Value; SmiSyntax type = val != null ? val.SmiSyntax : SmiSyntax.Null; Hashtable htElement = new Hashtable(); //Transform val to a string StringWriter sw = new StringWriter(); sw.Write("{0}", val); string myValue = sw.ToString(); htElement.Add("value", myValue); htElement.Add("type", type.ToString()); htElement.Add("oid", vb.Oid.ToString()); htResult.Add(i, htElement); } return(htResult); }
/// <summary> /// Prints the SNMP table returned by <see cref="GetTable"/>. /// </summary> /// <param name="os">The writer to use.</param> /// <param name="columnOids">The column OIDs.</param> /// <param name="vbTable">The table of variable bindings.</param> public static void PrintTable(TextWriter os, Oid[] columnOids, Vb[][] vbTable) { foreach (Vb[] vbRow in vbTable) { if (vbRow.Length > 0) { Oid rowIndex = GetRowIndex(vbRow, columnOids); os.WriteLine("Row index: " + rowIndex); for (int j = 0; j < vbRow.Length; j++) { if (vbRow[j] != null) { Oid oid = vbRow[j].Oid; SnmpSyntax val = vbRow[j].Value; os.WriteLine("{0},{1} ({2})", oid, val, val.SmiSyntax); } else { Oid oid = columnOids[j].Append(rowIndex); os.WriteLine("{0}, ({1})", oid, SmiSyntax.Null); } } os.WriteLine(); } } }
public static void PrintPdu(TextWriter os, string text, Pdu pdu, bool debug, object id) { lock (os) { os.WriteLine("[{0}]: {1}{2}", id, text, debug ? "\n" + pdu : ""); // another way would be through Pdu.Vbs: // foreach (Vb vb in pdu.Vbs) {...} int i = 0; foreach (Vb vb in pdu) { SnmpSyntax val = vb.Value; SmiSyntax type = val != null ? val.SmiSyntax : SmiSyntax.Null; os.WriteLine("oid [{0}]: {1}\nval [{0}]: {2} ({3})", i, vb.Oid, val, type); i++; } } }
private static void Walk(Snmp snmp, Pdu pdu, SnmpTarget target, bool asyncSync, bool show, bool debug, ref int ncalls) { Console.WriteLine("Rentre dans le Walk"); string thName = Thread.CurrentThread.Name; Oid rootOid = pdu[0].Oid; while (true) { if (debug) { PrintPdu(Console.Out, "Sending PDU to target " + target, pdu, debug); } Pdu resp = Invoke(snmp, pdu, target, asyncSync); ncalls++; Vb nextVb = resp[0]; Oid nextOid = nextVb.Oid; if (!nextOid.StartsWith(rootOid)) { break; } if (debug) { PrintPdu(Console.Out, "Received PDU:", resp, debug); } else if (show) { SnmpSyntax val = nextVb.Value; SmiSyntax type = val != null ? val.SmiSyntax : SmiSyntax.Null; Console.WriteLine("[{0}]: {1},{2},{3}", thName, nextOid, val, type); } pdu = pdu.Clone(new Vb(nextOid)); } }
private void ResponseCallback(IAsyncResult ar) { if (ar.AsyncState != this) { throw new ArgumentException( "Fatal: invalid data passed to callback"); } using (MemoryManager.GetMemoryManager()) { Pdu pdu; try { pdu = snmp_.EndInvoke(ar); } catch (Exception e) { Console.Error.WriteLine(e); barrier_.Enter(); return; } bool show = (nRepd_ % 1000) == 0; if (show || debug_) { ManagerUtilities.PrintPdu(Console.Out, "Callback PDU:", pdu, true, id_.ToString()); } Pdu nextPdu = pdu_; if (operType_ == OperType.Walk) { Vb nextVb = pdu[0]; Oid nextOid = nextVb.Oid; Oid rootOid = pdu_[0].Oid; if (nextOid.StartsWith(rootOid)) { if (show) { SnmpSyntax val = nextVb.Value; SmiSyntax type = val != null ? val.SmiSyntax : SmiSyntax.Null; Console.WriteLine("[{0}]: {1},{2},{3}", id_, nextOid, val, type); } nRepd_--; nextPdu = pdu_.Clone(new Vb(nextOid)); } } nCalls_++; nRepd_++; if (nRepd_ < nRepeats_) { AsyncDoSnmp(snmp_, target_, nextPdu, new AsyncCallback(ResponseCallback), this); } else { barrier_.Enter(); } } }
public static void Main(string[] args) { if (args.Length < 2) { Console.Error.WriteLine( "SNMP++.NET {0} built on {1}; SNMP++ v. {2}\n\n" + "Usage: {3} <ip> <readCommunity> [<debugMark>]", Assembly.GetAssembly(typeof(Snmp)), Snmp.BuildTime, Snmp.Snmp_ppVersion, Environment.GetCommandLineArgs()[0]); Environment.Exit(1); } string ip = args[0], community = args[1]; bool debug = args.Length > 2 && args[2].Length > 0; try { SnmpTarget.DefaultTimeout = 10000; SnmpTarget.DefaultRetries = 2; using (Snmp snmp = new Snmp(false)) { UdpAddress udp = new UdpAddress(ip); SnmpVersion ver = SnmpVersion.SNMPv1; CTarget target = new CTarget(udp, ver, community, community); Oid systemOid = (Oid)SYSOID2NAME_["system"]; Vb vb = new Vb(systemOid); Pdu pdu = new Pdu(PduType.GetNext, vb); while (true) { if (debug) { Console.WriteLine("Sending : " + pdu + " to " + target); } Pdu resp = snmp.Invoke(pdu, target); if (debug) { Console.WriteLine("Received: " + resp); } vb = resp[0]; Oid oid = vb.Oid; if (!oid.StartsWith(systemOid)) { break; } SnmpSyntax val = vb.Value; Console.WriteLine("{0}({1}): {2} ({3})", oid, SYSOID2NAME_[oid], val, val.SmiSyntax); pdu = pdu.Clone(vb); } } } catch (SnmpException e) { Console.WriteLine("SnmpException:" + "\nstatus : " + e.ErrorStatus + "\nindex : " + e.ErrorIndex + "\nmessage: " + e.Message); } catch (Exception e) { Console.WriteLine(e); } }
// Processes the next response to GET NEXT/GET BULK request. Map 'rows' // contains already received rows (possibly only partially filled with // variable bindings). The method returns new OIDs which are to be used // in the subsequent requests. If this is the last response, this array // will be empty. Note that 'columnOids' is also modified and nulls are // set for columns that have been finished. private static Oid[] ProcessResponse( Oid[] columnOids, Vb[] vbs, Oid endRowIndex, Hashtable rows) { int nNonNulls = GetNonNullCount(columnOids); Oid[] oids = new Oid[columnOids.Length]; Oid rowIndex = null; Vb[] row = null; for (int i = 0, o = 0; i < vbs.Length && nNonNulls > 0; i++, o = ++o % columnOids.Length) { while (columnOids[o] == null) { o = ++o % columnOids.Length; } Oid columnOid = columnOids[o]; Vb vb = vbs[i]; Oid oid = vb.Oid; SnmpSyntax val = vb.Value; // Check whether this Vb should be included in the results // and whether this column has been finished. 'val' should // never be NULL but some faulty agents send NULLs. bool endOfColumn = !oid.StartsWith(columnOid) || (val != null && val.SmiSyntax == SmiSyntax.EndOfMibView); bool includeVb = !endOfColumn; Oid rowIdx = null; if (!endOfColumn) { rowIdx = oid.TrimLeft(columnOid.Length); if (endRowIndex != null) { int res = rowIdx.CompareTo(endRowIndex); endOfColumn = res >= 0; includeVb = res <= 0; } } // if a valid value has been returned, store it in the hash // table and store its OID for a subsequent request if (includeVb) { oids[o] = oid; if (rowIndex == null || !rowIdx.Equals(rowIndex)) { rowIndex = rowIdx; row = (Vb[])rows[rowIndex]; if (row == null) { row = new Vb[columnOids.Length]; rows.Add(rowIndex, row); } } row[o] = vb; } // if the column has been finished, remove it from further // processing if (endOfColumn) { if (val != null && val.SmiSyntax == SmiSyntax.EndOfMibView) { columnOids[o] = null; nNonNulls--; } oids[o] = null; } } // set finished columns of 'columnOids' to null // and remove nulls from 'oids' int noids = 0; for (int i = 0; i < oids.Length; i++) { if (oids[i] == null) { columnOids[i] = null; } else { oids[noids++] = oids[i]; } } // shrink 'oids', if necessary if (noids < oids.Length) { Oid[] o = new Oid[noids]; Array.Copy(oids, 0, o, 0, noids); oids = o; } return(oids); }