sshj icon indicating copy to clipboard operation
sshj copied to clipboard

How to SFTP when SSH is disabled

Open mvanle opened this issue 5 years ago • 2 comments
trafficstars

My company has disabled SSH, but allows SFTP.

For example, "sftp" commands will work but "ssh" will not.

They may have configured something like this in "/etc/ssh/sshd_config":

Match Group sftp-only
ForceCommand internal-sftp
ChrootDirectory /pub/sftp
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no

The typical pattern does not work:

import net.schmizz.sshj.*;

...

void connect() {
    SSHClient sshClient; 
    SFTPClient sftpClient;

    sshClient = new SSHClient();
    sshClient.connect(hostname, port);  // Port = 2233
    sshClient.authPublickey(username, privatekey);
    sftpClient = sshClient.newSFTPClient();
}

The above code throws an exception at "sshClient.connect()":

Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at net.schmizz.sshj.SocketClient.connect(SocketClient.java:126)

Is there a way to get a SFTP implementation without instantiating SSHClient ?

Or does anybody know of a solution (or have I misunderstood something) ?

mvanle avatar Jul 10 '20 15:07 mvanle

connection refused means that either the port is blocked or firewalled. You need the SSHCLient to use SFTP.

hierynomus avatar Jul 10 '20 17:07 hierynomus

connection refused means that either the port is blocked or firewalled. You need the SSHCLient to use SFTP.

Just curious - why would the Unix sftp -oPort=2233 command work ? -- is it because the SSH implementation on the server is somehow compatible with (or can specifically handle) the sftp command but not sshj's SSHCLient ?

Have other people experienced this kind of problem with other SFTP / SSH libraries (eg. JSch, Apache Commons VFS et al.) ?

mvanle avatar Jul 11 '20 07:07 mvanle