snmp getTable demo :iftable ipAddresstable
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的更多相关文章
- JAVA8中Predicate,Consumer,UnaryOperator,Function接口的应用
		笔者平时时间有限,直接贴代码,关于几个接口的差别,可以查看这两篇文章 感受lambda之美,推荐收藏,需要时查阅 https://juejin.im/post/5ce66801e51d455d850d ... 
- SNMP
		net-snmp 了解snmp程序最好的工具,snmpwalk和snmptable都是关键命令,举例: snmptable -v 2c -c public X.X.X.X ifTable 显示网络接口 ... 
- SNMP 原理与实战详解
		原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ... 
- SNMP OID列表 监控需要用到的OID
		zabbix的snmp监控还没开始讲,不过先给大家列一些snmp常用的一些OID,比如cpu.内存.硬盘什么的.先了解这些,在使用snmp监控服务器. 系统参数(1.3.6.1.2.1.1) OID ... 
- jFinal中报对应模型不存在的错误(The Table mapping of model: demo.User not exists)
		jFinal中报对应模型不存在的错误(The Table mapping of model: demo.User not exists) 贴出错误: java.lang.RuntimeExceptio ... 
- Linux服务器SNMP常用OID (转)
		原文地址:http://www.haiyun.me/archives/linux-snmp-oid.html 收集整理一些Linux下snmp常用的OID,用做服务器监控很不错. 服务器负载: 1 2 ... 
- T4 代码生成 Demo (抽奖程序)
		参考自这位大狮的: https://github.com/Pencroff/Dapper-DAL/blob/master/Dapper-DAL/Models/ModelGenerator.tt 项目 ... 
- 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 ... 
- snmp模拟器snmpsid使用
		snmpsim使用 安装 pip install snmpsim 简单使用 生成snmpwalk文件: snmpwalk -v2c -c 'password' -ObentU 218.200.x.15 ... 
随机推荐
- CC2541连接BTool教程
			一.简介 本篇介绍如何基于Smart RF(主芯片CC2541).Smart RF(主芯片CC2540).Usb Dongle,来使用软件BTool. 本篇暂时只介绍如何连接,不介绍如何使用BTool ... 
- java jmx
			http://blog.csdn.net/qiao000_000/article/details/6063949 一.JMX简介 什么是JMX?在一篇网文中是这样说的:"JMX(Java M ... 
- RocEDU.阅读.写作
			RocEDU.阅读.写作 一.选择图书 <黑客大曝光> 二.读书计划 56天内学习完.时间:2016.01.25--2016.03.20 三.承诺 宋宸宁郑重承诺: 1.在56天内(开始时 ... 
- Vmware安装与VMware下Linux系统安装
			源文件地址:http://www.cnblogs.com/lclq/p/5619271.html 1.下载安装VMware,我安装的是VMware 12.VMware从11开始不再支持32位系统,32 ... 
- Array原型链添加“遍历”方法
			<script> //1.在我们之前的项目里向原型链中集成方法时大多代码分析不严密,有时间我在这里会做详细分析; Array.prototype.each = function(fn) { ... 
- Tornado-简介
			定义 Tornado全称为Tornado Web Server,是一个用Python写的Web服务器和应用框架,最终由FriendFeed公司使用,后来FriendFeed被Facebook收购之后, ... 
- c#上传文件(二)使用文件流保存文件
			1.html代码: <asp:FileUpload runat="server" ID="UpLoadFile"/> <asp:Button ... 
- linux命令之echo
			echo可以用来输出显示变量和常量,还可以用来写文件. 最简单的方式:使用 echo 命令 #echo abcbedf>>a.txt 将abcdef追加到a.txt文件末尾 往文件中写入内 ... 
- iOS苹果开发者客服电话地址
			苹果开发者客服电话地址:https://developer.apple.com/contact/phone.php 中国大陆地区客服电话:4006 701 855 中国香港地区客服电话:(852) 2 ... 
- SpringMVC 406  accept请求错误,没有加入将json序列化的包
			在spring中使用@responsebody发现请求报406错误 jar包中缺少json序列化的相关的包! 
