1 /**2 * Run shell scripts3 * @paramShell scripts that the shell needs to run4 */5 Public Static voidExecshell (String shell) {6 Try {7Runtime RT =runtime.getruntime ();8 rt.exec (shell);9}Catch(Exception e) {Ten e.printstacktrace (); One } A } - - the /** - * Run Shell - * - * @paramShstr + * Shell that needs to be executed - * @return + * @throwsIOException A */ at Public StaticList Runshell (String shstr)throwsException { -List<string> strlist =NewArrayList (); - - process process; -Process = Runtime.getruntime (). EXEC (Newstring[]{"/bin/sh", "-C", shstr},NULL,NULL); -InputStreamReader IR =NewInputStreamReader (Process in . getInputStream ()); -LineNumberReader input =NewLineNumberReader (IR); to String Line; + process.waitfor (); - while(line = Input.readline ())! =NULL){ the Strlist.add (line); * } $ Panax Notoginseng returnstrlist; -}
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
1 #!/bin/bash 2 echo ' test22 ' 3 echo $
$ $ is the first parameter passed in the script, and our console prints this parameter
To create a new MAVEN project, add dependencies:
1 <dependency> 2 <groupId>org.jvnet.hudson</groupId> 3 < Artifactid>ganymed-ssh2</artifactid> 4 <version>build210-hudson-1</version > 5 </dependency>
Write a tool class:
1 PackageCom.xj.runshell; 2 3 Importjava.io.IOException; 4 ImportJava.io.InputStream; 5 ImportJava.nio.charset.Charset; 6 7 Importch.ethz.ssh2.Connection; 8 Importch.ethz.ssh2.Session; 9 Ten Public classRemoteshelltool { One A PrivateConnection Conn; - PrivateString ipaddr; - PrivateString charset =Charset.defaultcharset (). toString (); the PrivateString UserName; - PrivateString password; - - PublicRemoteshelltool (String ipaddr, String userName, string password, + String CharSet) { - This. ipaddr =ipaddr; + This. UserName =UserName; A This. Password =password; at if(CharSet! =NULL) { - This. CharSet =CharSet; - } - } - - Public BooleanLogin ()throwsIOException { inconn =NewConnection (IPADDR); -Conn.connect ();//Connection to returnConn.authenticatewithpassword (userName, password);//Certifications + } - the Publicstring exec (string cmds) { *InputStream in =NULL; $String result = ""; Panax Notoginseng Try { - if( This. Login ()) { theSession session = Conn.opensession ();//Open a session + Session.execcommand (CMDS); A thein =session.getstdout (); +result = This. Processstdout (In, This. CharSet); - Session.close (); $ Conn.close (); $ } -}Catch(IOException E1) { - E1.printstacktrace (); the } - returnresult; Wuyi } the - Publicstring Processstdout (InputStream in, String charset) { Wu - byte[] buf =New byte[1024]; AboutStringBuffer SB =NewStringBuffer (); $ Try { - while(In.read (BUF)! =-1) { -Sb.append (NewString (BUF, CharSet)); - } A}Catch(IOException e) { + E.printstacktrace (); the } - returnsb.tostring (); $ } the the /** the * @paramargs the */ - Public Static voidMain (string[] args) { in theRemoteshelltool tool =NewRemoteshelltool ("192.168.27.41", "Hadoop", the"Hadoop", "Utf-8"); About theString result = Tool.exec ("./test.sh Xiaojun"); the System.out.print (Result); the + } - the}
The main function executes the./test.sh Xiaojun This command, the console prints out:
test22
Xiaojun
Java calls the shell script and returns the result set