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

feihooone feihooone 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
情况这样的 我直接用此程序登陆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);
}
...全文
评论 2
收藏 已收藏
分享
返回顶部
feihooone
Java EE 67,535 成员 225,843 内容