⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipaddrtable.java

📁 一个java编写的拓扑自动发现程序
💻 JAVA
字号:
package com.sitech.net.topo.table;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import org.snmp4j.smi.VariableBinding;

import sitech.www.frame.jdbc.SqlInsert;

import com.sitech.net.topo.CommSnmpGet;
import com.sitech.net.topo.disc.TopoDiscover;

public class IpAddrTable {

	public static final String ROOT_OID = "1.3.6.1.2.1.4.20.1.1";

	public static final int ATTRIBCOUNT = 5;

	private String ID;

	private String IP;

	Vector rowVector = new Vector();

	private void addRow(IpAddrTableRow row) {
		rowVector.add(row);
	}

	public void FillTable(String ip, int port, String community, String id) {
		this.ID = id;
		this.IP = ip;
		CommSnmpGet snmpGet = new CommSnmpGet(ip, port, community);
		snmpGet.init();
		snmpGet.setRootOid(ROOT_OID);
		try {
			snmpGet.workTable(ROOT_OID);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("[IP: " + ip + " 采集AddressTable失败]");
		}
		HashMap hm = snmpGet.getWalkHashMap();
		Set set = hm.keySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			String value = (String) hm.get(key);
			// 填充IP地址
			IpAddrTableRow row = new IpAddrTableRow();
			row.setRowIndex(key);
			row.setIpAddr(value);
			this.addRow(row);
		}

		try {
			for (int i = 0; i < rowVector.size(); i++) {
				IpAddrTableRow row = (IpAddrTableRow) rowVector.elementAt(i);
				String rowIndex = row.getRowIndex();
				Vector oidsVector = getOtherOids(rowIndex, ATTRIBCOUNT);
				Vector resultVector = snmpGet.getPDU(oidsVector);

				for (int j = 0; j < resultVector.size(); j++) {
					VariableBinding vb = (VariableBinding) resultVector.get(j);
					String oid = vb.getOid().toString();
					String var = vb.getVariable().toString();
					if (oid.startsWith("1.3.6.1.2.1.4.20.1.2.")) {
						row.setIfIndex(var);
					} else if (oid.startsWith("1.3.6.1.2.1.4.20.1.3.")) {
						row.setNetMask(var);
					} else if (oid.startsWith("1.3.6.1.2.1.4.20.1.4.")) {
						row.setBcastAddr(var);
					} else if (oid.startsWith("1.3.6.1.2.1.4.20.1.5.")) {
						row.setReasmMaxSize(var);
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("[IP: " + ip + " 采集AddressTable失败]");
		}
		snmpGet.release();
		System.out.println("[IP: " + ip + " AddressTable采集完毕]");
	}

	private Vector getOtherOids(String rowIndex, int attribCount) {
		Vector vector = new Vector();
		for (int i = 2; i <= attribCount; i++) {
			String oid = releaseRootOidLast(ROOT_OID, rowIndex, i);
			vector.add(oid);
		}
		return vector;
	}

	private String releaseRootOidLast(String rootOid, String rowIndex, int int_i) {
		String result = "";
		String rowIndexLast = rowIndex.substring(rootOid.length(), rowIndex
				.length());
		String rootOidStart = rootOid.substring(0, rootOid.length() - 1);

		result = rootOidStart + int_i + rowIndexLast;
		return result;
	}

	public Vector getRrows() {
		return rowVector;
	}

	public void saveToDB() {
		System.out.println("[NodeID: " + ID + " IpAddress: " + IP
				+ " AddressTable信息记录" + rowVector.size() + "条]");
		for (int i = 0; i < rowVector.size(); i++) {
			IpAddrTableRow row = (IpAddrTableRow) rowVector.get(i);
			String ifIndex = row.getIfIndex();
			String ipAddr = row.getIpAddr();
			String netMask = row.getNetMask();
			String bcastAddr = row.getBcastAddr();
			String reasmMaxSize = row.getReasmMaxSize();
			// noSuchInstance
			if ((!ifIndex.equalsIgnoreCase("noSuchInstance"))
					&& (!ipAddr.equalsIgnoreCase("noSuchInstance"))
					&& (!netMask.equalsIgnoreCase("noSuchInstance"))
					&& (!bcastAddr.equalsIgnoreCase("noSuchInstance"))
					&& (!reasmMaxSize.equalsIgnoreCase("noSuchInstance"))) {
				String sql = "insert into tb_topo_ipaddrtable (NODEID,IFINDEX,IPADDR,NETMASK,BCASTADDR,RASMMAXSIZE) values ('"
						+ ID
						+ "','"
						+ ifIndex
						+ "','"
						+ ipAddr
						+ "','"
						+ netMask
						+ "','"
						+ bcastAddr
						+ "','"
						+ reasmMaxSize
						+ "')";
				SqlInsert.insert(sql);
			}
		}
		System.out.println("[NodeID: " + ID + " IpAddress: " + IP
				+ " AddressTable入库完毕]");
	}

	public void saveToDB(String collStatus) {
		if (collStatus.equals(TopoDiscover.FIRST_COLL)) {
			System.out.println("[NodeID: " + ID + " IpAddress: " + IP
					+ " AddressTable信息记录" + rowVector.size() + "条]");
			for (int i = 0; i < rowVector.size(); i++) {
				IpAddrTableRow row = (IpAddrTableRow) rowVector.get(i);
				String ifIndex = row.getIfIndex();
				String ipAddr = row.getIpAddr();
				String netMask = row.getNetMask();
				String bcastAddr = row.getBcastAddr();
				String reasmMaxSize = row.getReasmMaxSize();
				// noSuchInstance
				if ((!ifIndex.equalsIgnoreCase("noSuchInstance"))
						&& (!ipAddr.equalsIgnoreCase("noSuchInstance"))
						&& (!netMask.equalsIgnoreCase("noSuchInstance"))
						&& (!bcastAddr.equalsIgnoreCase("noSuchInstance"))
						&& (!reasmMaxSize.equalsIgnoreCase("noSuchInstance"))) {
					String sql = "insert into tb_topo_ipaddrtable (NODEID,IFINDEX,IPADDR,NETMASK,BCASTADDR,RASMMAXSIZE,UPDATE_TIME) values ('"
							+ ID
							+ "','"
							+ ifIndex
							+ "','"
							+ ipAddr
							+ "','"
							+ netMask
							+ "','"
							+ bcastAddr
							+ "','"
							+ reasmMaxSize
							+ "',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'))";
					SqlInsert.insert(sql);
				}
			}
			System.out.println("[NodeID: " + ID + " IpAddress: " + IP
					+ " AddressTable入库完毕]");
		} else {

		}
	}

	public static void main(String args[]) throws Exception {
		String ip = "130.30.2.8";
		int port = 161;
		String community = "ahnms2008";

		IpAddrTable ipAddrTable = new IpAddrTable();
		ipAddrTable.FillTable(ip, port, community,
				"04001FFA-7943-A782-FDE6-769683CF268C");
		// ipAddrTable.saveToDB();
		// Vector rowsVector = ipAddrTable.getRrows();
		// for (int i = 0; i < rowsVector.size(); i++) {
		// IpAddrTableRow rows = (IpAddrTableRow) rowsVector.get(i);
		// System.out.println("[RowIndex = " + rows.getRowIndex()
		// + "] [IfIndex = " + rows.getIfIndex() + "] [IpAddr = "
		// + rows.getIpAddr() + "] [NetMask = " + rows.getNetMask()
		// + "] [BcastAddr = " + rows.getBcastAddr()
		// + "] [ReasmMaxSize = " + rows.getReasmMaxSize() + "]");
		// }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -