问题描述
最近在用ssh2远程Shell脚本执行工具写了一个代码测试执行普通的命令都可以比如“ls”啦什么的都可以但是执行动态的就不可以比如“top”也没有错误只是返回的是“”。求大家帮忙呀!下面是代码main是测试用的是root的权限packagecom.syxp.sjyw.util;importjava.io.IOException;importjava.io.InputStream;importjava.nio.charset.Charset;importch.ethz.ssh2.Connection;importch.ethz.ssh2.Session;importcom.syxp.sjyw.model.ScheduledTask;/***远程Shell脚本执行工具**@authorAdministrator*/publicclassRemoteShellToolimplementsSSHHelper{privateConnectionconn;privateStringipAddr;privateStringcharset=Charset.defaultCharset().toString();privateStringuserName;privateStringpassword;privateStringcmd;publicRemoteShellTool(){}publicRemoteShellTool(StringipAddr,StringuserName,Stringpassword,Stringcharset){this.ipAddr=ipAddr;this.userName=userName;this.password=password;if(charset!=null){this.charset=charset;}}/***登录远程Linux主机**@return*@throwsIOException*/publicbooleanlogin()throwsIOException{conn=newConnection(ipAddr);conn.connect();//连接returnconn.authenticateWithPassword(userName,password);//认证}/***执行Shell脚本或命令**@paramcmds命令行序列*@return*/publicStringexec(Stringcmds){InputStreamin=null;Stringresult="";try{if(this.login()){Sessionsession=conn.openSession();//打开一个会话session.execCommand(cmds);in=session.getStdout();result=this.processStdout(in,this.charset);conn.close();}}catch(IOExceptione1){e1.printStackTrace();}returnresult;}/***解析流获取字符串信息**@paramin输入流对象*@paramcharset字符集*@return*/publicStringprocessStdout(InputStreamin,Stringcharset){byte[]buf=newbyte[1024];StringBuffersb=newStringBuffer();try{while(in.read(buf)!=-1){sb.append(newString(buf,charset));}}catch(IOExceptione){e.printStackTrace();}returnsb.toString();}publicstaticvoidmain(String[]args){RemoteShellToolest=newRemoteShellTool("192.168.248.128","root","woaiwojia","utf-8");try{if(est.login()){System.out.println(est.exec("top"));//System.out.println(est.exec("exit"));}else{System.out.println("***连接失败***");}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}publicStringexecCmd(StringipAddr,Stringusername,Stringpassword,Stringcharset,Stringcmd){this.ipAddr=ipAddr;this.userName=username;this.password=password;this.charset=charset;if(charset!=null){this.charset=charset;}try{if(login()){Strings=exec(cmd);exec("exit");returns;}else{//System.out.println("连接失败");return"***连接失败***";}}catch(IOExceptione){e.printStackTrace();return"***SSH异常***";}}publicStringexecCmd(ScheduledTaskst){this.ipAddr=st.getIp();this.userName=st.getUsername();this.password=st.getPassword();if(charset!=null){this.charset=st.getCharset();}try{if(login()){Strings=exec(st.getCmd());exec("exit");returns;}else{//System.out.println("连接失败");return"***连接失败***";}}catch(IOExceptione){e.printStackTrace();return"***SSH异常***";}}publicStringgetIpAddr(){returnipAddr;}publicvoidsetIpAddr(StringipAddr){this.ipAddr=ipAddr;}publicStringgetCharset(){returncharset;}publicvoidsetCharset(Stringcharset){this.charset=charset;}publicStringgetUserName(){returnuserName;}publicvoidsetUserName(StringuserName){this.userName=userName;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicStringgetCmd(){returncmd;}publicvoidsetCmd(Stringcmd){this.cmd=cmd;}}
解决方案
解决方案二:
该回复于2011-05-13 08:54:46被版主删除