推荐回答
/*我想这就是你想要的telnet吧,既然我下面用的是apache开源包,你下来自己*看看能不能自己重写。方法已经给你提供了。要是可以的话结题吧.哈O∩_∩*/importjava.io.InputStream;importjava.io.PrintStream;importorg.apache.commons.net.telnet.TelnetClient;/***利用apachenet开源包,使用telnet方式获取AIX主机信息*@version1.2*/publicclassNetTelnet{//Telnet对象privateTelnetClienttelnet=newTelnetClient;privateInputStreamin;privatePrintStreamout;//提示符。具体请telnet到AIX主机查看privatecharprompt=''#'';//telnet端口privateStringport;//用户privateStringuser;//密码privateStringpassword;//IP地址privateStringip;publicNetTelnet{try{//AIX主机IPthis.ip="219.243.12.10";this.password="05933663007";this.user="administrator";this.port="23";telnet.connectip,Integer.parseIntport;System.out.println"开始获取输入流...";in=telnet.getInputStream;out=newPrintStreamtelnet.getOutputStream;//登录/*readUntil"login:";writeuser;readUntil"Password:";writepassword;readUntilprompt+"";*/}catchExceptione{e.printStackTrace;}}/***读取分析结果**@parampattern*@return*/publicStringreadUntilStringpattern{try{charlastChar=pattern.charAtpattern.length-1;StringBuffersb=newStringBuffer;charch=charin.read;whiletrue{sb.appendch;ifch==lastChar{ifsb.toString.endsWithpattern{returnsb.toString;}}ch=charin.read;}}catchExceptione{e.printStackTrace;}returnnull;}/***写**@paramvalue*/publicvoidwriteStringvalue{try{out.printlnvalue;out.flush;}catchExceptione{e.printStackTrace;}}/***向目标发送命令字符串**@paramcommand*@return*/publicStringsendCommandStringcommand{try{writecommand;returnreadUntilprompt+"";}catchExceptione{e.printStackTrace;}returnnull;}/***关闭连接**/publicvoiddisconnect{try{telnet.disconnect;}catchExceptione{e.printStackTrace;}}publicstaticvoidmainStringargs{try{System.out.println"开始执行telnet......";NetTelnettelnet=newNetTelnet;//通过aix的命令“查找主机名称”获取数据//命令是"hostname"//不熟悉命令的参考>System.out.println"开始发送hostname命令";Stringresult=telnet.sendCommand"hostname";System.out.println"显示结果";System.out.printlnresult;//最后一定要关闭telnet.disconnect;}catchExceptione{e.printStackTrace;}}。
龚小辉2019-11-05 20:02:20
提示您:回答为网友贡献,仅供参考。