ssh登陆后,执行命令不能返回结果,且 getExitStatus 返回为1

feihooone 2008-11-19 03:46:13
情况这样的 我直接用此程序登陆LINUX 系统是没问题的,可以将命令的执行结果返回,且 sess.getExitStatus 的返回是0 正常

但我用此程序登陆 一个设备,操作系统是 Welcome to MontaVista Linux 3.0, Professional Edition
无论执行什么命令都返回下面这样的欢迎信息,不知道是为什么?
而且 sess.getExitStatus 的返回是1


cli 2.1.2
Login from 221.10.44.138 port:22
Press TAB anytime, CLI will help you to finish the command line,
or gives the available keywords.
If you firstly use CLI, you can try "get" command.
For example:
set wlan o(press TAB)
you will get the following:
set wlan operationmode
and press TAB again to see what you will get!


找了一个程序,但每次都返回的是欢迎信息,并没将“get system cpu usage” 结果返回到系统,请高手帮忙分析下。

程序代码:
try
{
/* Create a connection instance */

Connection conn = new Connection(hostname);

/* Now connect */

conn.connect();

/* Authenticate.
* If you get an IOException saying something like
* "Authentication method password not supported by the server at this stage."
* then please check the FAQ.
*/

boolean isAuthenticated = conn.authenticateWithPassword(username, password);

if (isAuthenticated == false)
throw new IOException("Authentication failed.");

/* Create a session */

Session sess = conn.openSession();

sess.execCommand("get system cpu usage");
System.out.println("Here is some information about the remote host:");

/*
* This basic example does not handle stderr, which is sometimes dangerous
* (please read the FAQ).
*/

InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}

/* Show exit status, if available (otherwise "null") */

System.out.println("ExitCode: " + sess.getExitStatus());

/* Close this session */

sess.close();

/* Close the connection */

conn.close();

}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(2);
}
...全文
1739 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fosjos 2008-11-20
  • 打赏
  • 举报
回复
嵌入式?不能用普通的环境测试,最好有第三方提供的jar包,就像mobile开发
boboo_2000_0 2008-11-20
  • 打赏
  • 举报
回复
没做过,这应该和系统有关系。
在java里可以通过Ganymed SSH-2, Expect4j等实现ssh登录,由于Ganymed SSH-2是纯java实现,并且可以实现scp,sftp等,因此我们采用Ganymed SSH-2 1> 下载安装 从http://code.google.com/p/ganymed-ssh-2/ 下载,我们用的是ganymed-ssh2-build251beta1.zip。 在eclipse里新建一个测试工程,并将解压后的ganymed-ssh2-build251beta1.jar拷到工程的lib目录下,然后在工程属性的java build path里添加这个jar的library。 2> ssh 将解压后的examples目录下的Basic.java 拷到工程的src目录,编译运行以后可以在控制台看到输出结果。除了执行一条命令,也可以执行一个shell脚本。 例如将Basic.java中的“uname -a && date && uptime && who” 改为“/home/lss/test.sh”, test.sh的内容如下: #! /bin/sh echo "testing shell"; ls; 在eclipse里运行以后的结果为: Here is some information about the remote host: testing shell 1 test.sh ExitCode: 0 需要注意的是在一个session里只能执行一次命令,因此如果想在server上执行多个命令,要么打开多个session,要么在一个session里去执行一个shell脚本,shell脚本里去执行多个命令。 每次执行完以后,如果正确将返回ExitCode: 0,因此程序里可以通过sess.getExitStatus()来判断命令是否被正确执行。 3> scp 首先在程序里import ch.ethz.ssh2.SCPClient; 然后通过下面的方法来实现: SCPClient scpClient = conn.createSCPClient(); scpClient.put("localFiles", "remoteDirectory"); //从本地复制文件到远程目录 scpClient.get("remoteFiles","localDirectory"); //从远程获取文件 例如: scpClient.put("D:\\localTest.txt", "/home/bill/"); 需要注意的是windows的本地目录要用双斜杠来分隔目录。 scpClient.put("/home/bill/remoteTest.txt", "D:\\"); 4> sftp 首先在程序里import ch.ethz.ssh2.SFTPv3Client; SFTPv3Client sftpClient = new SFTPv3Client(conn); sftpClient.mkdir("newRemoteDir", 0755); //远程新建目录 ,第二个参数是创建的文件夹的读写权限 sftpClient.rmdir("oldRemoteDir"); //远程删除目录 另外还有创建删除文件,读写文件等接口,参见http://www.ganymed.ethz.ch/ssh2/javadoc/ch/ethz/ssh2/SFTPv3Client.html

67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧