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. nrf51822-广播模式

    解决以下几个问题: 1 SDK9 中的几种广播模型 2 广播超时如何进入睡眠 3 如何取消广播超时睡眠使其可以无限广播. 1 SDK9 中的几种广播模型 Nordci SDK对于广播方面有一个模块.这 ...

  2. wampserver

  3. ubuntu Nodejs和npm的安装

     cnpm install -g XXX errors :  npm i --registry=https://registry.npm.taobao.org     标签: nodejsnpm 20 ...

  4. UI---startup--jquery

    http://www.w3school.com.cn 传统的基于表单提交, 整页刷新式的并不需要前端MVC. 当 然这种体验会很糟糕.试想一下, 用WebQQ时,每发一次消息页面就要泛白一次, 这是什 ...

  5. rpc rmi http

    1.RPC与RMI (1)RPC 跨语言,而 RMI只支持Java. (2)RMI 调用远程对象方法,允许方法返回 Java 对象以及基本数据类型,而RPC 不支持对象的概念,传送到 RPC 服务的消 ...

  6. openssh-server 安装

    sudo apt-get update sudo apt-get install openssh-server 1:ssh-keygen -t rsa -f ~/.ssh/id_rsa 这里会提示输入 ...

  7. ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'

    ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click ...

  8. Weak Pair---hud5877大连网选(线段树优化+dfs)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5877  题意:给你一颗树,有n个节点,每个节点都有一个权值v[i]:现在求有多少对(u,v ...

  9. glusterfs rebalance

    # gluster volume rebalance VOLNAME start # gluster volume rebalance VOLNAME status # gluster volume ...

  10. Foundation和CoreFoundation之间的转换

    Foundation是OC的东西,CoreFoundation是C语言的东西 eg: NSString\NSArray\NSDictionary 属于Foundation CFStringRef\CF ...