Is it possible to run some SSH commands using SFTP?

11,296

You can only run commands on the local system using SFTP. Run help to see what commands are available:

sftp> help
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp grp path                     Change group of file 'path' to 'grp'
chmod mode path                    Change permissions of file 'path' to 'mode'
chown own path                     Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                                   filesystem containing 'path'
exit                               Quit sftp
get [-afPpRr] remote [local]       Download file
reget [-fPpRr] remote [local]      Resume download file
reput [-fPpRr] [local] remote      Resume upload file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-afPpRr] local [remote]       Upload file
pwd                                Display remote working directory
quit                               Quit sftp
rename oldpath newpath             Rename remote file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

As can be seen, there's no SFTP command that:

  • allows running commands on the remote system. There's only ! for local commands.
  • allows appending to or editing a file. You can get the file, edit it locally and put it back.

I wouldn't expect anything like this to exist, since a common use case for SFTP is to jail users and provide file access without the ability to run any commands.

Instead of zipping, maybe use the -C option with sftp:

 -C      Enables compression (via ssh's -C flag).
Share:
11,296

Related videos on Youtube

wpcoder
Author by

wpcoder

Updated on September 18, 2022

Comments

  • wpcoder
    wpcoder over 1 year

    Having me not knowing much about Ubuntu; this is my first question here. So excuse me if it looks invalid question.

    I was told that its possible to run some SSH commands using SFTP.

    If that is possible; what are the list of supported commands to run over SFTP remotely? (SSH commands or SSH emulation); not the PUT, GET etc..

    The scenario I am trying to achieve is the following:

    1. Zip some big number of files.
    2. Upload through sFTP.
    3. Unzip using sFTP (if supported); Not by SSH

    Or at least if that is possible; to crease some bash script and run through sFTP

    Update: I have another related question; Is it possible to edit a file using SFTP? Append or edit one line in remote file?

    Dose that need sFTP server combatibilty?

    Please advice and if you can share a link to the full list or further reading! Thank you

    • sudodus
      sudodus over 6 years
      If you have permissions, you can connect to a server via ssh in one terminal window and sftp in another window side by side. So you need not (and cannot) run ssh commands using sftp.
  • wpcoder
    wpcoder over 6 years
    dose this applies to sftp? how to implement in WinSCP? without a terminal
  • wpcoder
    wpcoder over 6 years
    Dose this means that the sftp protocol doesn't support shell commands? From remote client or WinSCP; a bit confused, I can see that WinSCP can send commands via same sftp connection , port 22 , so I guess its supported by sftp; is this right?
  • muru
    muru over 6 years
    @wpcoder it's a file transfer protocol. There's no reason for it support shell commands
  • wpcoder
    wpcoder over 6 years
    I am just making a research if that is possible, one sftp vendor's admin told me that sftp protocol support zip/unzip and remote file edit..just need to confirm... sftp protocol not ssh protocol. Thank you
  • Rinzwind
    Rinzwind over 6 years
    No and you cant. Command line only.