Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为使用JSch在SSH服务器上执行的命令提供多行输入_Java_Unix_Ssh_Jsch_Pbrun - Fatal编程技术网

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

Java 为使用JSch在SSH服务器上执行的命令提供多行输入,java,unix,ssh,jsch,pbrun,Java,Unix,Ssh,Jsch,Pbrun,我有一个类似于pop的密码出现的情况,这意味着我需要在密码之后输入另一个文本,并且我还需要以编程方式处理它 下面的代码正在为密码工作 ((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo demouser | pbrun democommand"); echo确实可以帮我输入密码。 但就在它之后,我需要像输入密码一样输入文本,我不能这样做。所以我用管子放了另一个回声,但它不起作用 我使用的代码与此相同 ((ChannelExe

我有一个类似于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");
我也尝试了下面的引用,并按照下面的方式编写了命令,但仍然没有成功

参考截图:

我使用的代码:

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);
      }
}
试试看{
JSch JSch=新的JSch();
Session Session=jsch.getSession(用户,主机,22);
属性配置=新属性();
配置放置(“检查”、“否”);
session.setConfig(config);;
session.setPassword(密码);
System.out.println(“用户==”+用户+“\n主机==”+主机);
session.connect();
System.out.println(“连接到主机==”+主机);
字符串sudo_pass=“demopassword”;
Channel=session.openChannel(“exec”);
System.out.println(“cd命令”);
(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();
字节[]tmp=新字节[1024];
while(true){
while(in.available()>0){
inti=in.read(tmp,0,1024);

如果(iBash)为命令提供两行输入的语法是:

( echo input1 && echo input2 ) | command
另见


您还可以在Java代码中提供输入,而不是使用shell构造:

多亏了马丁,下面的方法对我很有效

( 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);
 }
}
下面的代码适合我

( 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);
 }
}
试试看{
JSch JSch=新的JSch();
Session Session=jsch.getSession(用户,主机,22);
属性配置=新属性();
配置放置(“检查”、“否”);
session.setConfig(config);;
session.setPassword(密码);
System.out.println(“用户==”+用户+“\n主机==”+主机);
session.connect();
System.out.println(“连接到主机==”+主机);
字符串sudo_pass=“demo123”;
Channel=session.openChannel(“exec”);
System.out.println(“cd命令”);
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();
字节[]tmp=新字节[102400];
while(true){
while(in.available()>0){
inti=in.read(tmp,0102400);

如果(iTry类似于
“..”echo\\\\”密码\\另一个文本\“..”
您的意思是像((ChannelExec)channel).setCommand(“cd~demouser/bin;ls;echo\\\\”密码\\另一个文本\“;pbrun democommand”);以上不适用于meThanks Martin以获取您的答复。您的答案已输入密码,但另一个pop正在等待参数,这意味着程序没有终止并等待某些内容。我是否需要按enter ProgramitaCall?我已将代码与此问题一起发布。请查看并让我知道我是否做错了or遗漏了任何内容..这将非常有帮助抱歉,我不明白你所说的“你的答案只有密码,但另一个pop正在等待参数输入”是什么意思-我的答案显示了如何传递两行(密码和其他一些[参数?])。我也不明白你的
是如何输出的。write((sudo_pass+“\n”).getBytes()
与其余部分一起。这是另一个密码吗?您不能有两个单独的输入流。cd~demouser/bin;ls;(echo-demopassword&&echo-Automation)| pbrun-democommand”)命令成功输入密码,但在eclipse上下一个弹出窗口仍在等待输入输入2。或者没有输入输入2