public bool SnmpSetTest(string host, string community, string oid, string octetStromg) { try { SimpleSnmp snmp = new SimpleSnmp(host, community); if (!snmp.Valid) { return(false); } Dictionary <Oid, AsnType> result = snmp.Set(SnmpVersion.Ver1, new Vb[] { new Vb(new Oid(oid), new OctetString(octetStromg)) }); if (result == null) { return(false); } LogResult(result); return(true); } catch (Exception ex) { return(false); } }
/// /// <summary> /// Executes the installation process on the SNMP agent. /// </summary> /// <param name="ts">Target's settings</param> /// <param name="uploadFolder">Path of folder with exe update within it</param> /// <returns>True if installation was executed successfully</returns> public static bool setExecute(TargetSettings ts, string uploadFolder) { SimpleSnmp snmp = new SimpleSnmp(ts.TargetServer, ts.Community); if (!snmp.Valid) { return(false); } string d = ts.DestinationFolder.Substring(1); int i = uploadFolder.LastIndexOf("\\") + 1; string relativePath = uploadFolder.Substring(i); string destination = d.Replace("/", "\\"); string batFile = ts.RootPath + destination + relativePath + "\\UpdateVersion.bat"; Dictionary <Oid, AsnType> result = snmp.Set(SnmpVersion.Ver2, new Vb[] { new Vb(new Oid("1.3.6.1.4.1.2566.127.1.1.157.3.1.1.10.0"), new OctetString(batFile)) }); if (result != null) { return(true); } return(false); }
/// <summary> /// Send a SetRequest to the agent to start attacking /// </summary> /// <param name="selected"></param> public void SetAttacking(int selected) { String snmpAgent = "127.0.0.1"; String snmpCommunity = "public"; SimpleSnmp snmp = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 2); // Create a set Pdu Pdu pdu = new Pdu(); pdu.Type = PduType.Set; pdu.VbList.Add(new Oid(baseTreeOid + ".16"), new Counter32((uint)selected)); PrintPacketSend(pdu); Dictionary <Oid, AsnType> result = snmp.Set(SnmpVersion.Ver1, pdu); if (result == null) { Debug.Log("Manager:Set failed."); } else { foreach (KeyValuePair <Oid, AsnType> entry in result) { attacking = int.Parse(entry.Value.ToString()); } } }
/// <summary> /// Send a SetRequest to the agent to select a target /// </summary> /// <param name="selected"></param> public void SetTarget(int selected) { String snmpAgent = "127.0.0.1"; String snmpCommunity = "public"; SimpleSnmp snmp = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 2); // Create a set Pdu Pdu pdu = new Pdu(); pdu.Type = PduType.Set; pdu.VbList.Add(new Oid(baseTreeOid + ".14"), new Counter32((uint)selected)); PrintPacketSend(pdu); Dictionary <Oid, AsnType> result = snmp.Set(SnmpVersion.Ver1, pdu); if (result == null) { Debug.Log("Manager:Set failed."); } else { foreach (KeyValuePair <Oid, AsnType> entry in result) { selectedTarget = int.Parse(entry.Value.ToString()); } } if (selected == 0) { Enemy1.transform.GetChild(1).gameObject.SetActive(true); } else { Enemy1.transform.GetChild(1).gameObject.SetActive(false); } if (selected == 1) { Enemy2.transform.GetChild(1).gameObject.SetActive(true); } else { Enemy2.transform.GetChild(1).gameObject.SetActive(false); } if (selected == 2) { Enemy3.transform.GetChild(1).gameObject.SetActive(true); } else { Enemy3.transform.GetChild(1).gameObject.SetActive(false); } }
private void SetDataSnrERD(string oid, int data) { var snmp = new SimpleSnmp(IpAddress, Community); Pdu pdu = new Pdu { Type = PduType.Set }; pdu.VbList.Add(new Oid(oidTestFlag), new Integer32(data)); Dictionary <Oid, AsnType> result = snmp.Set(SnmpVersion.Ver1, pdu); }
/// <summary> /// when button is clicked send the GoTo with the data from the input fields /// </summary> public void SetGoTo() { SimpleSnmp snmp = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 2); // Create a request Pdu Pdu pdu = new Pdu(); pdu.Type = PduType.Set; //pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); pdu.VbList.Add(new Oid(baseTreeOid + ".3"), new Integer32(MoveToXInput)); pdu.VbList.Add(new Oid(baseTreeOid + ".4"), new Integer32(MoveToYInput)); PrintPacketSend(pdu); Dictionary <Oid, AsnType> result = snmp.Set(SnmpVersion.Ver1, pdu); //Debug.Log(result); //Dictionary<Oid, AsnType> result = snmp.GetNext(SnmpVersion.Ver1, pdu); if (result == null) { Debug.Log("Manager:Set failed."); } }
private void backgroundWorkerMain_DoWork(object sender, DoWorkEventArgs e) { try { e.Result = null; if (snmpObj.Version != SnmpVersion.Ver3) { SimpleSnmp simpleSnmp = new SimpleSnmp(snmpObj.IP, snmpObj.Port, snmpObj.CommunityName, snmpObj.Timeout, 1); if (snmpObj.PduType == PduType.Get) { result = simpleSnmp.Get(snmpObj.Version, snmpObj.Pdu); } else if (snmpObj.PduType == PduType.GetNext) { result = simpleSnmp.GetNext(snmpObj.Version, snmpObj.Pdu); } else if (snmpObj.PduType == PduType.GetBulk && snmpObj.Version == SnmpVersion.Ver2) { result = simpleSnmp.GetBulk(snmpObj.Pdu); } else if (snmpObj.PduType == PduType.Set) { result = simpleSnmp.Set(snmpObj.Version, snmpObj.Pdu); } } else { UdpTarget target = new UdpTarget(IPAddress.Parse(snmpObj.IP), snmpObj.Port, snmpObj.Timeout, 0); SnmpV3Packet packet = (SnmpV3Packet)target.Request(snmpObj.Pdu, snmpObj.Parameters); e.Result = packet; } } catch (Exception ex) { e.Result = ex.Message; } }
private static void DoWork() { // Construct a socket and bind it to the trap manager port 162 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162); EndPoint ep = (EndPoint)ipep; socket.Bind(ep); // Disable timeout processing. Just block until packet is received socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0); while (true) { byte[] indata = new byte[16 * 1024]; // 16KB receive buffer int inlen = 0; IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0); EndPoint inep = (EndPoint)peer; try { inlen = socket.ReceiveFrom(indata, ref inep); } catch (Exception ex) { Functions.AddTempLog(new List <string> { ex.Message, ex.ToString() }); inlen = -1; } if (inlen > 0) { // Check protocol version int ver = SnmpPacket.GetProtocolVersion(indata, inlen); if (ver == (int)SnmpVersion.Ver2) { // Parse SNMP Version 2 TRAP packet SnmpV2Packet pkt = new SnmpV2Packet(); pkt.decode(indata, inlen); List <string> alarm = new List <string> { inep.ToString(), pkt.Community.ToString() }; List <string> trap = new List <string>(); foreach (Vb v in pkt.Pdu.VbList) { trap.Add(v.Value.ToString()); } Functions.AddTempLog(trap); if (trap.Count == 42) { IPCom _ipcom = new IPCom(); CiscoPort _ciscoPort = new CiscoPort(); string path = "C:\\Repository.db"; string _connectionString; _connectionString = "Data Source=" + path + ";Version=3;"; using (var connection = new SQLiteConnection(_connectionString)) { connection.Open(); using (var command = new SQLiteCommand("SELECT IP,Com,PortId FROM Ports WHERE JDSUPort = @_JDSUPort", connection)) { command.Parameters.Add("@_JDSUPort", DbType.String).Value = trap[27]; try { using (var reader = command.ExecuteReader()) { // int k = (int)command.ExecuteScalar(); foreach (DbDataRecord record in reader) { try { SimpleSnmp snmp = new SimpleSnmp(record["IP"].ToString(), record["Com"].ToString()); Pdu pdu = new Pdu(PduType.Set); pdu.VbList.Add(new Oid(".1.3.6.1.2.1.2.2.1.7" + record["PortId"].ToString()), new Integer32(2)); snmp.Set(SnmpVersion.Ver2, pdu); } catch (Exception ex) { alarm.Add(ex.ToString()); } alarm.Add(record["IP"].ToString()); alarm.Add(record["Com"].ToString()); alarm.Add(record["PortId"].ToString()); alarm.Add(trap[27].ToString()); } } } catch (Exception ex) { alarm.Add(ex.ToString()); alarm.Add("в базе данных нет такой записи"); } } } // Functions.AddTempLog(alarm); } else { if (inlen == 0) { Functions.AddTempLog(new List <string> { "Zero length packet received." }); } } } } } }
private void mainDataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { this.mainDataGridView.Rows[e.RowIndex].Selected = false; switch (e.ColumnIndex) { //включить порт case 6: { if (e.RowIndex == -1) { break; } var jdsuCisco = GetJdsuCiscoClass(mainDataGridView[0, e.RowIndex].Value.ToString()); var serviceContext = new WaterGateServiceContext(); serviceContext.LogUnlockingPortAsync(jdsuCisco, UnlockingPortStatus.Starting); string host = jdsuCisco.CiscoIPCom.IP; string community = jdsuCisco.CiscoIPCom.Com; var port = jdsuCisco.CiscoPort; var portCell = this.mainDataGridView.Rows[e.RowIndex].Cells[2]; var ipCell = this.mainDataGridView.Rows[e.RowIndex].Cells[1]; var imageCell = (DataGridViewImageCell)mainDataGridView[e.ColumnIndex, e.RowIndex]; if (imageCell.Description == "OnButton") { ShowToolTrayTooltipActive(port + " " + host); return; } mainDataGridView.Cursor = Cursors.AppStarting; var asyncAction = new Action(() => { SimpleSnmp snmp = new SimpleSnmp(host, community); if (!snmp.Valid) { Invoke(new Action(() => mainDataGridView.Cursor = Cursors.Arrow)); serviceContext.LogUnlockingPortAsync(jdsuCisco, UnlockingPortStatus.InvalidSmnp); MessageBox.Show("Snmp isn't valid"); return; } Pdu pdu = new Pdu(PduType.Set); pdu.VbList.Add(new Oid(".1.3.6.1.2.1.2.2.1.7" + port.PortID), new Integer32(1)); snmp.Set(SnmpVersion.Ver2, pdu); Dictionary <Oid, AsnType> result = snmp.Get(SnmpVersion.Ver2, new[] { ".1.3.6.1.2.1.2.2.1.7" + port.PortID }); if (result == null) { Invoke(new Action(() => mainDataGridView.Cursor = Cursors.Arrow)); serviceContext.LogUnlockingPortAsync(jdsuCisco, UnlockingPortStatus.NoResponse); MessageBox.Show("Нет ответа от " + host + " / возможно указанный IP адрес не является IP адресом коммутационного оборудования Cisco"); return; } Invoke(new Action(() => { mainDataGridView.Cursor = Cursors.Arrow; foreach (var kvp in result) { if (kvp.Value.ToString() == "1") { serviceContext.LogUnlockingPortAsync(jdsuCisco, UnlockingPortStatus.Active); MarkPortCellAsEnabled(imageCell); ShowToolTrayTooltipActive(portCell.Value + " " + ipCell.Value); } else { serviceContext.LogUnlockingPortAsync(jdsuCisco, UnlockingPortStatus.NotActive); MarkPortCellAsDisabled(imageCell); ShowToolTrayTooltip(portCell.Value + " " + ipCell.Value); } } })); }); asyncAction.BeginInvoke(null, null); break; } //проверить порт case 5: { if (e.RowIndex == -1) { break; } var jdsuCisco = GetJdsuCiscoClass(mainDataGridView[0, e.RowIndex].Value.ToString()); var cisco = jdsuCisco.CiscoIPCom; //var cisco = GetJdsuCiscoClass(mainDataGridView[0, e.RowIndex].Value.ToString()).CiscoIPCom; if (cisco == null) { return; } mainDataGridView.Cursor = Cursors.AppStarting; var asyncAction = new Action(() => { try { if (IsCiscoActive(cisco)) { ShowToolTrayTooltipActive(jdsuCisco.CiscoPort.PortName + "" + jdsuCisco.CiscoIPCom.IP); } } catch (Exception ex) { foreach (DataGridViewRow label in this.mainDataGridView.Rows) { if (label.Cells[1].Value.ToString() == cisco.IP) { if (!IsPortDisabled(label)) { ShowToolTrayTooltip(cisco.IP + " " + cisco.Com); } paintCiscoIP(label, System.Drawing.Color.Red); Invoke( new Action(() => MarkPortCellAsDisabled((DataGridViewImageCell)label.Cells[6]))); } } Functions.AddTempLog(cisco.IP, ex.Message); } finally { Invoke(new Action(() => mainDataGridView.Cursor = Cursors.Arrow)); } }); asyncAction.BeginInvoke(null, null); break; } } }