1

I have opened session with jsch on Android, this way:

SshObjects Connect(String username, String password, String hostname, int port)
    {
        JSch jsch=new JSch();
        try
        {
            sshObjects._session = jsch.getSession(username, hostname, port);
        }
        catch (JSchException e)
        {
            e.printStackTrace();
            return null;
        }
        sshObjects._session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        sshObjects._session.setConfig(config);
        try
        {
            sshObjects._session.connect();
        }
        catch (JSchException e)
        {
            e.printStackTrace();
            return null;
        }

        try
        {
            sshObjects._channel = (ChannelExec) sshObjects._session.openChannel("exec");
        }
        catch (JSchException e)
        {
            e.printStackTrace();
            return null;
        }


        connected = true;

        return sshObjects;
}

And then, to execute some command on opened session and get result, I did this:

private String ExecuteCommand(SshCommandsEnum cmdType)
    {
        String result = "";

        switch (cmdType)
        {
            case SERVER_INFO:
                sshObjects._channel.setCommand("uname --all");
                break;
            .......
        }

        try
        {
            BufferedReader in=new BufferedReader(new InputStreamReader(sshObjects._channel.getInputStream()));
            //sshObjects._channel.disconnect();
            try
            {
                sshObjects._channel.connect();
            }
            catch (JSchException e)
            {
                e.printStackTrace();
            }

            String msg=null;
            try
            {
                while((msg=in.readLine())!=null)
                {
                    System.out.println(msg);
                    result += msg;
                }
                sshObjects._channel.disconnect();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        catch (IOException e)
        {
            return "";
        }



        return result;
    }

So I want to open my session only once. And then execute commands as "exec" on it. It works for first command executed after connect - everything seems to be ok and I can get result succesfully. But when I call "Execute Command" again, it doesn't work anymore. My thread hangs on sshObjects._channel.connect(); and nothing works. When I try to disconnect (close channel and session) and connect again - the same. I can connect and disconnect without any problems only if I don't even try to execute command.

However, I don't experience this issue without this:

BufferedReader in=new BufferedReader(new InputStreamReader(sshObjects._channel.getInputStream()));

But, obviously I need it to get my command output. So what's the problem? Do you have any idea what am I doing wrong?

1 Answer 1

1

The ChannelExec is not re-usable, so you need to instantiate it for each command.

Sign up to request clarification or add additional context in comments.

2 Comments

I'll try this. But why can't I disconnect and connect again after I execute command? I don't try to reuse channel in this case. Only close session, close (already used) channel. And conncet again - it hangs.
The session owns the channel. Close the channel, then create a new channel with the same session instance. Also, consider using a 'shell' Channel, as it allows more than one command to be sent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.