您的位置:首页 > 编程语言 > Java开发

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;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息