JSch stdout not appearing when running from command line

285 Views Asked by At

When I run the following code which is a modified block from one of the jsch examples. The ssh command works but although I can see subsequent stdout/stderr in eclipse or jdb I cannot see output when I run outside eclipse. I assume that stdout and/or stderr has been redirected but I don't know how to cancel the redirection.

Thanks John Ross

public void runSSHCommand(final String command, final String user, final String pwd,
    final String host)
{
    try
    {

        final JSch jsch = new JSch();
        final Session session = jsch.getSession(user, host, 22);
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.setPassword(pwd);
        session.connect();

        final Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(System.err);
        final InputStream in = channel.getInputStream();
        channel.connect();
        final byte[] tmp = new byte[1024];
        while (true)
        {
            while (in.available() > 0)
            {
                final 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)
                {
                    continue;
                }
                System.out.println("exit-status: " + channel.getExitStatus());
                break;
            }
            try
            {
                Thread.sleep(1000);
            } catch (final Exception ee)
            {
            }
        }
        channel.disconnect();
        session.disconnect();
    } catch (final Exception e)
    {
        System.out.println(e);
    }

    return;
}
0

There are 0 best solutions below