redheart_2006 发表于 2013-1-28 18:53:42

telnet 检测主机

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 {privateint time = 1500;privateString IP = "10.17.7.137"; // telnet的IP地址public MachineTelnetInfo(String iP) {super();IP = iP;}privateString port = "23"; // 端口号,默认23privateString user = "root";// 用户名privateList rowsInfo = null;// new ArrayList();// 解析出来的信息public MachineTelnetInfo(String iP, String user, String pwd) {super();IP = iP;this.user = user;this.pwd = pwd;}public MachineTelnetInfo() {super();}privateString pwd = "root88"; // 用户密码//privatechar startTag = '[';// 系统标示符号privatechar 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 telnetthis.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+"|");}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+"|");}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.split(",");//System.out.println(strs+":"+strs);sheet.setColumnView(Integer.parseInt(strs), Integer.parseInt(strs));}}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+"有错误");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);}}/*//还有个功能要做,就是实现与上回的对比功能public void generateView1(int server){try {//head sheet.mergeCells(0, row, 3, row); label =new Label(0, row,machinenames+"——"+IPs,headrow); sheet.addCell(label); row++;//columnsfor (int i = 0; i < titles.length; i++) {// Label(列号,行号 ,内容 )label = new Label(i, row, titles,colsrow); // put the title in row1sheet.addCell(label);}row++;//bodyint _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 row1sheet.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 row1sheet.addCell(label);}if(row==6)for (int col = 0; col < titles.length; col++) {label = new Label(col, row, "3.145"); // put the title in row1sheet.addCell(label);}if(row==3)for (int col = 0; col < titles.length; col++) {label = new Label(col, row, "29.00%"); // put the title in row1sheet.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+"——"+IPs,headrow); sheet.addCell(label); row++;//columnsfor (int i = 0; i < titles.length; i++) {// Label(列号,行号 ,内容 )label = new Label(i, row, titles,colsrow); // put the title in row1sheet.addCell(label);}row++;//bodyMachineTelnetInfo tt = null;if(IPs.equals("10.17.7.149"))tt = new MachineTelnetInfo(IPs,"root","linus123");else tt = new MachineTelnetInfo(IPs);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 row1sheet.addCell(label);}else if(_col==7){label = new Label(_col, row, " OK "); // put the title in row1sheet.addCell(label);} else {label = new Label(_col, row, " "); // put the title in row1sheet.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 row1sheet.addCell(label);}else if(_col==7){label = new Label(_col, row, " OK ",lightalarmrows); // put the title in row1sheet.addCell(label);} else {label = new Label(_col, row, " ",lightalarmrows); // put the title in row1sheet.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;return content;}public int pivot = 80;public int isAlarm(String percent){int level = 0;//0 normal 1 light_alarm2 alarmint 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;}} 
<div class="quote_title">写道
页: [1]
查看完整版本: telnet 检测主机