NM实验2-网络分析

NM实验2-网络分析

本实验4学时

实验目的

使用网络分析工具进行网络故障分析

实验内容和步骤

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.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

public class SNMP_MVP {

    public static void main(String[] args) throws IOException, InterruptedException {
        String command = command2("oop.microais.cn", "161", ".1.3.6.1");
        System.out.println(command);
    }

    static String command2(String IP, String port, String command) {
        String[] items = command.split(";");
        OID[] oids = new OID[items.length];
        for (int i = 0; i < items.length; i++) {
            oids[i] = new OID(items[i]);
        }
        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));
            snmpWalk(snmp, oids, communityTarget);
        } catch (IOException ex) {
            Logger.getLogger(SNMP_MVP.class.getName()).log(Level.SEVERE, null, ex);
            result = ex.toString();
        }
        return result;
    }

    private static void snmpWalk(Snmp snmp, OID[] oids, CommunityTarget target) {
        TableUtils tableUtils = new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK));
        List<TableEvent> tableEvents = tableUtils.getTable(target, oids, new OID("0"), new OID("9"));
        int counter = 0;
        for (TableEvent tableEvent : tableEvents) {
            System.out.println(tableEvent);
            counter++;
        }
        System.out.println("counter=" + counter);
    }
}

输出示例

org.snmp4j.util.TableEvent[index=2.1.1.1.0,vbs=[1.3.6.1.2.1.1.1.0 = Linux iZ6adr2x949lk5Z 4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019 x86_64],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.2.0,vbs=[1.3.6.1.2.1.1.2.0 = 1.3.6.1.4.1.8072.3.2.10],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.3.0,vbs=[1.3.6.1.2.1.1.3.0 = 58 days, 21:05:55.38],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.4.0,vbs=[1.3.6.1.2.1.1.4.0 = Me <me@example.org>],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.5.0,vbs=[1.3.6.1.2.1.1.5.0 = iZ6adr2x949lk5Z],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.6.0,vbs=[1.3.6.1.2.1.1.6.0 = Sitting on the Dock of the Bay],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.7.0,vbs=[1.3.6.1.2.1.1.7.0 = 72],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.8.0,vbs=[1.3.6.1.2.1.1.8.0 = 0:00:00.00],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.1,vbs=[1.3.6.1.2.1.1.9.1.2.1 = 1.3.6.1.6.3.11.3.1.1],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.2,vbs=[1.3.6.1.2.1.1.9.1.2.2 = 1.3.6.1.6.3.15.2.1.1],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.3,vbs=[1.3.6.1.2.1.1.9.1.2.3 = 1.3.6.1.6.3.10.3.1.1],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.4,vbs=[1.3.6.1.2.1.1.9.1.2.4 = 1.3.6.1.6.3.1],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.5,vbs=[1.3.6.1.2.1.1.9.1.2.5 = 1.3.6.1.6.3.16.2.2.1],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.6,vbs=[1.3.6.1.2.1.1.9.1.2.6 = 1.3.6.1.2.1.49],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.7,vbs=[1.3.6.1.2.1.1.9.1.2.7 = 1.3.6.1.2.1.4],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.8,vbs=[1.3.6.1.2.1.1.9.1.2.8 = 1.3.6.1.2.1.50],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.9,vbs=[1.3.6.1.2.1.1.9.1.2.9 = 1.3.6.1.6.3.13.3.1.3],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.2.10,vbs=[1.3.6.1.2.1.1.9.1.2.10 = 1.3.6.1.2.1.92],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.3.1,vbs=[1.3.6.1.2.1.1.9.1.3.1 = The MIB for Message Processing and Dispatching.],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.3.2,vbs=[1.3.6.1.2.1.1.9.1.3.2 = The management information definitions for the SNMP User-based Security Model.],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.3.3,vbs=[1.3.6.1.2.1.1.9.1.3.3 = The SNMP Management Architecture MIB.],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.3.4,vbs=[1.3.6.1.2.1.1.9.1.3.4 = The MIB module for SNMPv2 entities],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.3.5,vbs=[1.3.6.1.2.1.1.9.1.3.5 = View-based Access Control Model for SNMP.],status=0,exception=null,report=null]
org.snmp4j.util.TableEvent[index=2.1.1.9.1.3.6,vbs=[1.3.6.1.2.1.1.9.1.3.6 = The MIB module for managing TCP implementations],status=0,exception=null,report=null]

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

# snmpwalk -v 2c -c wl2019  localhost .1.3.6.1
SNMPv2-MIB::sysDescr.0 = STRING: Linux iZ6adr2x949lk5Z 4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019 x86_64
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (509156616) 58 days, 22:19:26.16
SNMPv2-MIB::sysContact.0 = STRING: Me <me@example.org>
SNMPv2-MIB::sysName.0 = STRING: iZ6adr2x949lk5Z
SNMPv2-MIB::sysLocation.0 = STRING: Sitting on the Dock of the Bay
SNMPv2-MIB::sysServices.0 = INTEGER: 72
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORID.1 = OID: SNMP-MPD-MIB::snmpMPDCompliance
SNMPv2-MIB::sysORID.2 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORID.3 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.4 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID.6 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.7 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.8 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.9 = OID: SNMP-NOTIFICATION-MIB::snmpNotifyFullCompliance
SNMPv2-MIB::sysORID.10 = OID: NOTIFICATION-LOG-MIB::notificationLogMIB
SNMPv2-MIB::sysORDescr.1 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.2 = STRING: The management information definitions for the SNMP User-based Security Model.
SNMPv2-MIB::sysORDescr.3 = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.5 = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORDescr.6 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.7 = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr.8 = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr.9 = STRING: The MIB modules for managing SNMP Notification, plus filtering.
SNMPv2-MIB::sysORDescr.10 = STRING: The MIB module for logging SNMP Notifications.
SNMPv2-MIB::sysORUpTime.1 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.2 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.3 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.4 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.5 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.6 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.7 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.8 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.9 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.10 = Timeticks: (0) 0:00:00.00

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值