【问题标题】:Provide multiple lines of input to command executed on SSH server with JSch使用 JSch 为在 SSH 服务器上执行的命令提供多行输入
【发布时间】:2019-12-31 19:57:41
【问题描述】:

我遇到了另一个弹出式密码的情况,意味着我需要在密码后输入另一个文本,我还需要以编程方式处理它。

下面的代码用于密码

((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo demouser | pbrun democommand");

echo 确实可以让我输入密码。 但就在它之后,我需要像密码一样输入文本,但我无法这样做。所以我用管道放了另一个回声,但它不起作用。

我正在使用的代码

((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation |  echo demouser | pbrun democommand");

我也尝试了下面的参考并写了如下命令,仍然没有运气

pipe password to sudo and other data to sudoed command

((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation |  { echo demopass; } | pbrun democommand");

参考截图:

我正在使用的代码:

try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);;
            session.setPassword(password);
            System.out.println("user=="+user+"\n host=="+host);
            session.connect();
            System.out.println("connected to host ===="+host);
            String sudo_pass="demopassword";

        Channel channel=session.openChannel("exec");


        System.out.println("cd command");
        ((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; ( echo demopassword && echo Automation )  | pbrun democommand");
        ((ChannelExec) channel).setPty(true);

        InputStream in=channel.getInputStream();
        OutputStream out=channel.getOutputStream();
        ((ChannelExec)channel).setErrStream(System.err);

        channel.connect();

        out.write((sudo_pass+"\n").getBytes());

        out.flush();

        byte[] tmp=new byte[1024];
        while(true){
          while(in.available()>0){
            int i=in.read(tmp, 0, 1024);
            if(i<0)break;
            System.out.print(new String(tmp, 0, i));
          }
          if(channel.isClosed()){
            System.out.println("exit-status: "+channel.getExitStatus());
            break;
          }
          try{Thread.sleep(1000);}catch(Exception ee){}
        }

        channel.disconnect();
        session.disconnect();
      }
      catch(Exception e){
        System.out.println(e);
      }
}

任何解决方法都会有所帮助

【问题讨论】:

  • 试试"... | echo \\\"password\\nanother text\" ..."
  • 你的意思是像 ((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo \\\"password\\nanother text\" | pbrun democommand");跨度>
  • 以上不适合我

标签: java unix ssh jsch pbrun


【解决方案1】:

为命令提供两行输入的 Bash 语法是:

( echo input1 && echo input2 ) | command

另见Pipe multiple commands into a single command


您也可以在 Java 代码中提供输入,而不是使用 shell 结构:
Providing input/subcommands to command executed over SSH with JSch

【讨论】:

  • 感谢马丁的回复。您的答案已输入密码,但另一个弹出窗口正在等待参数,这意味着程序没有终止并等待某些东西。我需要按程序输入吗
  • 我已将代码与此问题一起发布。如果我做错了什么或遗漏了什么,请查看并告诉我。它真的很有帮助
  • 对不起,我不明白你的意思 “你的答案只有密码,但另一个弹出窗口正在等待参数” - 我的答案显示如何通过两个行(密码和其他一些[参数?])。
  • 我也不明白您的out.write((sudo_pass+"\n").getBytes()); 与其他人的关系如何。这是另一个密码吗?您不能有两个单独的输入流。
  • cd ~demouser/bin;ls; ( echo demopassword && echo 自动化 ) | pbrun democommand") 命令成功输入密码,但在 eclipse 上,下一个弹出窗口仍在等待输入 input2。要么没有输入 input2
【解决方案2】:

感谢 Martin,下面的方法对我有用

( echo 'command1' && echo 'command2' ) | command

下面的代码对我有用

try {
        JSch jsch = new JSch();
       Session session = jsch.getSession(user, host, 22);
       Properties config = new Properties();
       config.put("StrictHostKeyChecking", "no");
       session.setConfig(config);;
       session.setPassword(password);
       System.out.println("user=="+user+"\n host=="+host);
       session.connect();
       System.out.println("connected to host ===="+host);
       String sudo_pass="demo123";

   Channel channel=session.openChannel("exec");


   System.out.println("cd command");

   ((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; ( echo 'echo Automation' && echo 'command' )  | pbrun democommand");
   ((ChannelExec) channel).setPty(true);

   InputStream in=channel.getInputStream();
   OutputStream out=channel.getOutputStream();
   ((ChannelExec)channel).setErrStream(System.err);

   channel.connect();

   out.write((sudo_pass+"\n").getBytes());
  // out.write(("\n").getBytes());

   out.flush();

   byte[] tmp=new byte[102400];
   while(true){
     while(in.available()>0){
       int i=in.read(tmp, 0, 102400);
       if(i<0)break;
       System.out.print(new String(tmp, 0, i));
     }
     if(channel.isClosed()){
       System.out.println("exit-status: "+channel.getExitStatus());
       break;
     }
     try{Thread.sleep(1000);}catch(Exception ee){
         ee.printStackTrace();
         System.out.println(ee.getMessage());
     }
   }
   channel.disconnect();
   session.disconnect();
 }
 catch(Exception e){
   System.out.println(e);
 }
}

使用的依赖:

    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.50</version>
    </dependency>

【讨论】:

    猜你喜欢
    • 2017-08-17
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多