使用JSch为SSH服务器上执行的命令提供多行输入

时间:2018-05-29 09:44:06

标签: java unix ssh jsch pbrun

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

以下代码适用于密码

((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");

参考截图:

enter image description here

enter image description here

我正在使用的代码:

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);
      }
}

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

感谢马丁以下的方法为我工作

( 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>