BenG
BenG

Reputation: 143

Why doesn't cd command work using Java JSch?

I'm just learning Java and Jsch, and I can get it to run other commands but not cd. The error code returned by the SSHManager sendCommand function is not null, but some unreadable string that is different every time (maybe that means it is null not that familiar with inner workings of Java).

Any idea why not? Similar question here JSch - Why doesn't CD work? but unanswered.

I won't copy and paste the whole SSHManager class here - useful answer with complete code here that I'm trying to follow. Run a command over SSH with JSch

Sample code below:

import SSH.SSHManager;
public class src
{
    int ERROR = 0;
    public static void main(String[] args)
    {
        String username = "debian";
        String password = "temppwd";
        String ipadd = "192.168.7.2";
        SSHManager ssh = new SSHManager(username, password, ipadd, "");
        ssh.connect();

        String out = "";

        //this doesn't work, printing output as bytes to show how weird it is
        out = ssh.sendCommand("cd Desktop");
        System.out.println(out.getBytes());

        //some other test commands
        out = ssh.sendCommand("mkdir test");
        System.out.println(out);
        out = ssh.sendCommand("ls");
        System.out.println(out);
        ssh.sendCommand("logout");
    }
}

Output from the Eclipse Console (bin and Desktop are already there in root directory):

[B@b065c63

bin
Desktop
test

Upvotes: 1

Views: 1826

Answers (3)

Martin Prikryl
Martin Prikryl

Reputation: 202504

Each command executed over SSH "exec" channel (what is behind SSHManager.sendCommand) is executed in its own shell. So the commands have no effect on each other.

To execute multiple commands in the same shell, just use an appropriate syntax of your server shell. Most *nix shells use semicolon or double-ampersand (with a different semantics).

In your case, the double-ampersand would be more appropriate.

cd Desktop && mkdir test && ls

See also Multiple commands using JSch.


Though, if your want to read commands output, you will have problem distinguishing, where output of one commands ends and output of the following commands starts. Let alone if you wanted to check command exit code.

Then it's better to execute each command in its own "exec" channel in a way that does not require a context. In your case that means using full paths:

mkdir Desktop/test
ls Desktop

See also How to perform multiple operations with JSch.


Also as you were going to use file manipulation only, you actually should not execute shell commands at all. Use the standard SSH API for file manipulation, the SFTP.

Upvotes: 2

Lee David Painter
Lee David Painter

Reputation: 510

I answered a similar question Using java jcabi SSH client (or other) to execute several commands in shell

My open-source API Maverick Synergy has a high-level API to execute multiple commands within a shell. Its currently designed for and works well with bash-type shells.

Upvotes: 0

Horatiu Jeflea
Horatiu Jeflea

Reputation: 7414

The question has kind of an answer, in the comments. Every command in sendCommand uses it's own 'pipe', so it disconnects and starts over in each one.

A quick solution would be to send multiple commands in one one sendCommand, such as:

out = ssh.sendCommand("cd Desktop; mkdir test; ls; logout");

But the correct way is to use a session, such as https://stackoverflow.com/a/9269234/290036

Upvotes: 0

Related Questions