/** * Run shell script * @param shell script required to run */public static void Execshell (String shell) {try {runtime RT = Runtime.getrunt IME (); rt.exec (shell);} catch (Exception e) {e.printstacktrace ();}} /** * Run Shell * * @param shstr * required to execute shell * @return * @throws ioexception */public static List Runshell (String s HSTR) throws Exception {list<string> strlist = new ArrayList (); Process process;process = Runtime.getruntime (). EXEC (new string[]{"/bin/sh", "-C", shstr},null,null); I Nputstreamreader ir = new InputStreamReader (Process.getinputstream ()); LineNumberReader input = new LineNumberReader (IR); String line;process.waitfor (); while (line = Input.readline ())! = null) { strlist.add (line);} return strlist;}
Remotely log in to Linux and invoke the shell
First write a test script test.sh on the remote server and give the executable permission: chmod +x test.sh
[Plain]View Plaincopy
- #!/bin/bash
- Echo ' test22 '
- echo $
$ $ is the first parameter passed in the script, and our console prints this parameter
To create a new MAVEN project, add dependencies:
[HTML]View Plaincopy
- <dependency>
- <groupId>org.jvnet.hudson</groupId>
- <artifactid>ganymed-ssh2</artifactid>
- <version>build210-hudson-1</version>
- </Dependency>
Write a tool class:
[Java]View Plaincopy
- Package Com.xj.runshell;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import Java.nio.charset.Charset;
- Import ch.ethz.ssh2.Connection;
- Import ch.ethz.ssh2.Session;
- 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;
- }
- }
- Public Boolean login () throws IOException {
- conn = new Connection (IPADDR);
- Conn.connect (); //Connection
- return Conn.authenticatewithpassword (userName, password); //Certification
- }
- Public string Exec (string cmds) {
- InputStream in = null;
- String result = "";
- try {
- if (this.login ()) {
- Session session = Conn.opensession (); //Open a session
- Session.execcommand (CMDS);
- in = Session.getstdout ();
- result = This.processstdout (in, this.charset);
- Session.close ();
- Conn.close ();
- }
- } catch (IOException E1) {
- E1.printstacktrace ();
- }
- return result;
- }
- Public String Processstdout (InputStream in, String charset) {
- byte[] buf = new byte[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 ();
- }
- /**
- * @param args
- */
- public static void Main (string[] args) {
- Remoteshelltool tool = new Remoteshelltool ("192.168.27.41", "Hadoop",
- " Hadoop", "Utf-8");
- String result = Tool.exec ("./test.sh Xiaojun");
- System.out.print (result);
- }
- }
The main function executes the./test.sh Xiaojun This command, the console prints out:
test22
Xiaojun
Java calls the shell script and obtains the example of the result set