- 浏览: 22409 次
- 性别:
- 来自: 长沙
最新评论
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;
}
}
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;
}
}
相关推荐
【telnet检测工具】是一种常用于网络管理员和IT专业人员的实用工具,它允许用户远程登录到另一台设备,通过命令行界面进行通信。在给定的上下文中,这个工具特别设计为并发telnet检测器,能够同时对多个IP地址和端口...
批量Ping和Telnet检测是网络管理员常用的两种网络连通性检查工具,它们在IT领域中扮演着重要角色,尤其在大型网络环境中。批量检测能够大大提高工作效率,帮助快速定位网络问题。 **批量Ping检测** 批量Ping检测是...
Telnet 服务是一种远程登录服务,允许用户从远程主机登录到 Linux 服务器。下面将详细介绍如何开启 Linux 的 Telnet 服务。 一、基本概念 Linux 提供服务是由运行在后台的守护程序(daemon)来执行的。守护进程的...
### Windows下bat批处理脚本使用telnet批量检测远程端口详解 本文旨在详细介绍如何在Windows环境下使用bat批处理脚本结合telnet命令批量检测远程服务器的端口状态。通过这种方式,用户可以在本地环境中轻松检查远程...
然而,在实际应用中,我们常常需要对多个主机进行Telnet连接,以检测这些主机的端口是否开放。因此,本文将介绍Linux下批量并行Telnet对端端口的实现方法。 1. 使用Telnet命令 Telnet命令是Linux系统中的一种远程...
它允许用户通过TCP/IP协议访问远程主机,仿佛坐在那台主机前操作一样。尽管telnet提供了远程控制的功能,但它并不安全,因为所有的通信都是明文传输,易受窃听和中间人攻击。因此,在现代网络环境中,telnet通常被更...
Telnet服务是Linux操作系统中一个常用的远程登录服务,通过telnet客户端程序可以连接到远程主机,并进行登录和远程控制。本文将详细讲解Linux_telnet服务的开启步骤,包括安装telnet软件包、启动telnet服务、测试...
2. **端口扫描**:尝试连接到目标主机的常用 Telnet 端口(通常是 23 号端口)。 3. **响应分析**:如果连接成功,表示该设备可能启用了 Telnet 服务。 4. **报告生成**:收集并记录所有开放的 Telnet 服务信息,供...
telnet默认情况下不允许root用户登入Linux主机。若要允许root用户登入,可以通过编辑/etc/pam.d/login文件或将/etc/securetty文件重命名为/etc/securetty.bak来实现。 七、安全的SSH telnet服务存在安全漏洞,因此...
2. **Windows Telnet高级入侵教程及原理(2) - 共享世纪.mht**:此文件很可能是关于利用telnet进行网络安全测试或渗透测试的高级教程,包括telnet的工作原理以及如何利用它来检测或攻击系统漏洞。MHT文件是Web档案,...
默认情况下,出于安全考虑,root用户无法通过Telnet登录Linux主机。若需允许root用户通过Telnet登录,可通过注释掉`/etc/pam.d/login`文件中的一行或移动`/etc/securetty`文件来实现。但强烈建议不要这样做,而是在...
4. **Telnet报文**:如果文件中包含Telnet报文,我们会在端口23上看到用户与远程主机的交互,包括键盘输入和服务器响应。由于Telnet是明文传输,所以可以清晰地看到命令和返回信息。 5. **错误检测**:通过...
#### 三、Telnet 服务检测 一旦 Telnet 服务安装完成,就需要检查其状态并进行必要的配置。 1. **查看 Telnet 服务的状态**: ```bash # chkconfig --list | grep telnet ``` 2. **启用 Telnet 服务**: ```...
本文将深入探讨如何使用批处理(BAT)脚本来实现通过telnet进行自动登录并执行ping命令,以此来远程检测网络连通性。这在监控服务器、网络故障排查或自动化测试场景下尤为常见。 首先,让我们理解一下涉及的三个...
默认情况下,不允许root用户直接使用telnet登录Linux主机。可以使用以下方法来允许root用户登录: [root@wljs root]# vi /etc/pam.d/login #auth required pam_securetty.so 将这一行加上注释! 或者: [root@...
IP则负责将数据包从源主机发送到目标主机。在telnet实现中,我们需要理解和使用TCP的连接建立(三次握手)、数据传输和连接关闭(四次挥手)过程。 在C语言实现telnet客户端时,开发者通常会使用以下步骤: 1. **...
- **连接远程服务器**:通过输入`telnet [主机名或IP地址]`命令,即可尝试建立连接。例如,`telnet 192.168.0.1`。 - **身份验证**:连接成功后,系统会提示输入登录用户名和密码,这是访问远程服务器的凭证。 - **...
本文档将详细介绍如何在Linux系统中批量并行使用Telnet对端端口,实现批量检测远程主机的端口状态。 一、前言 Telnet是一个常用的远程登录工具,通常用于远程连接服务器、路由器、交换机等设备。但是,使用Telnet...
在Linux或Unix系统中,telnet-server通常是默认安装的一部分,提供了一个简单的命令行接口,让用户能够执行远程主机上的命令。然而,由于telnet协议的数据传输是明文的,不加密,因此安全性较低,不推荐用于处理敏感...