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()方法总是决绝连接,我想用这个远程登入到交换机
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()方法总是决绝连接,我想用这个远程登入到交换机
解决方案 »
- 一表格中含有迭代若干个checkbox 如何点击删除将所有checked传入action
- 求一个java ee做的网上商城源码和数据库文件,简单点就行
- struts1 hibernate3.0 spring2.0 整合报The requested resource is not available.404
- 求个java 时间比较的要能比较时分秒的
- 问个问题,关于JAVA中类做为方法参数的问题
- 谁能告诉我Struts2的标签比html的好在哪里?
- servlet无法正确调用sql server2000的存储过程?
- 急,关于struts显示的问题,一个循环
- STRUTS框架中,如何从数据库表中取数据(100条)?300分
- 高分求用jbuilder 构建的Struts例子 最好是Weblogic 做server
- httpClient如何利用会话继续访问
- 高分求ssh整合jbpm5
难道你是要在项目里面用 通过项目调用这个ssh登陆去执行命令或者做其他什么?