Java调用snmpwalk获得路由器ARP表
来源:互联网 发布:知乎女神陈大花 编辑:程序博客网 时间:2024/06/03 11:18
入口类:Fetch.java
配置文件类:Config.java
对ARP数据的处理类:FetcherRunner.java
核心功能类:ArpTableFetcher.java
/*
* Main.java
*
* Created on 2007-9-4, 2:25:07
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package arpfetcher;
import java.util.*;
/**
*
* @author signx
*/
public class Fetch {
public Fetch() {
}
public static void main(String[] args) {
int total = 0;
Config config = new Config("target.conf");
ArrayList list = config.readConfig();
SqlDatabase db = null;
try {
ListIterator it = list.listIterator();
while (it.hasNext()) {
String[] s = (String[])it.next();
total += new FetchRunner(s[1],s[2],s[0]).run();
}
}
catch (Exception e) {System.out.println(e.getMessage());}
}
}
* Main.java
*
* Created on 2007-9-4, 2:25:07
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package arpfetcher;
import java.util.*;
/**
*
* @author signx
*/
public class Fetch {
public Fetch() {
}
public static void main(String[] args) {
int total = 0;
Config config = new Config("target.conf");
ArrayList list = config.readConfig();
SqlDatabase db = null;
try {
ListIterator it = list.listIterator();
while (it.hasNext()) {
String[] s = (String[])it.next();
total += new FetchRunner(s[1],s[2],s[0]).run();
}
}
catch (Exception e) {System.out.println(e.getMessage());}
}
}
配置文件类:Config.java
package arpfetcher;
import java.io.*;
import java.util.*;
/**
*
* @author signx
*/
public class Config {
private BufferedReader configFile;
public Config(String fileName) {
try {configFile = new BufferedReader(new FileReader(fileName));}
catch (FileNotFoundException e) {System.out.println(e.getMessage());}
}
public ArrayList readConfig() {
ArrayList list = new ArrayList();
try {
String s = configFile.readLine();
while (s != null) {
String[] c = s.split("/t");
if (!c[0].startsWith("#")) list.add(c);
s = configFile.readLine();
}
return list;
}
catch (Exception e) {System.out.println(e.getMessage());}
return null;
}
}
import java.io.*;
import java.util.*;
/**
*
* @author signx
*/
public class Config {
private BufferedReader configFile;
public Config(String fileName) {
try {configFile = new BufferedReader(new FileReader(fileName));}
catch (FileNotFoundException e) {System.out.println(e.getMessage());}
}
public ArrayList readConfig() {
ArrayList list = new ArrayList();
try {
String s = configFile.readLine();
while (s != null) {
String[] c = s.split("/t");
if (!c[0].startsWith("#")) list.add(c);
s = configFile.readLine();
}
return list;
}
catch (Exception e) {System.out.println(e.getMessage());}
return null;
}
}
对ARP数据的处理类:FetcherRunner.java
package arpfetcher;
import java.util.*;
import java.sql.*;
/**
*
* @author signx
*/
public class FetchRunner {
private ArpTableFetcher fetcher;
private String deviceName;
public FetchRunner(String hostName,String commStr,String name) {
fetcher = new ArpTableFetcher(hostName,commStr);
deviceName = name;
}
public int run() {
System.out.printf("Fetching arp table from %s: ",deviceName);
ArrayList arp = fetcher.getArpTable();
for (int i=0;i<arp.size();i++) {
String[] entry = (String[])arp.get(i);
System.out.printf("%s/t%s/t%s/n",entry[0],entry[1],entry[2]);
}
System.out.printf(" Total %d entries retrieved. /n", arp.size());
return arp.size();
}
}
import java.util.*;
import java.sql.*;
/**
*
* @author signx
*/
public class FetchRunner {
private ArpTableFetcher fetcher;
private String deviceName;
public FetchRunner(String hostName,String commStr,String name) {
fetcher = new ArpTableFetcher(hostName,commStr);
deviceName = name;
}
public int run() {
System.out.printf("Fetching arp table from %s: ",deviceName);
ArrayList arp = fetcher.getArpTable();
for (int i=0;i<arp.size();i++) {
String[] entry = (String[])arp.get(i);
System.out.printf("%s/t%s/t%s/n",entry[0],entry[1],entry[2]);
}
System.out.printf(" Total %d entries retrieved. /n", arp.size());
return arp.size();
}
}
核心功能类:ArpTableFetcher.java
package arpfetcher;
import java.io.*;
import java.util.*;
import java.util.regex.*;
/**
*
* @author signx
*/
public class ArpTableFetcher {
private String ifDescrOID = ".1.3.6.1.2.1.2.2.1.2";
private String arpTableOID = ".1.3.6.1.2.1.4.22.1.2";
public String host;
public String community;
private String snmpwalkCmdArpPrefix = "snmpwalk -v 1 -O nq0 -c ";
private String snmpwalkCmdIntPrefix = "snmpwalk -v 1 -O nq -c ";
private String snmpwalkCmdArpTable = null;
private String snmpwalkCmdIntTable = null;
public ArpTableFetcher(String hostName,String commStr) {
host = hostName;
community = commStr;
snmpwalkCmdArpTable = snmpwalkCmdArpPrefix + community + " " + host+" "+arpTableOID;
snmpwalkCmdIntTable = snmpwalkCmdIntPrefix + community + " " + host+" "+ifDescrOID;
}
private String[] parseArpEntry(String str) {
Pattern pattern = Pattern.compile(
"//.1//.3//.6//.1//.2//.1//.4//.22//.1//.2//.(//d+)//.(.+?)//s(.+)");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
String[] result = {matcher.group(1),matcher.group(2),matcher.group(3)};
return result;
}
return null;
}
private String[] parseIntEntry(String str) {
Pattern pattern = Pattern.compile(
"//.1//.3//.6//.1//.2//.1//.2//.2//.1//.2//.(//d+)//s(.+)");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
String[] result = {matcher.group(1),matcher.group(2)};
return result;
}
return null;
}
private String getIntName(ArrayList itTable,String idx) {
for (int i=0;i<itTable.size();i++) {
String[] entry = parseIntEntry(itTable.get(i).toString());
if (entry[0].equals(idx)) return entry[1];
}
return null;
}
public ArrayList getArpTable() {
ArrayList arpList = getSnmpwalkOutput(snmpwalkCmdArpTable);
ArrayList intList = getSnmpwalkOutput(snmpwalkCmdIntTable);
ArrayList arpTable = new ArrayList();
for (int i=0; i<arpList.size(); i++) {
String[] entry = parseArpEntry(arpList.get(i).toString());
entry[0] = getIntName(intList,entry[0].toString());
arpTable.add(entry);
}
arpList = null;
intList = null;
return arpTable;
}
private ArrayList getSnmpwalkOutput(String cmd) {
ArrayList list = new ArrayList();
try {
String outString;
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((outString = reader.readLine()) != null) {
list.add(outString);
}
return list;
}
catch (IOException e) {System.err.println(e.getMessage());}
return null;
}
}
import java.io.*;
import java.util.*;
import java.util.regex.*;
/**
*
* @author signx
*/
public class ArpTableFetcher {
private String ifDescrOID = ".1.3.6.1.2.1.2.2.1.2";
private String arpTableOID = ".1.3.6.1.2.1.4.22.1.2";
public String host;
public String community;
private String snmpwalkCmdArpPrefix = "snmpwalk -v 1 -O nq0 -c ";
private String snmpwalkCmdIntPrefix = "snmpwalk -v 1 -O nq -c ";
private String snmpwalkCmdArpTable = null;
private String snmpwalkCmdIntTable = null;
public ArpTableFetcher(String hostName,String commStr) {
host = hostName;
community = commStr;
snmpwalkCmdArpTable = snmpwalkCmdArpPrefix + community + " " + host+" "+arpTableOID;
snmpwalkCmdIntTable = snmpwalkCmdIntPrefix + community + " " + host+" "+ifDescrOID;
}
private String[] parseArpEntry(String str) {
Pattern pattern = Pattern.compile(
"//.1//.3//.6//.1//.2//.1//.4//.22//.1//.2//.(//d+)//.(.+?)//s(.+)");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
String[] result = {matcher.group(1),matcher.group(2),matcher.group(3)};
return result;
}
return null;
}
private String[] parseIntEntry(String str) {
Pattern pattern = Pattern.compile(
"//.1//.3//.6//.1//.2//.1//.2//.2//.1//.2//.(//d+)//s(.+)");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
String[] result = {matcher.group(1),matcher.group(2)};
return result;
}
return null;
}
private String getIntName(ArrayList itTable,String idx) {
for (int i=0;i<itTable.size();i++) {
String[] entry = parseIntEntry(itTable.get(i).toString());
if (entry[0].equals(idx)) return entry[1];
}
return null;
}
public ArrayList getArpTable() {
ArrayList arpList = getSnmpwalkOutput(snmpwalkCmdArpTable);
ArrayList intList = getSnmpwalkOutput(snmpwalkCmdIntTable);
ArrayList arpTable = new ArrayList();
for (int i=0; i<arpList.size(); i++) {
String[] entry = parseArpEntry(arpList.get(i).toString());
entry[0] = getIntName(intList,entry[0].toString());
arpTable.add(entry);
}
arpList = null;
intList = null;
return arpTable;
}
private ArrayList getSnmpwalkOutput(String cmd) {
ArrayList list = new ArrayList();
try {
String outString;
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((outString = reader.readLine()) != null) {
list.add(outString);
}
return list;
}
catch (IOException e) {System.err.println(e.getMessage());}
return null;
}
}
- Java调用snmpwalk获得路由器ARP表
- 获得ARP表
- 路由器的ARP绑定
- arp基本命令 -- 路由器
- 使用snmpwalk远程获得目标主机的信息
- ARP表是在交换机上还是在路由器上?
- arp免费/arp更新老化 -- 路由器
- 无线路由器ARP攻击排除
- 获得路由器pin码
- 两个路由器之间静态ARP和代理ARP配置
- MAC地址表(交换机)、ARP缓存表以及路由表(路由器)
- 诺博NB1200+ 路由器解决网吧ARP病毒
- 路由器处理ARP包过程(ZZ)
- ARP/RARP协议报文格式 -- 路由器
- Java 获得方法调用者名称
- Java 获得方法调用者名称
- Java调用shell脚本并获得结果
- Java调用shell脚本并获得结果
- 驱动程序的动态加载
- 在VC中编译、运行程序的小知识点
- 加班感悟~~~
- 使用keytool生成SSL双向认证
- 用javascript进行身份证验证
- Java调用snmpwalk获得路由器ARP表
- 进程链表监视进程是否被创建或者销毁
- Java keytool工具的作用及使用方法
- Hook API监视驱动的加载_ASM
- Mysql日期和时间函数不求人
- 常用log4j配置
- 通过进程链枚举进程_asm
- 列举进程的内核函数ZwQuerySystemInformation _asm
- Kill_Vagaa_Process完整版