Ganymed SSH2 模拟putty远程交互式执行命令工具
来源:互联网 发布:js eval 不能用 编辑:程序博客网 时间:2024/05/16 02:48
接着上篇http://blog.csdn.net/doctor_who2004/article/details/47322105的介绍:
我们模拟下putty这类交互式的执行远程命令:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.nio.charset.StandardCharsets;import java.util.Scanner;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import org.apache.commons.io.IOUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import ch.ethz.ssh2.Connection;import ch.ethz.ssh2.Session;import ch.ethz.ssh2.StreamGobbler;/** * 模拟交互式终端 * * @author doctor * * @time 2015年8月6日 * * @see http://www.programcreek.com/java-api-examples/index.php?api=ch.ethz.ssh2.SCPClient * */public final class SSHAgent2 {private Logger log = LoggerFactory.getLogger(getClass());private Connection connection;private Session session;private BufferedReader stdout;private PrintWriter printWriter;private BufferedReader stderr;private ExecutorService service = Executors.newFixedThreadPool(3);private Scanner scanner = new Scanner(System.in);public void initSession(String hostName, String userName, String passwd) throws IOException {connection = new Connection(hostName);connection.connect();boolean authenticateWithPassword = connection.authenticateWithPassword(userName, passwd);if (!authenticateWithPassword) {throw new RuntimeException("Authentication failed. Please check hostName, userName and passwd");}session = connection.openSession();session.requestDumbPTY();session.startShell();stdout = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout()), StandardCharsets.UTF_8));stderr = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStderr()), StandardCharsets.UTF_8));printWriter = new PrintWriter(session.getStdin());}public void execCommand() throws IOException {service.submit(new Runnable() {@Overridepublic void run() {String line;try {while ((line = stdout.readLine()) != null) {System.out.println(line);}} catch (IOException e) {e.printStackTrace();}}});service.submit(new Runnable() {@Overridepublic void run() {while (true) {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}System.out.print("input:");String nextLine = scanner.nextLine();printWriter.write(nextLine + "\r\n");printWriter.flush();}}});}public void close() {IOUtils.closeQuietly(stdout);IOUtils.closeQuietly(stderr);IOUtils.closeQuietly(printWriter);IOUtils.closeQuietly(scanner);session.close();connection.close();}/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {SSHAgent2 sshAgent = new SSHAgent2();sshAgent.initSession("127.0.0.1", "doctor", "xxxx");sshAgent.execCommand();// sshAgent.close();}}
这里读写流我分别在不同线程下操作,防止读写流之间的阻塞,简单模拟交互环境。当然,登陆的是本地系统,没有安装虚拟机等。
执行结果:
Last login: Thu Aug 6 20:18:52 2015 from 127.0.0.1input:pwd;[doctor@localhost ~]$ pwd;/home/doctor[doctor@localhost ~]$ input:ll[doctor@localhost ~]$ ll总用量 2013760drwxrwxr-x. 3 doctor doctor 4096 7月 27 21:58 android-sdk_r24.3.3-linux-rw-rw-r--. 1 doctor doctor 309109716 7月 26 17:05 android-sdk_r24.3.3-linux.tgzdrwxrwxr-x. 7 doctor doctor 4096 7月 29 00:04 android-studio-rw-rw-r--. 1 doctor doctor 258628239 7月 26 17:01 android-studio-ide-141.1980579-linux.zipdrwxrwxr-x. 3 doctor doctor 4096 7月 5 09:48 appdrwxrwxr-x. 7 doctor doctor 4096 8月 4 22:11 Documentsdrwxrwxr-x. 21 doctor doctor 4096 8月 2 21:22 gitdrwxrwxr-x. 3 doctor doctor 4096 6月 14 12:03 ideaIU-14.1.3-rw-rw-r--. 1 doctor doctor 308609842 6月 13 13:36 ideaIU-14.1.3.tar.gzdrwxrwxr-x. 11 doctor doctor 4096 7月 26 12:26 jstorm-0.9.6.3-rw-rw-r--. 1 doctor doctor 11113536 6月 8 21:19 jstorm-0.9.6.3.zipdrwxrwxr-x. 7 doctor doctor 4096 7月 23 21:24 node_modules-rw-rw-r--. 1 doctor doctor 20063992 7月 11 11:49 node-v0.12.7.tar.gzdrwxrwxrwx. 18 doctor doctor 4096 8月 2 18:10 opt-rw-rw-r--. 1 doctor doctor 466053 7月 28 20:05 pljava-x86_64-unknown-linux-gnu-pg9.1-1.4.3.tar.gz-rwxrwxrwx. 1 doctor doctor 155693 7月 24 21:58 plv8-1.4.4.tar.gzdrwxrwxr-x. 3 doctor doctor 4096 7月 27 23:33 pycharm-professional-4.5.3-rwxrwxrwx. 1 doctor doctor 160688951 7月 27 22:38 pycharm-professional-4.5.3.tar.gzdrwxrwxr-x. 3 doctor doctor 4096 7月 27 23:35 PycharmProjectsdrwxrwxr-x. 3 doctor doctor 4096 6月 9 19:08 spring-tool-suite-3.6.4.RELEASE-e4.4.2-linux-gtk-x86_64-rwxrwxrwx. 1 doctor doctor 417016357 6月 8 22:13 spring-tool-suite-3.6.4.RELEASE-e4.4.2-linux-gtk-x86_64.tar.gzdrwxrwxr-x. 3 doctor doctor 4096 7月 3 21:14 spring-tool-suite-3.7.0.RELEASE-e4.5-linux-gtk-x86_641-rwxrwxrwx. 1 doctor doctor 429456394 7月 3 13:20 spring-tool-suite-3.7.0.RELEASE-e4.5-linux-gtk-x86_641.tar.gzdrwxrwxr-x. 8 doctor doctor 4096 7月 23 22:08 tmpdrwxrwxr-x. 10 doctor doctor 4096 11月 12 2014 v8-3.31.1-rw-rw-r--. 1 doctor doctor 16423158 7月 25 00:28 v8-3.31.1.tar.gzdrwxrwxr-x. 3 doctor doctor 4096 6月 8 20:49 WebStorm-10.0.3-rw-rw-r--. 1 doctor doctor 130207806 6月 8 20:04 WebStorm-10.0.3.tar.gzdrwxrwxr-x. 5 doctor doctor 4096 7月 29 19:51 WebstormProjectsdrwxrwxr-x. 5 doctor doctor 4096 9月 25 2014 zookeeperdrwxrwxr-x. 3 doctor doctor 4096 8月 13 2014 壁纸drwxr-xr-x. 2 doctor doctor 4096 7月 26 17:00 图片drwxr-xr-x. 2 doctor doctor 4096 7月 30 23:34 文档drwxr-xr-x. 2 doctor doctor 4096 8月 5 21:47 下载drwxr-xr-x. 2 doctor doctor 4096 6月 7 21:44 桌面[doctor@localhost ~]$ input:who[doctor@localhost ~]$ whodoctor pts/1 2015-08-06 20:28 (127.0.0.1)[doctor@localhost ~]$ input:who am i[doctor@localhost ~]$ who am idoctor pts/1 2015-08-06 20:28 (127.0.0.1)[doctor@localhost ~]$ input:date[doctor@localhost ~]$ date2015年 08月 06日 星期四 20:29:18 CST[doctor@localhost ~]$ input:
2 0
- Ganymed SSH2 模拟putty远程交互式执行命令工具
- java使用ganymed-ssh2执行linux命令
- Ganymed SSH-2 java执行远程linux机器命令工具
- ganymed-ssh2执行命令返回结果乱码的问题解决
- Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar)
- Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar)
- Ganymed SSH2 模拟类似FileZilla远程传输文件(基于SCP协议)
- java远程调用ssh2执行Linux命令
- ganymed-ssh2
- 使用ssh2 执行远程机器命令 获取执行结果
- python ssh2远程登录节点并执行命令
- php ssh2实现远程上传、下载、执行命令
- 纯JAVA实现远程执行SSH2主机的SHELL命令
- JAVA实现远程执行SSH2主机的SHELL命令
- Linux下使用SSH非交互式远程执行命令脚本
- Linux下使用SSH非交互式远程执行命令脚本
- Linux下使用SSH非交互式远程执行命令脚本
- java使用ganymed-ssh2-build210.jar实现linuxscp命令。
- 浅谈算法和数据结构: 八 平衡查找树之2-3树
- HDOJ-1242-RESCURE(营救公主)【BFS+优先队列】
- android开发笔记之进程通信Messenger
- SYBASE --- all commands
- 浅谈算法和数据结构: 七 二叉查找树
- Ganymed SSH2 模拟putty远程交互式执行命令工具
- iptables 实现流量监控,重定向: 一
- scala实现设计模式之单例设计模式
- UITextField
- 湖南多校对抗赛(D-D)
- Python 之 安装模块的多种方法
- Android复习笔记(14)-自定义控件(1)-滑动开关
- 浅谈算法和数据结构: 六 符号表及其基本实现
- 由浅入深学习自定义控件(4)-滑动开关slidemenu