Java calls remote shell scripts (to be verified)

Source: Internet
Author: User

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

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.