Java调用snmpwalk获得路由器ARP表
2007-09-06 00:14
183 查看
入口类:Fetch.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());}
}
}
配置文件类: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;
}
}
对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();
}
}
核心功能类: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++
9281
) {
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;
}
}
/*
* 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;
}
}
对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();
}
}
核心功能类: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++
9281
) {
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或C#调用命令行批处理命令如何获得返回值
- java调用shell脚本,并获得结果集的例子
- java调用shell脚本,并获得结果集的例子
- Java 调用 MySQL 存储过程并获得返回值
- android java获得root权限调用linux命令
- Java调用MySQL存储过程并获得返回值的方法
- Crosswalk+Cordova开发安卓app之 JavaScript调用java (附源代码下载)
- flex 中通过 RO 调用一个java写的Bean的时候获得IP地址的方法 .也就是获得session 和 request的方法
- java调用shell脚本,并获得结果集的例子
- Java调用shell脚本并获得结果
- Java调用shell脚本并获得结果
- java调用shell脚本,并获得结果集的例子
- 【转】用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获得值!
- java语句调用getprop和setprop命令来获得prop中的数据
- java获得当前方法是在哪里被调用的
- android java获得root权限调用linux命令
- 【转】java中调用cmd命令(cmd /c 和 cmd/k)获得当前目录绝对路径
- Java代码中调用shell脚本和python脚本并获得输出结果(分为小数据量和大数据量)
- java调用中国天气网api获得天气预报信息的方法