Recently, a Web management system needs to be developed to execute service scripts on a remote Linux host through web control.
This tool is packaged into a jar file: ganymed-ssh2-build210.jar, which can be downloaded at http://www.ganymed.ethz.ch/ssh2. This tool is implemented based on the SSH2 protocol. It is very easy to use it. You only need to specify a valid user name and password or authorize the authentication file, you can create a connection to a remote Linux host, call the script file on the Linux host in the established session, and perform related operations.
Below is a demo I wrote based on the ganymed-ssh2-build210.jar library,CodeAs follows:
Package org. shirdrn. shell;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. NiO. charset. charset;
Import Ch. ethz. ssh2.connection;
Import Ch. ethz. ssh2.session;
/**
* Remote shell script execution Tool
*
* @ Author Administrator
*/
Public class remoteshelltool {
Private connection conn;
Private string ipaddr;
Private string charset = charset. defaultcharset (). tostring ();
Private string username;
Private string password;
Public remoteshelltool (string ipaddr, string username, string password, string charset ){
This. ipaddr = ipaddr;
This. Username = username;
This. Password = password;
If (charset! = NULL ){
This. charset = charset;
}
}
/**
* Log on to a remote Linux host
*
* @ Return
* @ Throws ioexception
*/
Public Boolean login () throws ioexception {
Conn = new connection (ipaddr );
Conn. Connect (); // connect
Return conn. authenticatewithpassword (username, password); // Authentication
}
/**
* execute shell scripts or commands
* @ Param cmds command line sequence
* @ return
*/
Public String exec (string cmds) {
inputstream in = NULL;
string result = "";
try {
If (this. login () {
session = Conn. opensession (); // open a session
session.exe ccommand (cmds);
In = session. getstdout ();
result = This. processstdout (in, this. charset);
Conn. close ();
}< BR >}catch (ioexception E1) {
e1.printstacktrace ();
}< br> return result;
}
/**
* The parsing stream obtains string information.
*
* @ Param in input stream object
* @ Param charset Character Set
* @ Return
*/
Public String processstdout (inputstream in, string charset ){
Byte [] Buf = new byte [1, 1024];
Stringbuffer sb = new stringbuffer ();
Try {
While (in. Read (BUF )! =-1 ){
SB. append (new string (BUF, charset ));
}
} Catch (ioexception e ){
E. printstacktrace ();
}
Return sb. tostring ();
}
}
It must be noted that after the script is executed, the result Text of the script execution can be obtained. The text must be correctly encoded and then returned to the client (Our Web Console ), to see the actual script execution result.
In addition, when the login method is executed, the logon is successful, which is located in the current ***. ***. ***. * **: Under the/home/username/directory, you can specify the absolute path of the script file, or navigate to the directory where the script file is located through CD, then pass the parameters required to execute the script to complete the script call and execution.
Http://hi.baidu.com/shirdrn/blog/item/59223b129a7322c5c3fd78a9.html