java 运程连接 linux
2013-10-31 22:46
190 查看
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; public class FirstConnectSSH { public static void main(String[] args) { String hostname = ""; String username = ""; String password = ""; 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("cd /root; ls -l"); InputStream stdout = new StreamGobbler(sess.getStdout()); InputStream stderr = new StreamGobbler(sess.getStderr()); BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout)); BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr)); System.out.println("Here is the output from stdout:"); while (true) { String line = stdoutReader.readLine(); if (line == null) break; System.out.println(line); } System.out.println("Here is the output from stderr:"); while (true) { String line = stderrReader.readLine(); if (line == null) break; System.out.println(line); } /* Close this session */ sess.close(); /* Close the connection */ conn.close(); } catch (IOException e) { e.printStackTrace(System.err); System.exit(2); } } }
body{font-family: Courier New, Verdana, Arial, '宋体';font-size:12px;}
相关文章推荐
- jconsole不能远程连接linux下java应用的解决方法
- java连接linux
- Java远程连接Linux系统并执行Linux命令
- java连接linux中mysql出现:Access denied for user 'root'@'192.168.91.1' (using password: YES)
- java 连接 linux redis 失
- linux下用java通过串口连接手机或短信猫发送中文短信的方法
- Linux下java连接sqlserver2005
- JAVA连接Linux
- Java用ssh连接Linux服务报Segmentation fault (core dumped)解决
- linux下redis后台运行连接Java代码
- 本地Java项目连接Linux系统上的Redis
- java连接远程linux的redis
- JMX配置Tomcat 7.0连接Linux java监控
- java连接linux里的redis数据库报:Could not get a resource from the pool解决方法
- 阿里云服务器连接以及centos 搭建 web java环境(linux java部署 tomcat部署)
- JAVA本地远程连接linux程序监控状态
- linux下搭建redis单机版+java连接测试
- Linux下 Redis 4.0安装 及Java使用Jedis连接
- Linux 下使用Java连接 mysql
- jconsole不能远程连接linux下java应用的解决方法