你敢来帮我回答下嘛!
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软件登陆、不用自己手写
[其他解释]
ls说得可以啊!
[其他解释]
难道你是要在项目里面用 通过项目调用这个ssh登陆去执行命令或者做其他什么?