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