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

📄 subnet.java

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

import java.util.List;
import java.util.Vector;

import sitech.www.frame.jdbc.SqlInsert;
import sitech.www.frame.jdbc.SqlQuery;

import com.sitech.net.topo.CommSnmpGet;
import com.sitech.net.topo.util.RandomGUID;
import com.sitech.net.topo.util.netHelper;

public class Subnet {

	private String nodeid = "";

	private String ip = "";

	public void saveToDB(String nodeid, String ip) {
		this.nodeid = nodeid;
		this.ip = ip;
		netHelper nethelper = new netHelper();

		// 填充子网信息
		String route_table_sql = "select iprouteifindex,iproutedest,iproutemask,iproutenexthop from tb_topo_iproutetable where iproutetype='3' and nodeid='"
				+ nodeid + "'";
		List rt_list = SqlQuery.findList(route_table_sql);
		System.out.println("[NodeID: " + nodeid + " IpAddress: " + ip
				+ " SubNet信息记录" + rt_list.size() + "条]");
		for (int j = 0; j < rt_list.size(); j++) {
			String[] rt_item = (String[]) rt_list.get(j);
			String ifindex = rt_item[0];
			String routedest = rt_item[1];
			String routemask = rt_item[2];
			String nexthop = rt_item[3];
			String netnum = nethelper.getNetNum(routedest, routemask);
			String sub_net_sql = "insert into tb_topo_subnet (nodeid,subnetid,subnetaddr,subnetmask,nexthop) values ('"
					+ nodeid
					+ "','"
					+ ifindex
					+ "','"
					+ netnum
					+ "','"
					+ routemask + "','" + nexthop + "')";
			SqlInsert.insert(sub_net_sql);
		}
		System.out.println("[NodeID: " + nodeid + " IpAddress: " + ip
				+ " SubNet入库完毕]");
	}

	public Vector processSubNet() {
		// 处理子网部分
		System.out.println("[处理NodeID: " + nodeid + " IpAddress: " + ip
				+ " 子网信息]");
		Vector vector = new Vector();
		// 子网中的switch节点无重复的入库到info表
		// 不是已发现的节点所有ip,不是自身地址ip
		String switch_sql = "select ipaddr from tb_topo_ipnettomediatable where h_type='3' and nodeid='"
				+ this.nodeid
				+ "' and ipaddr not in (select moni_ipaddr from tb_topo_node_info) and ipaddr not in (select ipaddr from tb_topo_ipaddrtable)";

		// String switch_sql = "select ipaddr from tb_topo_ipnettomediatable
		// where h_type='3' and nodeid='"
		// + ID
		// + "' and ipaddr not in (select ipaddr from tb_topo_ipaddrtable)";
		// System.out.println("---" + switch_sql + "---");
		List switch_list = SqlQuery.findList(switch_sql);
		for (int i = 0; i < switch_list.size(); i++) {
			String[] item = (String[]) switch_list.get(i);
			String ipaddr = item[0];
			// 检测IP地址是否有响应
			boolean isAdoptable = true;
			String noanswer_sql = "select * from tb_topo_noanswer where ipaddress='"
					+ ipaddr + "'";
			List noanswer_list = SqlQuery.findList(noanswer_sql);
			if (noanswer_list.size() > 0) {
				isAdoptable = false;
			} else {
				CommSnmpGet snmpGet = new CommSnmpGet(ipaddr, 161, "ahnms2008");
				snmpGet.init();
				isAdoptable = snmpGet.isAdoptable();
				snmpGet.release();
				if (!isAdoptable) {
					SqlInsert
							.insert("insert into tb_topo_noanswer (ipaddress) values ('"
									+ ipaddr + "')");
				}
			}

			if (isAdoptable) {
				// switch处理
				RandomGUID myGUID = new RandomGUID();
				String switch_nodeid = myGUID.toString();

				List list11 = SqlQuery
						.findList("select moni_ipaddr from tb_topo_node_info where moni_ipaddr='"
								+ ipaddr + "'");
				if (!(list11.size() > 0)) {
					String switch_insert = "insert into tb_topo_node_info (node_id,node_type,moni_ipaddr,moni_port,moni_community,process_flag,root_flag,discover_nodeid,info_proc_flag,connect_type) values ('"
							+ switch_nodeid
							+ "','switch','"
							+ ipaddr
							+ "','161','ahnms2008','0','0','"
							+ nodeid
							+ "','0','3')";
					SqlInsert.insert(switch_insert);
					vector.add(switch_nodeid);
				}

				// List list111 = SqlQuery
				// .findList("select peeripaddr from tb_topo_routerconnect where
				// peeripaddr='"
				// + ipaddr
				// + "' and nodeid='"
				// + ID
				// + "' and localipaddr='" + IP + "'");
				// if (!(list111.size() > 0)) {
				// String switch_insert1 = "insert into tb_topo_routerconnect
				// (nodeid,localipaddr,peernodeid,peeripaddr) values ('"
				// + ID
				// + "','"
				// + IP
				// + "','"
				// + switch_nodeid
				// + "','"
				// + ipaddr + "')";
				// SqlInsert.insert(switch_insert1);
				// }
			}
		}

		System.out.println("[NodeID: " + nodeid + " IpAddress: " + ip
				+ " 子网信息处理完毕]");
		return vector;
	}
}

⌨️ 快捷键说明

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