hierynomus/sshj

Shell expansion does not work

juddgaddie opened this issue · 11 comments

It seems as though shell expansion is not supported is this correct or am I doing something wrong.

The example below will not find the file test_file
unless you explicitly specify "test_file"

  SSHClient ssh = new SSHClient();
        // ssh.useCompression(); // Can lead to significant speedup (needs JZlib in classpath)
        ssh.loadKnownHosts();
        ssh.connect("localhost");
        try {
            ssh.authPublickey(System.getProperty("user.name"));
            ssh.newSCPFileTransfer().download("*_file", new FileSystemFile("/tmp/"));
        } finally {
            ssh.disconnect();
        }

Thanks

The stack trace is:

Exception in thread "main" net.schmizz.sshj.xfer.scp.SCPException: Remote SCP command returned error: scp: *_file: No such file or directory
at net.schmizz.sshj.xfer.scp.SCPDownloadClient.process(SCPDownloadClient.java:126)
at net.schmizz.sshj.xfer.scp.SCPDownloadClient.startCopy(SCPDownloadClient.java:79)
at net.schmizz.sshj.xfer.scp.SCPDownloadClient.copy(SCPDownloadClient.java:50)
at net.schmizz.sshj.xfer.scp.SCPFileTransfer.download(SCPFileTransfer.java:69)
at net.schmizz.sshj.SCPDownload.main(SCPDownload.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

I have found this is caused by SCPEngine.java quoting the source path:

SCPEngine.java line 112:
cmd.append("'").append(path.replaceAll("'", "'")).append("'");

Why the requirement to quote, could we optionally not quote ?

linked to #184

Have you verified it works without quoting?

Yes

OK, what do you suggest as a good approach then? As said in #184 not quoting is not a good idea. Double quotes could work I think, but do they work always?

Double quotes does not solve the issue. If you want to escape space char then \ works, on Linux at least. I am not sure if a universal escaping solution exists across operating system, file systems and ssh servers. Leaving escaping to the user or at least a way of opting out of escaping is required for sshj to at least be functional without a universal escaping mechanism.

I am currently using jsch and unfortunately can't move to sshj until this is resolved.

Could we start compiling a list of all the characters that need to be escaped, across platforms and see if there is a common set?

In the mean time can we leave escaping to the user, or at least make it optional by providing a Typed interface, eg. RemoteFile which will be escaped and String remoteFile which leaves it up to the user?

Thanks for this patch, I'll test it out.

Let me know. It seems that actually Apache Mina does not need any escaping. I have not had the time to test it out on OpenSSH and others. If you could let me know the results of your tests?

It's working for our use cases with openssh.