Ganymed SSH2 analog putty Remote Interactive Execution Command tool

Source: Internet
Author: User
Tags gtk


Then the introduction of the previous article http://blog.csdn.net/doctor_who2004/article/details/47322105:

We simulate interactive execution of remote commands such as the following 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;/** * Analog Interactive terminal * * @author doctor * * @time August 6, 2015 * * @see Http://www.programcreek.com/java-api-examples/index.php?api=ch.ethz.ssh2.SCPClient * */pub LIC final class SSHAgent2 {private Logger log = Loggerfactory.getlogger (GetClass ());p rivate Connection Connection; Private Session session;private bufferedreader stdout;private printwriter printwriter;private bufferedreader stderr; Private Executorservice service = Executors.newfixedthreadpool (3);p rivate Scanner Scanner = new Scanner (system.in); public void INitsession (String hostName, String userName, String passwd) throws IOException {connection = new connection (hostName); con Nection.connect (); Boolean authenticatewithpassword = Connection.authenticatewithpassword (UserName, passwd); 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));p rintwriter = 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 ();p rintwriter.write (nextline + "\ r \ n");p Rintwriter.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 ();}}

Here read and write stream I separately in different threads, to prevent the blocking between read and write streams, simple simulation of the interactive environment. Of course, the login is the local system, no virtual machine installed, and so on.


Execution Result:

Last Login:thu 6 20:18:52 from 127.0.0.1input:pwd; [[email protected] ~]$ pwd;/home/doctor[[email protected] ~]$ input:ll[[email protected] ~]$ ll Total dosage  2013760drwxrwxr-x. 3 Doctor doctor 4096 July 21:58 android-sdk_r24.3.3-linux-rw-rw-r--.  1 Doctor doctor 309109716 July 17:05 android-sdk_r24.3.3-linux.tgzdrwxrwxr-x.  7 Doctor doctor 4096 July 00:04 android-studio-rw-rw-r--.  1 Doctor doctor 258628239 July 17:01 android-studio-ide-141.1980579-linux.zipdrwxrwxr-x.  3 Doctor doctor 4096 July 5 09:48 appdrwxrwxr-x. 7 Doctor doctor 4096 August 4 22:11 documentsdrwxrwxr-x.  Doctor Doctor 4096 August 2 21:22 gitdrwxrwxr-x.  3 Doctor doctor 4096 June 12:03 ideaiu-14.1.3-rw-rw-r--. 1 Doctor doctor 308609842 June 13:36 ideaiu-14.1.3.tar.gzdrwxrwxr-x.  Doctor doctor 4096 July 12:26 jstorm-0.9.6.3-rw-rw-r--.  1 Doctor doctor 11113536 June 8 21:19 jstorm-0.9.6.3.zipdrwxrwxr-x. 7 Doctor doctor 4096 July 21:24 node_mOdules-rw-rw-r--. 1 Doctor doctor 20063992 July 11:49 node-v0.12.7.tar.gzdrwxrwxrwx.  Doctor Doctor 4096 August 2 18:10 opt-rw-rw-r--.  1 Doctor doctor 466053 July 20:05 pljava-x86_64-unknown-linux-gnu-pg9.1-1.4.3.tar.gz-rwxrwxrwx.  1 Doctor doctor 155693 July 21:58 plv8-1.4.4.tar.gzdrwxrwxr-x.  3 Doctor doctor 4096 July 23:33 pycharm-professional-4.5.3-rwxrwxrwx.  1 Doctor doctor 160688951 July 22:38 pycharm-professional-4.5.3.tar.gzdrwxrwxr-x.  3 Doctor doctor 4096 July 23:35 pycharmprojectsdrwxrwxr-x.  3 Doctor doctor 4096 June 9 19:08 spring-tool-suite-3.6.4.release-e4.4.2-linux-gtk-x86_64-rwxrwxrwx.  1 Doctor 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 doctor 4096 July 3 21:14 spring-tool-suite-3.7.0.release-e4.5-linux-gtk-x86_641-rwxrwxrwx.  1 Doctor Doctor 429456394 July 3 13:20 spring-tool-suite-3.7.0.release-e4.5-linux-gtk-x86_641.tar.gzdrwxrwxr-x. 8 Doctor DoctoR 4096 July 22:08 tmpdrwxrwxr-x.  Ten Doctor doctor 4096 November V8-3.31.1-rw-rw-r--.  1 Doctor doctor 16423158 July 00:28 v8-3.31.1.tar.gzdrwxrwxr-x.  3 Doctor doctor 4096 June 8 20:49 webstorm-10.0.3-rw-rw-r--.  1 Doctor doctor 130207806 June 8 20:04 webstorm-10.0.3.tar.gzdrwxrwxr-x.  5 Doctor doctor 4096 July 19:51 webstormprojectsdrwxrwxr-x.  5 Doctor doctor 4096 September zookeeperdrwxrwxr-x.  3 Doctor doctor 4096 August 13 2014 wallpaper drwxr-xr-x.  2 Doctor doctor 4096 July 26 17:00 picture drwxr-xr-x.  2 Doctor doctor 4096 July 30 23:34 document DRWXR-XR-X.  2 Doctor doctor 4096 August 5 21:47 download drwxr-xr-x. 2 Doctor doctor 4096 June 7 21:44 desktop [[email protected] ~]$ input:who[[email protected] ~]$ whodoctor pts/   1 2015-08-06 20:28 (127.0.0.1) [[email protected] ~]$ input:who am i[[email protected] ~]$ who am idoctor PTS/1 2015-08-06 20:28 (127.0.0.1) [[email protected] ~]$ input:date[[email protected] ~]$ date2015 August 06 Thu 20:29:18 cst[[email protected] ~]$ input: 





Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint [http://blog.csdn.net/doctor_who2004].

Ganymed SSH2 analog putty Remote Interactive Execution Command tool

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.