简体   繁体   中英

Multiple SHELL Channels JSch java SSH

Basically, I have a java program that controls two different SSH connections using the JSch SSH library for Java.

I create my own class called SSHConnection that is a wrapper for the JSch libraries, and I initialize two copies by calling the constructor shown below:

public SSHConnection(String username,String password,String host, String knownHosts, int portNumber) throws JSchException{

  JSch jsch = new JSch(); jsch.setKnownHosts(knownHosts); this.session = jsch.getSession(username,host,portNumber); this.session.setPassword(password); this.session.connect(); 

}

Now, inside the SSHConnection class, in order to create the shell channel, I use this command:

shell = (ChannelShell)this.session.openChannel("shell");

However, I have found out that if I have already have a session open that is using the SHELL channel I get an error saying "Channel is not opened" (JSch Exception) when I try to open the first channel for the second session.

Is there a way that I can open two Shell channels at once? Is this my problem, a JSch problem, or an SSH issue?

Edit::::

I can connect to the SSH server simultaneously from multiple terminals. For instance, in one terminal I can do...

ssh asofjpasf@myserver

and in another terminal I can do... ssh opapaos@myserver

However, when I try to make a single program using JSch have the same behavior, it fails stating that the channel is not open when I try to open the second shell channel.

I'm able to open two shell channels at once. I've seen embedded servers which disallow this, but an out-of-the-box OpenSSH server should have no problem with it.

By the way, for my own testing, I'm using clj-ssh (a Clojure wrapper for jsch, this implementation built against jsch 0.1.50):

(ns ssh-test.core
  (:use [clj-ssh.ssh]))

(defn test-conn []
  (let [agent (ssh-agent {})]
    (let [session (session agent "127.0.0.1" {:strict-host-key-checking :no})]
      (with-connection session
        (let [ch-a (shell-channel session)
              ch-b (shell-channel session)]
          [ch-a ch-b])))))

...or, using two separate sessions:

(defn test-conn []
  (let [agent (ssh-agent {})]
    (let [session-a (session agent "127.0.0.1" {:strict-host-key-checking :no})
          session-b (session agent "127.0.0.1" {:strict-host-key-checking :no})]
      (with-connection session-a
        (let [ch-a (shell-channel session-a)]
          (with-connection session-b
            (let [ch-b (shell-channel session-b)]
              [ch-a ch-b])))))))

Thus, this works correctly in either case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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