Ganymed SSH2 remote interactive Command Execution tool that simulates putty, ganymedputty

Source: Internet
Author: User

Ganymed SSH2 remote interactive Command Execution tool that simulates putty, ganymedputty


Next, I will introduce http://blog.csdn.net/doctor_who2004/article/details/47322105:

We simulate remote commands such as 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. imports; 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;/*** simulate interactive terminal *** @ author doctor *** @ time August 6, 2015 *** @ 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 partition = new partition (System. in); public void initSessio N (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. getSt Derr (), 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 = bytes. nextLine (); printWriter. write (nextLine + "\ r \ n"); printWriter. flush () ;}}) ;}public void close () {IOUtils. closeQuietly (stdout); IOUtils. closeQuietly (stderr); IOUtils. closeQuietly (printWriter); IOUtils. closeQuietly (logical); 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" using sshagent.exe cCommand (); // sshAgent. close ();}}

Here, I operate read and write streams in different threads to prevent blocking between read and write streams and simulate the interaction environment. Of course, you are logged on to the local system and have not installed virtual machines.


Execution result:

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 total usage: 2013760drwxrwxr-x. 3 doctor 4096 July 27 21:58 android-sdk_r24.3.3-linux-rw-rw-r --. 1 doctor 309109716 July 26 17:05 android-sdk_r24.3.3-linux.tgzdrwxrwxr-x. 7 doctor 4096 July 29 00:04 android-studio-rw-r --. 1 doctor 258628239 July 26 17:01 android-studio-ide-141.1980579-linux.zipdrwxrwxr-x. 3 doctor 4096 09:48 appdrwxrwxr-x. 7 doctor 4096 December 22, August 4: 11 Documentsdrwxrwxr-x. 21 doctor 4096 21:22 gitdrwxrwxr-x. 3 doctor 4096 June 14 12:03 ideaIU-14.1.3-rw-rw-r --. 1 doctor 308609842 June 13 13:36 ideaIU-14.1.3.tar.gzdrwxrwxr-x. 11 doctor 4096 July 26 12:26 jstorm-0.9.6.3-rw-rw-r --. 1 doctor 11113536 June 8 21:19 jstorm-0.9.6.3.zipdrwxrwxr-x. 7 doctor 4096 July 23 21:24 node_modules-rw-rw-r --. 1 doctor 2 0063992 July 11 11:49 node-v0.12.7.tar.gzdrwxrwxrwx. 18 doctor 4096 18:10 opt-rw-r --. 1 doctor 466053 July 28 20:05 pljava-x86_64-unknown-linux-gnu-pg9.1-1.4.3.tar.gz-rwxrwxrwx. 1 doctor 155693 July 24 21:58 plv8-1.4.4.tar.gzdrwxrwxr-x. 3 doctor 4096 July 27 23:33 pycharm-professional-4.5.3-rwxrwxrwx. 1 doctor 160688951 July 27 22:38 pycharm-professional-4.5.3.tar. Gzdrwxrwxr-x. 3 doctor 4096 23:35 PycharmProjectsdrwxrwxr-x. 3 doctor 4096 June 9 19:08 spring-tool-suite-3.6.4.RELEASE-e4.4.2-linux-gtk-x86_64-rwxrwxrwx. 1 doctor 417016357 June 8 22:13 spring-tool-suite-3.6.4.RELEASE-e4.4.2-linux-gtk-x86_64.tar.gzdrwxrwxr-x. 3 doctor 4096 July 3 21:14 spring-tool-suite-3.7.0.RELEASE-e4.5-linux-gtk-x86_641-rwxrwxrwx. 1 doctor 42 9456394 July 3 13:20 spring-tool-suite-3.7.0.RELEASE-e4.5-linux-gtk-x86_641.tar.gzdrwxrwxr-x. 8 doctor 4096 22:08 tmpdrwxrwxr-x. 10 doctor 4096 November 12 v8-3.31.1-rw-rw-r --. 1 doctor 16423158 July 25 00:28 v8-3.31.1.tar.gzdrwxrwxr-x. 3 doctor 4096 June 8 20:49 WebStorm-10.0.3-rw-rw-r --. 1 doctor 130207806 June 8 20:04 WebStorm-10.0.3.tar.gzdrwxrwxr-x. 5 doctor d Octor 4096 19:51 WebstormProjectsdrwxrwxr-x. 5 doctor 4096 September 25 2014 zookeeperdrwxrwxr-x. 3 doctor 4096 2014 wallpaper drwxr-xr-x. 2 doctor 4096 17:00 image drwxr-xr-x. 2 doctor 4096 23:34 document drwxr-xr-x. 2 doctor 4096 21:47 download drwxr-xr-x. 2 doctor 4096 21:44 desktop [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 ~] $ Date Thursday, August 06, 2015 20:29:18 CST [doctor @ localhost ~] $ Input:





Copyright Disclaimer: This article is an original article by the blogger. It cannot be reproduced at [http://blog.csdn.net/doctor_who2004.pdf] without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.