package org.huangxf.snmp.test;

import java.io.IOException;
import java.util.List; import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.PDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils; public class ipTable {
public static void main(String[] args) {
ipTable.collectInterface();
} // 服务器接口集合
public static void collectInterface() {
TransportMapping transport = null;
Snmp snmp = null;
CommunityTarget target;
String[] IF_OIDS = { "1.3.6.1.2.1.2.2.1.1", // Index
"1.3.6.1.2.1.2.2.1.2", // descr
"1.3.6.1.2.1.2.2.1.3", // type
"1.3.6.1.2.1.2.2.1.5", // speed
"1.3.6.1.2.1.2.2.1.6", // mac
"1.3.6.1.2.1.2.2.1.7", // adminStatus
"1.3.6.1.2.1.2.2.1.8", // operStatus "1.3.6.1.2.1.2.2.1.10", // inOctets
"1.3.6.1.2.1.2.2.1.16", // outOctets
"1.3.6.1.2.1.2.2.1.14", // inError
"1.3.6.1.2.1.2.2.1.20", // outError
"1.3.6.1.2.1.2.2.1.13", // inDiscard
"1.3.6.1.2.1.2.2.1.19", // outDiscard
"1.3.6.1.2.1.2.2.1.11", // inUcastPkts
"1.3.6.1.2.1.2.2.1.17", // outUcastPkts
"1.3.6.1.2.1.2.2.1.12", // inNUcastPkts
"1.3.6.1.2.1.2.2.1.18" };// outNUcastPkts
String[] IP_OIDS = { "1.3.6.1.2.1.4.20.1.1", // ipAdEntAddr
"1.3.6.1.2.1.4.20.1.2", // ipAdEntIfIndex
"1.3.6.1.2.1.4.20.1.3" ,// ipAdEntNetMask
"1.3.6.1.2.1.4.20.1.4" ,//ipAdentBcastAddr
"1.3.6.1.2.1.4.20.1.5" };//ipAdEntReasmMaxSize
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
snmp.listen();
target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setRetries();
target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));
target.setTimeout();
target.setVersion(SnmpConstants.version2c);
TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {
@Override
public PDU createPDU(Target arg0) {
PDU request = new PDU();
request.setType(PDU.GET);
return request;
}
});
OID[] columns = new OID[IF_OIDS.length];
for (int i = ; i < IF_OIDS.length; i++)
columns[i] = new OID(IF_OIDS[i]);
@SuppressWarnings("unchecked")
List<TableEvent> list = tableUtils.getTable(target, columns, null, null);
if (list.size() == && list.get().getColumns() == null) {
System.out.println(" null");
} else {
for (TableEvent event : list) {
VariableBinding[] values = event.getColumns();
if (values == null)
continue;
System.out.println("interface ---Index:" + values[].getVariable().toString() + " descr:"
+ " type:"
+ values[].getVariable().toString() + " speed:" + values[].getVariable().toString()
+ " mac:"
+ values[].getVariable().toString() + " operStatus:"
+ values[].getVariable().toString());
// System.out.println("interface ---Index:" + values[0].getVariable().toString() + " descr:"
// + getChinese(values[1].getVariable().toString()) + " type:"
// + values[2].getVariable().toString() + " speed:" + values[3].getVariable().toString()
// + " mac:" + getChinese(values[4].getVariable().toString()) + " adminStatus:"
// + values[5].getVariable().toString() + " operStatus:"
// + values[6].getVariable().toString());
}
}
// 获取ip
OID[] ipcolumns = new OID[IP_OIDS.length];
for (int i = ; i < IP_OIDS.length; i++)
ipcolumns[i] = new OID(IP_OIDS[i]);
@SuppressWarnings("unchecked")
List<TableEvent> iplist = tableUtils.getTable(target, ipcolumns, null, null);
if (iplist.size() == && iplist.get().getColumns() == null) {
System.out.println(" null");
} else {
for (TableEvent event : iplist) {
VariableBinding[] values = event.getColumns();
if (values == null)
continue;
System.out.println(" IP--->ipAdEntAddr:" + values[].getVariable().toString() + " ipAdEntIfIndex:"
+ values[].getVariable().toString() + " ipAdEntNetMask:"
+ values[].getVariable().toString() + " 3:" + values[].getVariable().toString()
+ " 4:"
+ values[].getVariable().toString() );
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (transport != null)
transport.close();
if (snmp != null)
snmp.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

snmp getTable demo :iftable ipAddresstable的更多相关文章

  1. JAVA8中Predicate,Consumer,UnaryOperator,Function接口的应用

    笔者平时时间有限,直接贴代码,关于几个接口的差别,可以查看这两篇文章 感受lambda之美,推荐收藏,需要时查阅 https://juejin.im/post/5ce66801e51d455d850d ...

  2. SNMP

    net-snmp 了解snmp程序最好的工具,snmpwalk和snmptable都是关键命令,举例: snmptable -v 2c -c public X.X.X.X ifTable 显示网络接口 ...

  3. SNMP 原理与实战详解

    原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...

  4. SNMP OID列表 监控需要用到的OID

    zabbix的snmp监控还没开始讲,不过先给大家列一些snmp常用的一些OID,比如cpu.内存.硬盘什么的.先了解这些,在使用snmp监控服务器. 系统参数(1.3.6.1.2.1.1) OID ...

  5. jFinal中报对应模型不存在的错误(The Table mapping of model: demo.User not exists)

    jFinal中报对应模型不存在的错误(The Table mapping of model: demo.User not exists) 贴出错误: java.lang.RuntimeExceptio ...

  6. Linux服务器SNMP常用OID (转)

    原文地址:http://www.haiyun.me/archives/linux-snmp-oid.html 收集整理一些Linux下snmp常用的OID,用做服务器监控很不错. 服务器负载: 1 2 ...

  7. T4 代码生成 Demo (抽奖程序)

    参考自这位大狮的:  https://github.com/Pencroff/Dapper-DAL/blob/master/Dapper-DAL/Models/ModelGenerator.tt 项目 ...

  8. zabbix SNMP OID列表

    系统参数(1.3.6.1.2.1.1) OID 描述 备注 请求方式 .1.3.6.1.2.1.1.1.0 获取系统基本信息 SysDesc GET .1.3.6.1.2.1.1.3.0 监控时间 s ...

  9. snmp模拟器snmpsid使用

    snmpsim使用 安装 pip install snmpsim 简单使用 生成snmpwalk文件: snmpwalk -v2c -c 'password' -ObentU 218.200.x.15 ...

随机推荐

  1. CC2540的使用入门

    目录 1. 介绍 2. 开发环境 3. SDCC 1. 介绍 CC2540是一款2.4GHz Bluetooth® low energy SOC,基于8051 MCU 首先,你需要硬件设备 笔者的开发 ...

  2. Qt Model/View(官方翻译,图文并茂)

    http://doc.trolltech.com/main-snapshot/model-view-programming.html 介绍 Qt 4推出了一组新的item view类,它们使用mode ...

  3. 基于vs2005以上版本Qt程序发布的注意事项(讲了manifest的问题)

    最近发现了一个非常恼人的程序deployment的问题,估计大家有可能也会遇到,特此memo. 问题的出现我觉得主要还是微软搞的花头太多, 一个不知所谓的manifest文件让本来简单的程序发布变得困 ...

  4. php--validate错误信息提示样式

    //validate  错误信息提示样式  可以提示错误信息 可以使用jq 自带的属性改变错误的显示的位置,其中element是验证未通过的当前表单元素,error为错误后的提示信息 [注意]:放的位 ...

  5. CSS之position

    1.当元素使用了position:relative或者position:absolute才能激活 top  left  等属性的使用! 2.使用了position后,display无论设置了什么值都会 ...

  6. QFile文件操作-QT

    #include <QCoreApplication> #include<QFile> #include<QString> #include<QDebug&g ...

  7. c#导出excel(转)

    C#导出Excel文件实例代码 2010-08-03 14:10:36|  分类: 软件编程 |  标签:excel  c#导出excel   |字号大中小 订阅 /// <summary> ...

  8. js获取网站根目录

    //js获取网站根路径(站点及虚拟目录),获得网站的根目录或虚拟目录的根地址         function getRootPath(){        var strFullPath=window ...

  9. Ubuntu下如何将普通用户提升到root权限

    在ubuntu的系统操作中,我们经常会使用到系统权限的,因为权限不足,导致在一些操作当中非常麻烦.要获取权限,最长使用的方法就是使用sudo指令,但是来回使用还是比较麻烦,有没有直接提升我们的用户权限 ...

  10. js工具类大全

    /********** 日期处理函数 *********/<script type="text/javascript" src="${springMacroRequ ...