你敢来帮我回答下嘛!
public class SSHmain
{
public static void main(String[] args)
{
String hostname = "192.168.1.1";//要登陆目标主机
String username = "lbb";//登陆用的用户名
String password = "cisco"; //登陆用到的密码
try
{
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* Now connect */
conn.connect();
/* Authenticate */
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new
IOException("Authentication failed.");
/* Create a session */
Session sess = conn.openSession();
//sess.execCommand("uname -a && date && uptime && who");
sess.execCommand("show");
System.out.println("Here is some information about the remote host:");
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);
}
}
}
怎么才能执行多条命令,为什么我调用第二个execCommand()方法总是决绝连接,我想用这个远程登入到交换机
------最佳解决方案--------------------我敢,但是,这个我真不会啊
------其他解决方案--------------------
-0 -我搞不定啊,为什么不能执行多条,大神你有其他ssh登入的方法吗
------其他解决方案--------------------用个SSH软件登陆、不用自己手写