private void AsyncDoSnmp() { if (operType_ != OperType.Table) { AsyncDoSnmp(snmp_, target_, pdu_, new AsyncCallback(ResponseCallback), this); } else { Oid[] columnOids = GetColumnOids(pdu_); Pdu pdu = pdu_.Clone(new Vb[0]); // not really needed; for test purposes TableReader.BeginGetTable(snmp_, pdu, target_, columnOids, tableOptions_, new AsyncCallback(TableResponseCallback), columnOids); } }
private static bool ProcessGetTable(GetTableState state, IAsyncResult ar, ref int nRequests) { Pdu pdu = state.pdu; Oid[] oids; try { Pdu resp = state.invoke(ar); oids = ProcessResponse( state.columnOids, resp.Vbs, state.endRowIndex, state.rows); } catch (SnmpException e) { oids = ProcessSnmpv1EndOfMIB( state.target.Version, state.columnOids, pdu, e); if (oids == null) { throw; } } nRequests++; if (oids.Length == 0 || state.rows.Count >= state.maxRows) { return(true); } else { state.vbs = CreateNullVbs(oids, state.vbs); state.pdu = pdu.Clone(state.vbs); return(false); } }
private static GetTableState PrepareGetTable( Snmp snmp, Pdu pdu, SnmpTarget target, Oid[] columnOids, Oid startRowIndex, Oid endRowIndex, int maxRows, int rowsPerQuery, AsyncCallback callback, object callbackData, bool sync) { if (!(pdu.Type == PduType.GetNext || pdu.Type == PduType.GetBulk)) { throw new ArgumentException("Invalid pdu type: " + pdu.Type, "pdu"); } if (columnOids.Length == 0) { throw new ArgumentException("Empty column oid table", "columnOids"); } if (maxRows <= 0) { maxRows = int.MaxValue; } columnOids = (Oid[])columnOids.Clone(); Vb[] vbs = CreateNullVbs(columnOids, startRowIndex); pdu = pdu.Clone(vbs); pdu.NonRepeaters = 0; return(new GetTableState(snmp, pdu, target, columnOids, endRowIndex, maxRows, rowsPerQuery, vbs, callback, callbackData, sync)); }
private static void Table(Snmp snmp, Pdu pdu, SnmpTarget target, ref TableReader.GetTableOptions tableOptions, bool show, bool debug, ref int ncalls) { Oid[] columnOids = GetColumnOids(pdu); pdu = pdu.Clone(new Vb[0]); // not really needed; for test purposes Vb[][] vbTable = TableReader.GetTable(snmp, pdu, target, columnOids, tableOptions, ref ncalls); if (show || debug) { TableReader.PrintTable(Console.Out, columnOids, vbTable); } }
public static IList Walk(Snmp snmp, SnmpTarget target, Pdu pdu, IList list) { if (!(pdu.Type == PduType.GetNext || pdu.Type == PduType.GetBulk)) { throw new ArgumentException("Invalid Pdu type", "pdu"); } if (pdu.Count != 1) { throw new ArgumentException("Expected a single Vb", "pdu"); } try { Oid subtree = pdu[0].Oid; while (true) { pdu = snmp.Invoke(pdu, target); Vb lastVb = null; foreach (Vb vb in pdu) { Oid oid = vb.Oid; if (!oid.StartsWith(subtree) || vb.Value.SmiSyntax == SmiSyntax.EndOfMibView) { return(list); } lastVb = vb; list.Add(vb); } pdu = pdu.Clone(lastVb); } } catch (SnmpException e) { // SNMPv1 and NoSuchName? if (e.ErrorStatus != SnmpError.NoSuchName) { throw; } } return(list); }
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)); } }
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); } }