Java calls the shell script and obtains the example of the result set

Source: Internet
Author: User

/** * 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
    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:

[HTML]View Plaincopy
  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:

[Java]View Plaincopy
  1. Package Com.xj.runshell;
  2. Import java.io.IOException;
  3. Import Java.io.InputStream;
  4. Import Java.nio.charset.Charset;
  5. Import ch.ethz.ssh2.Connection;
  6. Import ch.ethz.ssh2.Session;
  7. Public class Remoteshelltool {
  8. private Connection Conn;
  9. private String ipaddr;
  10. private String charset = Charset.defaultcharset (). toString ();
  11. private String UserName;
  12. private String password;
  13. Public Remoteshelltool (string ipaddr, String userName, string password,
  14. String CharSet) {
  15. this.ipaddr = ipaddr;
  16. this.username = userName;
  17. This.password = password;
  18. if (charset! = null) {
  19. This.charset = CharSet;
  20. }
  21. }
  22. Public Boolean login () throws IOException {
  23. conn = new Connection (IPADDR);
  24. Conn.connect (); //Connection
  25. return Conn.authenticatewithpassword (userName, password); //Certification
  26. }
  27. Public string Exec (string cmds) {
  28. InputStream in = null;
  29. String result = "";
  30. try {
  31. if (this.login ()) {
  32. Session session = Conn.opensession (); //Open a session
  33. Session.execcommand (CMDS);
  34. in = Session.getstdout ();
  35. result = This.processstdout (in, this.charset);
  36. Session.close ();
  37. Conn.close ();
  38. }
  39. } catch (IOException E1) {
  40. E1.printstacktrace ();
  41. }
  42. return result;
  43. }
  44. Public String Processstdout (InputStream in, String charset) {
  45. byte[] buf = new byte[1024];
  46. StringBuffer sb = new StringBuffer ();
  47. try {
  48. While (In.read (BUF)! =-1) {
  49. Sb.append (new String (BUF, CharSet));
  50. }
  51. } catch (IOException e) {
  52. E.printstacktrace ();
  53. }
  54. return sb.tostring ();
  55. }
  56. /** 
  57. * @param args
  58. */
  59. public static void Main (string[] args) {
  60. Remoteshelltool tool = new Remoteshelltool ("192.168.27.41", "Hadoop",
  61. " Hadoop", "Utf-8");
  62. String result = Tool.exec ("./test.sh Xiaojun");
  63. System.out.print (result);
  64. }
  65. }


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

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.