简体   繁体   English

Jsch - 一个会话多个频道

[英]Jsch - One Session Multiple Channels

I managed to execute a single command through ssh with Jsch but when i try to execute a second command it fails 我设法通过ssh用Jsch执行单个命令但是当我尝试执行第二个命令时它失败了

For debugging i brought this problem down to this lines: 为了调试我把这个问题归结为这样:

import java.io.IOException;
import java.io.InputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class Exec {

    public static void test(Session session) throws Exception {
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand("pwd");

        channel.setInputStream(null);

        ((ChannelExec) channel).setErrStream(System.err);

        InputStream in = channel.getInputStream();

        channel.connect();

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

    public static void main(String[] arg) {
        try {
            JSch jsch = new JSch();         
            Session session = jsch.getSession("nck", "127.0.0.1", 22);          
            session.setPassword("asd");         
            session.setConfig("StrictHostKeyChecking", "no");           
            session.connect();

            test(session); // This one succeeds with exit-status: 0         
            test(session); // This one fails with exit-status: 255

            session.disconnect();
        } catch (Exception e) {
            //
        }
    }
}

This is mostly the official Exec example but this gives me this output: 这主要是官方的Exec示例,但这给了我这个输出:

/home/nck
exit-status: 0
exit-status: 255

The first command is executed succesfully the second doesnt. 第一个命令成功执行第二个不执行。

Any Ideas ? 有任何想法吗 ?

Here is a complete working Java class based on @krishna's answer: 这是一个基于@krishna答案的完整的Java类:

/**
 * Run several ssh commands in a single JSch session
 */
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.*;

class SshMultiCommands
{
   public static void main(String[] args) throws Exception
   {
      JSch jsch = new JSch();
      String user = "user";            //CHANGE ME
      String host = "192.168.222.157"; //CHANGE ME
      String passwd = "password";      //CHANGE ME
      int port = 22;    
      Session session = jsch.getSession(user, host, port);
      session.setPassword(passwd);

      session.setConfig("StrictHostKeyChecking", "no");

      session.connect();

      Channel channel = session.openChannel("shell");
      OutputStream ops = channel.getOutputStream();
      PrintStream ps = new PrintStream(ops, true);

      channel.connect();
      InputStream input = channel.getInputStream();

      //commands
      ps.println("ls -lag");
      ps.println("cd /etc");
      ps.println("ls");
      ps.println("exit");
      ps.close();

      printResult(input, channel);

      channel.disconnect();
      session.disconnect();
   }

   /**
    * @param input
    * @param channel
    */
   private static void printResult(InputStream input,
                                   Channel channel) throws Exception
   {
      int SIZE = 1024;
      byte[] tmp = new byte[SIZE];
      while (true)
      {
         while (input.available() > 0)
         {
            int i = input.read(tmp, 0, SIZE);
            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(300);
         }
         catch (Exception ee)
         {
         }
      }
   }
}

Hey I have exactly the same problem with jsch and Ubuntu. 嘿我和jsch和Ubuntu有完全相同的问题。 (How) did you solve it? (怎么样)你解决了吗? Making a new session for every execute eats too much time? 为每次执行创建一个新会话会占用太多时间吗? At the moment I catch the jsch exception and search for "session isn't down" then I reconnect the session and execute command again. 目前我捕获了jsch异常并搜索“session is not down”然后我重新连接会话并再次执行命令。 That works but is not a good solution. 这有效,但不是一个好的解决方案。

Edit: I was on the wrong way I solved my problem for now by using 编辑:我错了,我现在通过使用解决了我的问题

channel = session.openChannel("shell");

instead of "exec". 而不是“exec”。

you can use shell to execute multiple commands. 你可以使用shell来执行多个命令。 following is the code to execute commands via shell. 以下是通过shell执行命令的代码。

Channel channel=session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);

channel.connect();
ps.println("mkdir folder"); 
ps.println("dir");
//give commands to be executed inside println.and can have any no of commands sent.
ps.close();

InputStream in=channel.getInputStream();
byte[] bt=new byte[1024];

while(true) {
    while(in.available()>0) {
        int i=in.read(bt, 0, 1024);
        if(i<0)
            break;
        String str=new String(bt, 0, i);

        //displays the output of the command executed.
        System.out.print(str);
    }
    if(channel.isClosed()) 
        break;

    Thread.sleep(1000);
    channel.disconnect();
    session.disconnect();   
}

I used exactly same code and had problem some of the times reading output of the script i executed through ssh. 我使用完全相同的代码,并且有时会读取通过ssh执行的脚本输出的问题。 Sometimes the program was missing the all content, and sometime only a part of the content. 有时程序缺少所有内容,有时只缺少部分内容。 I resolved my problem by changing the if(channel.isclosed) in the nested loop. 我通过更改嵌套循环中的if(channel.isclosed)解决了我的问题。 Here is the new code 这是新代码

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()) {
             if(in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                System.out.print(new String(tmp, 0, i));
             } 
            System.out.println("exit-status: " + channel.getExitStatus());
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception ee) {
}

If you want to use exec. 如果你想使用exec。 you can run multiple commands by putting a semi colon (;) within commands 您可以通过在命令中放置一个分号(;)来运行多个命令

like: 喜欢:

pwd;ls- -l; 
cd foldername

((ChannelExec) channel).setCommand("pwd;ls -l; cd foldername");

This wasn't a bug in jsch, it was a bug of my ssh server (cygwin - I bet I configured something wrong...) 这不是jsch中的错误,它是我的ssh服务器的一个错误(cygwin - 我打赌我配置错了......)

Edit: hmm I had the same problem with jsch with Ubuntu ... weird 编辑:嗯我与Ubuntu的jsch有同样的问题......很奇怪

Guess as workarround I'll have to open a new session for each query. 猜测是workarround我将不得不为每个查询打开一个新的会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM