接着上篇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() {
@Override
public void run() {
String line;
try {
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
service.submit(new Runnable() {
@Override
public 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.1
input:pwd;
[doctor@localhost ~]$ pwd;
/home/doctor
[doctor@localhost ~]$
input:ll
[doctor@localhost ~]$ ll
总用量 2013760
drwxrwxr-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.tgz
drwxrwxr-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.zip
drwxrwxr-x. 3 doctor doctor 4096 7月 5 09:48 app
drwxrwxr-x. 7 doctor doctor 4096 8月 4 22:11 Documents
drwxrwxr-x. 21 doctor doctor 4096 8月 2 21:22 git
drwxrwxr-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.gz
drwxrwxr-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.zip
drwxrwxr-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.gz
drwxrwxrwx. 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.gz
drwxrwxr-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.gz
drwxrwxr-x. 3 doctor doctor 4096 7月 27 23:35 PycharmProjects
drwxrwxr-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.gz
drwxrwxr-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.gz
drwxrwxr-x. 8 doctor doctor 4096 7月 23 22:08 tmp
drwxrwxr-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.gz
drwxrwxr-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.gz
drwxrwxr-x. 5 doctor doctor 4096 7月 29 19:51 WebstormProjects
drwxrwxr-x. 5 doctor doctor 4096 9月 25 2014 zookeeper
drwxrwxr-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 ~]$ who
doctor pts/1 2015-08-06 20:28 (127.0.0.1)
[doctor@localhost ~]$
input:who am i
[doctor@localhost ~]$ who am i
doctor pts/1 2015-08-06 20:28 (127.0.0.1)
[doctor@localhost ~]$
input:date
[doctor@localhost ~]$ date
2015年 08月 06日 星期四 20:29:18 CST
[doctor@localhost ~]$
input: