Ganymed SSH-2 java execute remote linux machine command tools, ganymedssh-2

Source: Internet
Author: User

Ganymed SSH-2 java execute remote linux machine command tools, ganymedssh-2

Ganymed SSH2 for Java is a library which implements the SSH-2 protocol in pure Java (tested on J2SE 1.4.2 and 5.0 ). it allows one to connect to SSH servers from withinJava programs. it supports SSH sessions (remote command execution and shell access), local and remote port forwarding, local stream forwarding, X11 forwarding, SCP and SFTP. there are no dependencies on any JCE provider, as all crypto functionality is supported ded.

Ganymed SSH2 for Java was first developed for the Ganymed replication project and acouple of other projects at the IKS group at ETH Zurich.

Website: http://www.ganymed.ethz.ch/ssh2.

Please read the specified ded LICENCE.txt, latest changes can be found in HISTORY.txt.


In a linux development environment, we generally use some terminal tools to connect to a remote linux server and execute bash scripts or system commands. In fact, the core of these tools is to implement the SSH2 protocol and use SSH sessions, we pass the command to display the command output result.

In the java environment, the loose authorization is Ganymed SSH-2.


Simulate remote login on the local machine and execute the system command:


Package com.doc tor. ganymed_ssh2; import java. io. IOException; import java. io. inputStream; import java. nio. charset. standardCharsets; import org. apache. commons. io. IOUtils; import ch. ethz. ssh2.Connection; import ch. ethz. ssh2.Session; import ch. ethz. ssh2.StreamGobbler;/*** @ author doctor *** @ time August 5, 2015 *** @ see https://github.com/northern-bites/ganymed-ssh2/blob/master/examples/Basic.java ***/public cl Ass Basic {/*** if the following error occurs: Password authentication fails, I get "Authentication method password ** not supported by the server at this stage **, check the ssh configuration file: /ect/ssh/sshd_config, find the configuration: "PasswordAuthentication" set to "no ". * changed it to "PasswordAuthentication yes" *** @ param args * @ throws IOException */public static void main (String [] args) throws IOException {String hostname = "127.0.0.1 "; string Username = "doctor"; String passwd = "*****"; Connection connection Connection = new connection (hostname); Connection. connect (); boolean authenticateWithPassword = connection. authenticateWithPassword (username, passwd); if (! Break) {throw new RuntimeException ("authenticateWithPassword failed");} Session session = reset cCommand ("pwd; date; echo hello doctor"); InputStream streamGobbler = new StreamGobbler (session. getStdout (); String result = IOUtils. toString (streamGobbler, StandardCharsets. UTF_8); System. out. println ("result:" + result); System. out. println ("ExitStatus:" + session. getExitStatus (); session. close (); connection. close ();}}

When executing the above program, you have to confirm whether the ssh service of the current system is available.

Run the ssh 127.0.0.1 command.

Execution result of the above program:


Result:/home/doctor
Thursday, August 06, 2015 20:12:47 CST
Hello doctor

ExitStatus: 0




Some tools are now integrated with these services, the well-known jenkins plug-inAnsibleThe ssh2 protocol is implemented.

Appendix:

Check the ssh service status:

[Doctor @ localhost ~] $ Service sshd statusRedirecting to/bin/systemctl status sshd. service ● sshd. service-OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd. service; enabled; vendor preset: enabled) Active: <em> <strong> <span style = "color: # FF6666;"> active (running) </span> </strong> </em> since 4 18:58:15 CST; 1 h 19 min ago Docs: man: sshd (8) man: sshd_config (5) Main PID: 975 (sshd) CGroup:/system. slice/sshd. service ready-975/usr/sbin/sshd-DWarning: Journal has been rotated since unit was started. log output is incomplete or unavailable.


[doctor@localhost ~]$ ssh localhostdoctor@localhost's password: Last failed login: Wed Aug  5 20:02:46 CST 2015 from 127.0.0.1 on ssh:nottyThere was 1 failed login attempt since the last successful login.Last login: Sun Jun  7 21:43:45 2015[doctor@localhost ~]$ 


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.