Question

I am executing a rm command via ssh using the jsch library. The command is being sent and executed correctly. But when the file I want to delete is not found in the directory, I get an error message as follows:

No such file or directory

I want to store the error message in a variable and use it afterwards.

Currently this message is displayed when the following are executed:

((ChannelExec)channel).setErrStream(System.err);  
 InputStream in=channel.getInputStream();
 channel.connect();

after the channel.connect() the message error mesg is displayed. How can I store it in a variable let's say string and print it after completion?

Below is the main class.

        String SSHHOST = "127.0.0.1";
        int SSHPPORT = 22;
        String SSHPUSER = "user";
        String SSHPPASS = "pass";    
            StringBuilder result = new StringBuilder();
        System.out.println("Connecting To Server "+SSHHOST);
        JSch jsch = new JSch();
        Session session=jsch.getSession(SSHPUSER, SSHHOST, 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(SSHPPASS);
            session.connect();
            Channel channel=session.openChannel("exec");
            ((ChannelExec)channel).setCommand("find /home/test/DC* -print -exec rm {} \\;|wc -l");
            channel.setInputStream(null);                         
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ((ChannelExec)channel).setErrStream(bos) ;            
            ((ChannelExec)channel).setErrStream(System.err);             
            InputStream in=channel.getInputStream();
            channel.connect();        
            String result2 = bos.toString();                       
            System.out.println(result2);
            byte[] tmp=new byte[1024];
            while(true)
            {
              while(in.available()>0)
              {
                int i=in.read(tmp, 0, 1024);
                if(i<0)break;
                out=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();
            session.disconnect();
            int status = channel.getExitStatus();        
            System.out.println(out.trim());
            if  (channel.getExitStatus() == 0)
            {
                System.out.println("Perso file purging process completed successfully.");
                System.out.println("exit-status: "+channel.getExitStatus());
                System.out.println("Number of perso files deleted : "+out);                 
            }
            else 
            {
                System.out.println("Perso file purging process completed with errors.");
            }

I am still unable to get the error message when the files are not found in the directory with the above code.

Was it helpful?

Solution 2

i have been able to store the error in a file by using FileOutputStream

FileOutputStream fos = null;
File file = new File("errors.txt");
try 
{
  fos = new FileOutputStream(file);
} 
catch(IOException ioe) 
{
  System.err.println("redirection not possible: "+ioe);
  System.exit(-1);
}
PrintStream ps = new PrintStream(fos);            
((ChannelExec)channel).setErrStream(ps);

OTHER TIPS

This is a bit of a guess from me but .....

You seem to be setting the error stream of your channel object to the System.err of your application - unless you have reconfigured the System.err of your application to point to somewhere else, that will be the console.

What arguments does setErrStream take? If it's an OutputStream, could you get it to write to a ByteArrayOutputStream and then build a string from that?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top