Using JSch to SFTP when one must also switch user

前端 未结 1 1818
别那么骄傲
别那么骄傲 2020-12-02 00:53

I am using JSch in a Java client to connect to a remote server and get some files using SFTP. The following code has been working fine for me: -

JSch ssh = n         


        
相关标签:
1条回答
  • 2020-12-02 01:38

    I do not think you can do this directly with JSch. But with some modification of its code, it's probably doable.

    Note that my answer assumes that the server is *nix-based (what is backed by your reference to su) and uses OpenSSH SFTP server.


    You have to open SSH "exec" channel, to execute something like:

    sudo /bin/sftp-server
    

    But on top of that channel, you need to build the ChannelSftp instance, not ChannelExec.

    So you will need to implement Session.openChannel-like method, that will open exec channel, but create ChannelSftp for it.


    For some background, see how it's possible to do sudo with WinSCP SFTP client.

    Note that while the FAQ claims, that you won't be able to use password for the sudo, that's true for WinSCP. But as you have a full control of the session with JSch, you may be able to feed the password to sudo.

    For that you might override the ChannelSftp.start() to write the password to the channel input, before starting the actual SFTP session.

    You still need the requiretty option be off, as the SFTP cannot work with TTY.


    For general considerations when automating operations using a different/root account, see:
    Allowing automatic command execution as root on Linux using SSH

    0 讨论(0)
提交回复
热议问题