ssh登陆后,执行命令不能返回结果,且 getExitStatus 返回为1
情况这样的 我直接用此程序登陆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);
}