The use of ch.ethz.ganymed package Ganymed-ssh2

Source: Internet
Author: User
Tags ssh


The recent automated implementation of the test, the need to implement in the Windows environment through the springboard to the test machine, run a shell script on a test machine function, see some previous reference code, using Runtime.exec ("\" C:\\Program Files\\ Securecrt\\vsh.exe \ "-PW Password ***@*** sh **.sh") such a way to achieve, and later tried, found through the board to achieve the test function, but the middle of a little bit of a toss, and, Log on to the test machine after the execution of the shell script results when the good and bad, because of the unfamiliar, the personal feel should be the flow is not control, or the cause of the thread and so on. Then find another way to achieve the same function, introduce Ganymed-ssh2-build210.jar.



Ganymed SSH-2 for Java is a package that implements the SSH-2 protocol with pure Java. It is very easy to use it, just specify a valid username password, or authorize the authentication file, you can create a connection to a remote Linux host, invoke the script file on the Linux host in the established session, and perform the related operation.
How to use:
To add a configuration to the Pom.xml:


<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactid>ganymed-ssh2</ artifactid>
<version>build210</version>
</dependency>


Usage examples:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class GanymedTest {

public static void main (String [] args) {

try {

Connection conn = new Connection ("Test machine IP");
conn.connect ();
boolean isAuthenticated = conn.authenticateWithPassword ("username",
"password");
if (isAuthenticated == false)
throw new IOException ("Authentication failed.");
Session sess = conn.openSession ();
sess.execCommand ("ls \ n");
InputStream stdout = new StreamGobbler (sess.getStdout ());
BufferedReader br = new BufferedReader (
new InputStreamReader (stdout));
while (true) {
String line = br.readLine ();
if (line == null)
break;
System.out.println (line);
}
sess.close ();
conn.close ();
} catch (IOException e) {
e.printStackTrace (System.err);

System.exit (2);
}

}
} 





In this way, directly through the user name and password directly to the test machine, to carry out related operations, there will be no thread, blocking and other results caused by the execution of good and bad situation.



Transferred from: http://testing.etao.com/node/421


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.