2012-02-14 117 views
1

我有一个bash脚本,它使用sshpass和ssh自动登录到不同的机器和触发器命令。当从命令行触发时,bash脚本运行良好,但是当它从Java应用程序调用时,它无法继续。从命令行使用sshpass和命令行工作,而不是从java exec

sshpass -p 'password' ssh [email protected] './SleepDisplay && exit' 

的bash脚本做了很多其他的事情,我也没有办法直接在Java中实现ssh登录。我似乎无法弄清楚,为什么它会失败。除了ssh以外的一切都运行良好。

+2

你不会说'这不是working'得到任何帮助。什么不工作?代码在哪里?你如何执行命令? – 2012-02-14 12:31:37

+0

考虑编辑你的问题,以包含你的java上下文中的行,以及(更重要的)任何你可以捕获的错误信息。 java中的调用返回失败状态吗?你能否将shell调试嵌入到上面的命令中,即'set -vx; sshpass ...'。你的JAVA和你运行shell代码的终端有相同的PATH吗?考虑添加'echo $ PATH; set -vx; sshpass ...来验证PATH是正确的。祝你好运。 – shellter 2012-02-14 14:13:57

回答

1

当经由Runtime.exec()执行命令,所述第一元件可执行,那么所有其他参数在分别通过在阵列的其余部分。

但是,您可能(可能)将整个linux命令作为可执行文件传递,这不起作用。

试试这个:

String[] cmdarray = {"sshpass", "-p", "'password'", "ssh", "[email protected]", "'./SleepDisplay && exit'"}; 
Runtime.getRuntime().exec(cmdarray); 
+0

问题是exec()只调用bash脚本 'Runtime.getRuntime().exec('bashscriptname')' sshpass在脚本里面有很多其他命令,工作正常。 – nxtwrld 2012-02-15 23:03:46

0

你可以尝试使用ganymed-ssh2 Java库,它让你能够如此使用Java执行和执行shell脚本和...... 下面使用这个库是表明一个例子:

{ 
    String hostname = "127.0.0.1"; 
    String username = "joe"; 
    String password = "joespass"; 

    try 
    { 
     /* Create a connection instance */ 

     Connection conn = new Connection(hostname); 

     /* Now connect */ 

     conn.connect(); 

     /* Authenticate. 
     * If you get an IOException saying something like 
     * "Authentication method password not supported by the server at this stage." 
     * then please check the FAQ. 
     */ 

     boolean isAuthenticated = conn.authenticateWithPassword(username, password); 

     if (isAuthenticated == false) 
      throw new IOException("Authentication failed."); 

     /* Create a session */ 

     Session sess = conn.openSession(); 

        // here execute which command separate for ";" 
     sess.execCommand("uname -a && date && uptime && who"); 

     System.out.println("Here is some information about the remote host:"); 

     /* 
     * This basic example does not handle stderr, which is sometimes dangerous 
     * (please read the FAQ). 
     */ 

     InputStream stdout = new StreamGobbler(sess.getStdout()); 

     BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); 

     while (true) 
     { 
      String line = br.readLine(); 
      if (line == null) 
       break; 
      System.out.println(line); 
     } 

     /* Show exit status, if available (otherwise "null") */ 

     System.out.println("ExitCode: " + sess.getExitStatus()); 

     /* Close this session */ 

     sess.close(); 

     /* Close the connection */ 

     conn.close(); 

    } 
    catch (IOException e) 
    { 
     e.printStackTrace(System.err); 
     System.exit(2); 
    } 
} 
1

开放首先执行命令并执行命令。尝试如下:

String COMMAND = "sshpass -p 'password' ssh [email protected] './SleepDisplay && exit'"; 
String[] SHELL_COMMAND = { "/bin/sh", "-c", COMMAND }; 
... 
Runtime runtime = Runtime.getRuntime(); 
Process process = runtime.exec(SHELL_COMMAND); 

希望我能给你一个有帮助的提示。