problem running several command over SSH with JSch

1

I'm trying to run several command over SSH with JSch.The different command works fine but I have a problem: it looks like the system restart from the root folder after any cmd, if I try to read in another folder, simply I can't because I will read again the root folder. Consider I'am enough new in this.

This is my method that menage the command (it is composed by parts founded here on stackOverflow):

public String ask (String command) throws SftpException, IOException, JSchException {
        StringBuilder outputBuffer = new StringBuilder();
         try
         {

        Channel  channel = session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
        InputStream commandOutput = channel.getInputStream();
        channel.connect();
        int readByte = commandOutput.read();

        while(readByte != 0xffffffff)
        {
           outputBuffer.append((char)readByte);
           readByte = commandOutput.read();
        }

        channel.disconnect();
     }
     catch(IOException ioX)
     {
        System.out.println("PROBLEMI NEL CHANNEL DI SESSIONE");
        return null;
     }
     catch(JSchException jschX)
     {
        System.out.println("ALTRI PROBLEMI");
        return null;
     }
     System.out.println(outputBuffer.toString());
     return outputBuffer.toString();
}

No error/exception. I am tryng to execute 2 simply conseguent cmd like: "cd " and "dir".

So, as suggest by Martin Prikryl, "concatenate" the cmd like "cd nameFolder && ls". For my test porpuse it is enough, if someone else have some more ideas...

java
ssh
jsch
asked on Stack Overflow Feb 27, 2019 by Michael Mucci • edited Feb 27, 2019 by Michael Mucci

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0