【问题标题】:Why runtime.exec() is not executing. when I given sshpass command为什么 runtime.exec() 没有执行。当我给出 sshpass 命令时
【发布时间】:2015-09-15 17:33:58
【问题描述】:

为什么 runtime.exec() 没有执行。当我给出 sshpass 命令时

rt.exec("sshpass -p sbsiz scp '/home/surendra/Desktop/remote_backup.txt' root@192.168.59.115:/home/");

但是当我直接在终端中运行这个命令时,它的工作方式就像 在终端中

sshpass -p sbsiz scp '/home/surendra/Desktop/remote_backup.txt' root@192.168.59.115:/home/

【问题讨论】:

    标签: java runtime.exec sshpass


    【解决方案1】:

    您可以检查 Process 类(返回 rt.exec)中的输入流和错误流,以查看导致命令未执行的实际错误,如下所示:

    public static void printStream(InputStream is, String type){
    try
       {
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String line=null;
          while ( (line = br.readLine()) != null)
                System.out.println(type + ">" + line);    
       } catch (IOException ioe){
               ioe.printStackTrace();  
       }
    }
    
    public static void main(String args[])
    {
        String cmd = "command to execute";
        Process proc = Runtime.getRuntime().exec("sshpass -p sbsiz scp '/home/surendra/Desktop/remote_backup.txt' root@192.168.59.115:/home/");
        printStream(proc.getInputStream(), "OUTPUT");
        printStream(proc.getErrorStream(), "ERROR");
    }
    

    【讨论】:

    • 是否可以更改远程系统上的文件名?存储文件时
    • @SurendrakumarKadiri .. 为什么不尝试在提供目标主机时提供文件名。如果您有问题,请使用 linux/scp 标签发布另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2021-07-06
    相关资源
    最近更新 更多