Rajeev
Rajeev

Reputation: 46949

jsch change directory and get contents

In jsch if we issue the command cd ../ && pwd and if the result is /home .Next time if i execute the command ls will jsch output me the contents of /home.I do expect so ..

          Channel channel=session.openChannel("exec"); 
          ((ChannelExec)channel).setCommand("cd ../ && pwd");

          channel.connect();
          channel.run();

          ((ChannelExec)channel).setCommand("ls");

Upvotes: 1

Views: 11886

Answers (1)

linski
linski

Reputation: 5094

It looks like ChannelExec serves the purpose of executing a single command. But for a complete explanation look here.

Therefore you could rewrite the code like this:

Channel channel=session.openChannel("exec"); 
((ChannelExec)channel).setCommand("cd ../ && pwd && ls");
channel.connect();
channel.run();

Upvotes: 8

Related Questions