Ganymed SSH-2 java execute Remote Linux machine command tool

Source: Internet
Author: User
Tags openssh server



Ganymed SSH2 for Java are 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 Forwar Ding, X11 forwarding, SCP and sftp.there are no dependencies on any JCE provider, as all crypto functionality is included.



Ganymed SSH2 for Java is first developed for the Ganymed replication project and acouple of other projects at the IKS Gro Up at ETH Zurich.



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



Please read the included LICENCE.txt, latest changes can is found in HISTORY.txt.






In the Linux development environment, we generally use some terminal tools to connect to remote Linux servers, execute bash scripts or system commands, in fact, the core of these tools is to implement the SSH2 protocol, through the SSH session, we pass the command, display command output results.



In the Java environment, the authorization is more lenient is ganymed SSH-2.






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





Package com.doctor.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 class Basic {

/**
* If it appears: Password authentication fails, I get "Authentication method password
* not supported by the server at this stage
*
* Please see: ssh configuration file: /ect/ssh/sshd_config , find the configuration: "PasswordAuthentication" is 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 = new Connection(hostname);
Connection.connect();

Boolean authenticateWithPassword = connection.authenticateWithPassword(username, passwd);
If (!authenticateWithPassword) {
Throw new RuntimeException("authenticateWithPassword failed");
}

Session session = connection.openSession();
session.execCommand("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();

}

}


In the execution of the above program, you also have to confirm that the current system of SSH service is available.



The SSH 127.0.0.1 command is available for confirmation.



The results of the above program execution:






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

exitstatus:0












Some tools now integrate these services, and the SSH2 protocol is implemented by the more well-known Jenkins plugin ansible .



Report:



Check the SSH service status:




[doctor@localhost ~]$ service sshd status
Redirecting 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 四 2015-08-06 18:58:15 CST; 1h 19min ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 975 (sshd)
   CGroup: /system.slice/sshd.service
           └─975 /usr/sbin/sshd -D

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.


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


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



Ganymed SSH-2 java execute Remote Linux machine command tool


Related Article

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.