Jsch User/Authors,
   I am writing a wrapper class around jsch that would make it easier
to implement ssh/scp connections.

So far I am able to connect thru ssh via private keys and issue a
command on a remote sure.
I am able to capture the output, but the stderr I am having issues
with.  When ever I start to work with STDERR I see:


INFO: Caught an exception, leaving main loop due to Socket Closed

whenever I turn on logging.

No matter where I tried to implement
((ChannelExec)channel).getErrStream();  that message shows up.

What is the proper way to gather stderr for the remote command?  Or is
the above message something that can be ignored.

My code:

public class SecureCommandUtility
{       
        
        static {
                // Don't check host key
                Properties config = new Properties();
                config.put("StrictHostKeyChecking", "no");
                config.put("Protocol", "2");
                config.put("Ciphers", "arcfour,arcfour128");
                config.put("Cipher", "arcfour");
                config.put("GSSAPIAuthentication", "no");
                config.put("PreferredAuthentications", "publickey");
                JSch.setConfig(config);
        }
        
        public static SecureCommandResponse
executeRemoteCommandWithPrivateKey(String hostName, String userName,
String keyFile, String command) throws Exception
        {
                SecureCommandResponse response = new SecureCommandResponse();
                
                try{
                    JSch jsch = new JSch();
                    jsch.addIdentity(keyFile);
                
                    Session session = jsch.getSession(userName, hostName, 22);
                    session.setTimeout(5000);
                    session.connect();
                
                    Channel channel=session.openChannel("exec");
                    ((ChannelExec)channel).setCommand(command);
                
                    channel.setInputStream(null);
                   ((ChannelExec)channel).setErrStream(System.err, false);
                   //((ChannelExec)channel).getErrStream();
                    InputStream in =  ((ChannelExec)channel).getInputStream();
                
                    channel.connect();
                
                    ArrayList<String> stdout = new ArrayList<String>();
                    BufferedReader reader = new BufferedReader(new
InputStreamReader(in, "UTF-8"));
                    while(true) {
                        if ( in != null ) {
                                String line;
                                while ((line = reader.readLine()) != null) {
                                        stdout.add(line);
                                }
                                if(channel.isClosed()){
                                        response.returnCode = 
channel.getExitStatus();
                                        break;
                                }
                                try {
                                        Thread.sleep(1000);
                                }catch(Exception ee){}
                        }
                    }
                
                    response.stdOut = stdout;
                
                
                    channel.disconnect();
                    session.disconnect();
                
                }catch(Exception e){
                    System.out.println(e);
            }


                return response;
                
        }
        

If you can point me in the right direction it would be greatly appreciated...


Thanks,
Chad


PS.  Great utility by the way....



-- 
A grasshopper walks into a bar and the bartender says "Hey, we have a
drink named after you." And the grasshopper says "Really, You have a
drink named Murray?"

------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to