`
redheart_2006
  • 浏览: 21819 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类

telnet 检测主机

    博客分类:
  • java
阅读更多
package org.zw.excel;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.telnet.TelnetClient;

public class MachineTelnetInfo {

	private  int time = 1500;

	private  String IP = "10.17.7.137"; // telnet的IP地址

	public MachineTelnetInfo(String iP) {
		super();
		IP = iP;
	}
	private  String port = "23"; // 端口号,默认23

	private  String user = "root";// 用户名
	
	private  List rowsInfo = null;// new ArrayList();// 解析出来的信息

	public MachineTelnetInfo(String iP, String user, String pwd) {
		super();
		IP = iP;
		this.user = user;
		this.pwd = pwd;
	}
	public MachineTelnetInfo() {
		super();
	}
	private  String pwd = "root88"; // 用户密码


	//private  char startTag = '[';// 系统标示符号
	private  char endTag = '#';// 系统标示符号

	private TelnetClient tc = null;

	private BufferedReader in; // 输入流,接收返回信息

	private PrintStream out; // 像 服务器写入 命令

	public void connect() throws InterruptedException {
		try {
			tc = new TelnetClient();
			tc.connect(IP, Integer.parseInt(port));
			in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
			// in = tc.getInputStream();
			out = new PrintStream(tc.getOutputStream());

		} catch (Exception e) {
			System.out.println("connect error !");
		}
		System.out.println("服务器IP:"+IP+" success?" + tc.isConnected());
		out.println(user); // 是发送到服务器上的请求信息
		out.flush();
		Thread.sleep(time);
		out.println(pwd);
		out.flush();
	}

	public String excute(String command) throws InterruptedException,
			IOException {
		Thread.sleep(1500);
		out.println(command);
		out.flush();
		int i = 0;
		StringBuffer sb = new StringBuffer();
		try {
			char ch = (char) in.read();
			while (true) {
				if (i == 1)
					sb.append(ch);
				if (ch == endTag)
					i++;
				if (i > 1)
					break;
				ch = (char) in.read();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public List getInfo() throws Exception {
		if(rowsInfo==null){//obtain this machine information by protocol of telnet
			this.connect();
			String str = this.excute("df -g");
			InputStream is = new ByteArrayInputStream(str.getBytes());   
			ParseMachineInfo it = new ParseMachineInfo();
			rowsInfo = it.readToList(is);
			close();
		}
		return rowsInfo;
	}
	public static void main(String[] args) throws Exception {
		MachineTelnetInfo tt = new MachineTelnetInfo("10.17.7.129");
		List list = tt.getInfo();
		
		for (int i = 0; i < list.size(); i++) {
			String[] strs =(String[])(list.get(i));
			for (int j = 0; j < strs.length; j++) {
				System.out.print(strs[j]+"|");
			}
			System.out.println();
		}
	}
	public static void main1(String[] args) throws InterruptedException,
			IOException {
		MachineTelnetInfo tt = new MachineTelnetInfo();
		tt.connect();
		String str = tt.excute("df -g");
		System.out.println("----------------finsh---------------------------");
		//System.out.println(str);
		InputStream is = new ByteArrayInputStream(str.getBytes());   
		ParseMachineInfo it = new ParseMachineInfo();
		tt.rowsInfo = it.readToList(is);
		for (int i = 0; i < tt.rowsInfo.size(); i++) {
			String[] strs =(String[])(tt.rowsInfo.get(i));
			for (int j = 0; j < strs.length; j++) {
				System.out.print(strs[j]+"|");
			}
			System.out.println();
		}
		tt.close();
	}

	public void close() {
		try {
			tc.disconnect();
			in.close();
			// out.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}

 

package org.zw.excel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelView {
	
	private String[] widths;
	private String targetfile = "F:/out.xls ";// 输出的excel文件名
	private String worksheet = "主机列表检查"; // 输出的excel文件工作表名
	WritableSheet sheet = null;// 工作表
	
	WritableCellFormat alarmrows = null;
	WritableCellFormat lightalarmrows = null;
	WritableCellFormat colsrow = null;
	WritableCellFormat headrow = null;
	
	private int row = 0 ;
	//String[] IPs = {"10.17.7.131","10.17.7.132"};
	//private String[] machinenames = {"OSS_SERV1","OSS_SERV2"};
	String[] IPs = {"10.17.7.131","10.17.7.132","10.17.7.137","10.17.7.138","10.17.7.139","10.17.7.140","10.17.7.141"
			,"10.17.7.133","10.17.7.134","10.17.7.135","10.17.7.136","10.17.7.148","10.17.7.149"};

	private String[] machinenames = {"OSS_SERV1","OSS_SERV2","OSS_WEB1","OSS_WEB2","OSS_COMM1","OSS_COMM2","Test"
			,"NMS_SERV1","NMS_SERV2","NMS_APP1","NMS_APP2","cognosDB","TMS(root/linus123)"} ;
	Label label;
	WritableWorkbook workbook;
	String[] titles;
	
	public void init() throws WriteException, IOException{
		WritableFont alarmfont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
		 alarmrows = new WritableCellFormat(alarmfont);
		alarmrows.setBackground(Colour.RED);
		lightalarmrows = new WritableCellFormat(alarmfont); 
		lightalarmrows.setBackground(Colour.YELLOW2);
		WritableFont colsfont = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
		 colsrow = new WritableCellFormat(colsfont);
		colsrow.setBackground(Colour.GRAY_50);
		
		WritableFont _font = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
		headrow = new WritableCellFormat(_font);
		headrow.setBackground(Colour.LIGHT_BLUE);
		
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String _date = formatter.format(date) ;
		targetfile = "F:/主机检查_"+_date+".xls";
		
		// 创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下
		System.out.println("begin");
		OutputStream os = new FileOutputStream(targetfile);
		workbook = Workbook.createWorkbook(os);
		sheet = workbook.createSheet(worksheet, 0); // 添加第一个工作表
		setColSpace();
		
		Date date0 = new Date();
		SimpleDateFormat _formatter = new SimpleDateFormat("yyyy-MM-dd");
		String __date = _formatter.format(date0) ;
		//String[] _colums = { "Filesystem", "Mounted","GB_blocks", "Free","%Used",__date,"Remarks" }; // excel工作表的标题
		String[] _colums = { "Filesystem", "GB_blocks", "Free","%Used","Iused","%Iused","Mounted",__date,"Remarks" }; // excel工作表的标题
		titles = _colums;
	}
	
	private void setColSpace() {
		if(widths==null)return;
		for (int i = 0; i < widths.length; i++) {
			String[] strs = widths[i].split(",");
			//System.out.println(strs[0]+":"+strs[1]);
			sheet.setColumnView(Integer.parseInt(strs[0]), Integer.parseInt(strs[1]));
		}
	}
	public void setWidths(String[] widths) {
		this.widths = widths;
	}

	public static void main(String[] args) throws WriteException, IOException {
		ExcelView ev = new ExcelView();
		String[] widths = { "0,17", "1,13","2,10","3,10", "4,12","5,10","6,18","7,14","8,30"};
		ev.setWidths(widths);
		
		ev.init();
		for (int i = 0; i < ev.IPs.length; i++){ 
			try {
				ev.generateView(i);
			} catch (Exception e) {
				System.out.println("IP--"+ev.IPs[i]+"有错误");
				continue;
			}
		
			ev.row =  ev.row+3;
			}
		
		ev.workbook.write();
		ev.workbook.close();
//---------------------------//--------------------------		
		Runtime r = Runtime.getRuntime();
		Process p = null;
		String cmd[] = {"C:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE",ev.targetfile };
		try {
			p = r.exec(cmd);
		} catch (Exception e) {
			System.out.println("error executing: " + cmd[0]);
		}
	}
/*	//还有个功能要做,就是实现与上回的对比功能
	public void generateView1(int server){
		try {
			//head
				 sheet.mergeCells(0, row, 3, row);
				 label =new Label(0, row,machinenames[server]+"——"+IPs[server],headrow);
				 sheet.addCell(label);
				 row++;
			//columns
			for (int i = 0; i < titles.length; i++) {
				// Label(列号,行号 ,内容 )
				label = new Label(i, row, titles[i],colsrow); // put the title in row1
				sheet.addCell(label);
			}
			row++;
			//body
			int _row = row+7;
			while (row < _row) {
				// Label(列号,行号 ,内容 )
				for (int _col = 0; _col < titles.length; _col++) {
					label = new Label(_col, row, row+"行"+_col+"列"); // put the title in row1
					sheet.addCell(label);
				}
				if(row==5)	
					for (int col = 0; col < titles.length; col++) {
					label = new Label(col, row, row+"行"+col+"列",alarmrows); // put the title in row1
					sheet.addCell(label);
				}
				if(row==6)	
					for (int col = 0; col < titles.length; col++) {
					label = new Label(col, row, "3.145"); // put the title in row1
					sheet.addCell(label);
				}
				if(row==3)	
					for (int col = 0; col < titles.length; col++) {
					label = new Label(col, row, "29.00%"); // put the title in row1
					sheet.addCell(label);
				}
				row++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("end");
	}*/
	
	//还有个功能要做,就是实现与上回的对比功能
	public void generateView(int server){
		try {
			//head
				 sheet.mergeCells(0, row, 3, row);
				 label =new Label(0, row,machinenames[server]+"——"+IPs[server],headrow);
				 sheet.addCell(label);
				 row++;
			//columns
			for (int i = 0; i < titles.length; i++) {
				// Label(列号,行号 ,内容 )
				label = new Label(i, row, titles[i],colsrow); // put the title in row1
				sheet.addCell(label);
			}
			row++;
			//body
			MachineTelnetInfo tt = null;
			if(IPs[server].equals("10.17.7.149"))tt = new MachineTelnetInfo(IPs[server],"root","linus123");
			else tt = new MachineTelnetInfo(IPs[server]);
			List list = tt.getInfo();
			for (int i = 0; i < list.size(); i++) {
				switch (isAlarm(getContent(list,i,3))) {
				case 0:
					for (int _col = 0; _col < titles.length; _col++) {
						if(_col<7){
							label = new Label(_col, row, getContent(list,i,_col)); // put the title in row1
							sheet.addCell(label);}
							else if(_col==7){
								label = new Label(_col, row, " OK "); // put the title in row1
								sheet.addCell(label);
							} else {
								label = new Label(_col, row, " "); // put the title in row1
								sheet.addCell(label);
							}
					}
					break;
	            case 1:
					for (int _col = 0; _col < titles.length; _col++) {
						if(_col<7){
						label = new Label(_col, row, getContent(list,i,_col),lightalarmrows); // put the title in row1
						sheet.addCell(label);}
						else if(_col==7){
							label = new Label(_col, row, " OK ",lightalarmrows); // put the title in row1
							sheet.addCell(label);
						} else {
							label = new Label(_col, row, " ",lightalarmrows); // put the title in row1
							sheet.addCell(label);
						}
					}
					break;
                case 2:
                	// Label(列号,行号 ,内容 )
    				for (int _col = 0; _col < titles.length; _col++) {
    					if(_col<7){
    					label = new Label(_col, row, getContent(list,i,_col),alarmrows); // put the title in row1
    					sheet.addCell(label);}
    					else if(_col==7){
    						label = new Label(_col, row, " XX ",alarmrows); // put the title in row1
    						sheet.addCell(label);
    					} else {
    						label = new Label(_col, row, " ",alarmrows); // put the title in row1
    						sheet.addCell(label);
    					}
    				}
					break;
				default:
					break;
				}
				row++;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		//System.out.println("end");
	}
	
	public String getContent(List list,int row,int col){
	   String[] strs =(String[])list.get(row);
		String content = strs[col];
		return content;
	}
	public int pivot = 80;
	public int isAlarm(String percent){
		int level = 0;//0 normal 1 light_alarm  2 alarm
		int i = 0;
		try {
			i = Integer.parseInt(percent.substring(0, percent.indexOf('%')));
			if(i>=60)level =1;
			if(i>=80)level = 2;
		} catch (Exception e) {
			return 0;
		}
		return level;
	}
}

 

写道
package org.zw.excel;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ParseMachineInfo {

/**
* 1. 演示将流中的文本读入一个 StringBuffer 中
*
* @throws IOException
*/
public void readToBuffer(StringBuffer buffer, InputStream is)
throws IOException {
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
String[] strs = parseLine(line);
line = reader.readLine(); // 读取下一行
}
}

public List readToList(InputStream is)
throws IOException {
String line; // 用来保存每行读取的内容
List list = new ArrayList();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
String[] strs = parseLine(line);
if (strs != null)
list.add(strs);
line = reader.readLine(); // 读取下一行
}
return list;
}

public static void main(String[] args) throws IOException {
ParseMachineInfo it = new ParseMachineInfo();
// StringBuffer buffer = new StringBuffer();
InputStream is = new FileInputStream("E:\\1.txt");
//it.readToBuffer(buffer, is);
//System.out.println(buffer.toString());
List list = it.readToList(is);
for (int i = 0; i < list.size(); i++) {
String[] strs =(String[])(list.get(i));
for (int j = 0; j < strs.length; j++) {
System.out.print(strs[j]+"|");
}
System.out.println();
}
}

public String[] parseLine(String line) {
if (line.startsWith("Filesystem") || line.endsWith("#"))
return null;
// System.out.println("分解" + line);
line = line.replaceAll("\\s+", ",");
String[] strs = line.split(",");
return strs;
}
}

 

分享到:
评论

相关推荐

    Windows下bat批处理脚本使用telnet批量检测远程端口小记

    主要介绍了Windows下bat批处理脚本使用telnet批量检测远程端口小记,需要的朋友可以参考下

    nc和telnet工具

    nc是一款很不错的网络检测工具,以下是详细使用。 'nc.exe -h'即可看到各参数的使用方法。 基本格式:nc [-options] hostname port[s] [ports] ...  nc -l -p port [options] [hostname] [port] -d 后台模式 -e...

    Win7下telnet使用

    这两天一直在倒腾自己的网络课程设计,题目是本地以及远程主机端口开闭检测,很自然的想到了使用telnet,但在实际操作中发现win7下的telnet很是恶心,竟然无法使用。这里总结一下经验,给出win7下开启和使用telnet的...

    网络安全课程设计(1).doc

    4、利用Telnet连接,为对方主机设定Telnet服务Mytel 利用Telnet连接和相关的软件,在虚拟机中设立一个服务名称是Mytel的服务,且该服务 为开机自启动服务,服务实际巡行程序为Telnet关闭原有的Telnet服务。...

    在远程路由器或远程主机上检测服务器端口是否可以访问

    在远程路由器或远程主机上检测服务器端口是否可以访问,判断网络配置是否正确,需要用命令telnet 服务器IP 端口, 大部分时候即使配置正确但telnet时是不能退出的或需要很长的响应时间才退出。如果服务器上运行了...

    Windows下常用网络命令的运用和比较

    如果在浏览某个网页时迟迟得不到回应,用户可以通过向该主机发出一个试探性的IP检测包(相当于声纳脉冲),来测试该主机是否可以到达。同时Ping返回了丰富的资料,用户借此可以了解从本机到达对方主机的速度和该主机...

    网络刺客II(查找主机共享资源)

    这里集成了一些相关的网络工具,包括有IP 主机名转换器、Finger客户查询工具、主机端口扫描工具以及主机查找器、域名查找器、Telnet客户端程序等,在工具箱中还可以查看网络状态(netstat)和自己的IP地址,通过这些...

    网络刺客2软件下载网络刺客2软件下载帮助你搜索共享主机

    四、工具箱 这里集成了一些相关的网络工具,包括有IP 主机名转换器、Finger客户查询工具、主机端口扫描工具以及主机查找器、域名查找器、Telnet客户端程序等,在工具箱中还可以查看网络状态(netstat)和自己的IP...

    关于漏洞扫描原理与技术

    通过与目标主机TCP/IP端口建立连接并请求某些服务(如TELNET、FTP等),记录目标主机的应答,搜集目标主机相关信息(如匿名用户是否可以登录等),从而发现目标主机某些内在的安全漏洞。对某一类漏洞进行检查的程序...

    TK2006(漏洞扫描)比流光X-SCAN还好的扫描工具

    传统的漏洞扫描器(比如:...11、UDP端口检测:检测对方主机打开的UPD端口. 12、木马种植机:向对方主机植入文件. 注意:软件的部分溢出漏洞模块可能被某些杀毒软视为病毒杀掉,实际对本机无危害.木软件绝对没有恶意代码.

    漏洞扫描器TK2006

    传统的漏洞扫描器(比如:...11、UDP端口检测:检测对方主机打开的UPD端口. 12、木马种植机:向对方主机植入文件. 注意:软件的部分溢出漏洞模块可能被某些杀毒软视为病毒杀掉,实际对本机无危害.木软件绝对没有恶意代码.

    强大的局域网扫描工具,可以扫描端口,IP,MAC等信息

    管理和控制是通过telnet实现的,如果对方没有开启telnet服务,只要符合入侵条件本工具都可强行进行telnet登陆。 3.可以对局域网主机发送文字消息(需要双方都开启Messager服务)。 4.快速关闭或重启局域网内的主机。

    SqlServer1433端口入侵检测工具

    这是网上目前流行的针对mssql的入侵工具,首先进行半开式扫描,扫出开放1433端口的主机之后,再用xscan进行弱口令猜解,最后恢复管理员删除的存储过程,上传...mssql使用者可以用来检测自己的数据库是否能被轻易入侵

    T.K 2006(漏洞扫描).rar

    11、UDP端口检测:检测对方主机打开的UPD端口. 12、木马种植机:向对方主机植入文件. 注意:软件的部分溢出漏洞模块可能被某些杀毒软视为病毒杀掉,实际对本机无危害.木软件绝对没有恶意代码. 软件的登录口令是:空/空;...

    网络安全测试方案.pdf

    第1章 内网安全测试 1.1 基础安全 端口隔离 MAC 安全 IP 绑定 ARP 绑定 1.2 身份认证 AAA 802.1x PPPOE Potal、CTP、认证代理 1.3 内网准入控制(盈高解决方案) Pc 状态检测() 主机外设控制 第2章 安全设备测试...

    最强最小最高效的扫描器HScan

    多线程方式对指定IP段(指定主机),或主机列表进行检测.GUI和命令行两种方式. 扫描项目: -name --- 获取主机名; -port --- 默认端口扫描; -ftp --- FTP Banner,匿名用户,弱口令账号扫描; -ssh --- SSH Banner...

    科来网络分析系统 v4.06.172

    6. 实时检测所有主机的流量。 二. 分析功能 1. 支持数据包解码与协议分析,集中于应用层协议 (如 POP3、SMTP、HTTP、TELNET、FTP 等) ,以 HEX 视图和 ASCII 视图同时对照显示分析结果,结果数据实时更新。 2. 重组...

    hscan120.zip

    多线程方式对指定IP段(指定主机),或主机列表进行检测.GUI和命令行两种方式. 扫描项目: -name --- 获取主机名; -port --- 默认端口扫描; -ftp --- FTP Banner,匿名用户,弱口令账号扫描; -ssh --- SSH Banner扫描...

    cmd操作命令和linux命令大全收集

    telnet 在本机上直接键入telnet 将进入本机的telnet copy 路径文件名1 路径文件名2 /y 复制文件1到指定的目录为文件2,用参数/y就同时取消确认你要改写一份现存目录文件 copy c:srv.exe ipadmin$ 复制本地c:srv....

Global site tag (gtag.js) - Google Analytics