NM实验1-网络监视

    xiaoxiao2023-11-21  50

    NM实验1-网络监视

    本实验2学时

    实验目的

    使用网络监视工具进行网络流量监视

    实验内容和步骤

    snmp4j编程示例

    import java.io.IOException; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.event.ResponseEvent; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.UdpAddress; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; public class SNMP_MVP { public static String command(String IP, String port, String command) { String result = ""; try { Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); snmp.listen(); CommunityTarget communityTarget = new CommunityTarget(); communityTarget.setCommunity(new OctetString("wl2019")); communityTarget.setVersion(SnmpConstants.version2c); communityTarget.setAddress(new UdpAddress(IP + "/" + port)); PDU pdu = createGetPdu(command); result = sendRequest(snmp, pdu, communityTarget); } catch (IOException ex) { Logger.getLogger(SNMP_MVP.class.getName()).log(Level.SEVERE, null, ex); result = ex.toString(); } return result; } private static PDU createGetPdu(String command) { PDU pdu = new PDU(); pdu.setType(PDU.GET); String[] oids = command.split(";"); for (String oid : oids) { pdu.add(new VariableBinding(new OID(oid))); } return pdu; } private static String sendRequest(Snmp snmp, PDU pdu, CommunityTarget target) throws IOException { ResponseEvent responseEvent = snmp.send(pdu, target); PDU response = responseEvent.getResponse(); StringBuilder result = new StringBuilder(); if (response == null) { System.out.println("TimeOut..."); return result.toString(); } if (response.getErrorStatus() == PDU.noError) { List<VariableBinding> bindingList = response.getBindingList(new OID("1")); bindingList.forEach((vb) -> { result.append(vb.toString()).append(" ,").append(vb.getVariable().getSyntaxString()).append("\n"); }); } else { System.out.println("Error:" + response.getErrorStatusText()); } return result.toString(); } public static void main(String[] args) throws IOException, InterruptedException { String command = command("oop.microais.cn", "161", "1.3.6.1.2.1.1.3.0;1.3.6.1.2.1.2.2.1.11.2;1.3.6.1.2.1.2.2.1.17.2;"); System.out.println(command); } }

    输出示例

    1.3.6.1.2.1.1.3.0 = 56 days, 23:28:18.26 ,TimeTicks 1.3.6.1.2.1.2.2.1.11.2 = 469873 ,Counter 1.3.6.1.2.1.2.2.1.17.2 = 3199997 ,Counter

    对比在目标机上直接执行snmp客户端命令的结果:

    # snmpwalk -v 2c -c wl2019 localhost .1.3.6.1.2.1.2.2.1.17.2 IF-MIB::ifOutUcastPkts.2 = Counter32: 3149327 # snmpwalk -v 2c -c wl2019 localhost .1.3.6.1.2.1.2.2.1.11.2 IF-MIB::ifInUcastPkts.2 = Counter32: 462657 # snmpwalk -v 2c -c wl2019 localhost .1.3.6.1.2.1.2.2.1.2 IF-MIB::ifDescr.1 = STRING: lo IF-MIB::ifDescr.2 = STRING: Red Hat, Inc Device 0001 IF-MIB::ifDescr.3 = STRING: Red Hat, Inc Device 0001 # snmpwalk -v 2c -c wl2019 localhost .1.3.6.1.2.1.1.3.0 DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (490098793) 56 days, 17:23:07.93
    最新回复(0)