How do I execute FTP commands on one line?

  • This is what I'm doing when I log into a FTP:

    ftp user:[email protected]
    ftp: user:[email protected]: Unknown host
    ftp> echo HELLO WORLD!
    ftp> quit

    I'd like to do a one-line FTP command...

    ftp user:[email protected] -command "echo HELLO WORLD"


    "echo HELLO WORLD" | ftp user:[email protected] 

    Something similar... as part of a script I'm trying to create. Nothing is getting transferred, I just need to echo some instructions and this is the easiest way I've found to do it between two of my systems.

    Wait, FTP is *not* for that! FTP stands for File Transfer Protocol. You need SSH.

    when you need to drive some nails and all you have is an ironing board... you make do with what you have. I'm trying Telnet, but FTP works. Limited control of second system and what not... FTP echo works even if it's an "Ironing board".

    Well, it didn't work for me... `[email protected]:~$ ftp ftp> echo "hi" ?Invalid command ftp> `

    `ftp` doesn't have an `echo` command. `lftp` does, but it's purely local, so I don't see why you'd want to do that by itself....

    @nickguletskii Yeah... I was trying to get an answer to what I was doing wrong (maybe I could have been more clear). Charles has the answer that works for me (in addition to a cli-php solution that I got on SO).

    @poolie lftp "-e 'echo HELLO WORLD!'" works for me. Command runs on the secondary system.

    @WernerCD how do you know it's running on the remote system?

    @poolie it's a command that sends a pager message. I send the command and I get the message on my phone. It's a little extra effort, basically, to use our existing paging system so I don't have to duplicate any of what's already setup (Phone numbers, scheduler, lists, etc).

  • That really is more of a job for SSH (as others have pointed out), but if you're determined to use ftp, try lftp. It's freely available for all currently supported versions of Ubuntu. You can install it with the command sudo apt install lftp

    lftp -u username,password -e "your command;quit"

    lftp documents a -c switch that runs the command and then quits, but it appears to be broken in most distributions. -e will keep you connected unless you issue a quit.

    Eeeeew... Plain text passwords...

    ``-c`` does work fine for me on Ubuntu.

    You don't need to put the password on the command line: one good option is to put it in `~/.netrc` and make sure sure that's mode `0600`. FTP can negotiate non-plain-text passwords on the wire.

    That works! Awesome. Yes... it's not a "Good" solution, but my options are limited on the second machine so FTP, while ugly, works.

  • I found this thread when I was searching for a way to have a single ftp command execute a file transfer from this machine to the ftp server. Here is how:

    Create a file with the ftp commands in it: (call it 'ftpcommands.txt')

     user YourUserName YourPassword
     put localfilename remotefilename

    Then run the ftp command and feed the file into it:

     ftp -n < ftpcommands.txt

    The -n option keeps ftp from trying to log in automatically when it receives you 'open' command.

    Hope THAT helps someone. I couldn't find anything online that was this solution, so I had to figure it out myself.

    NOTE: if your file transfer fails with a "200" response, try adding the command "passive" right before the file transfer to enter passive mode.

  • You can't use FTP for executing commands remotely. It stands for File Transfer Protocol. What you actually need is SSH.

    All you need to do is install the package ssh on both machines and then follow this guide to set up password-less logins.

    And now how to call it:

    ssh [email protected] echo "Hello World\!"

    For example, this is me doing it to myself:

    [email protected]:~$ ssh [email protected] echo "Hello World\!"
    Hello World!

    second system doesn't have SSH. I've actually got something working in PHP via FTP_RAW, but I will see if I can't get this working at some point.

  • Complementing @charlesbridge answer, include in your ~/.bashrc file the following:

    alias yoursite="lftp -u <username> -e \"<commandA;commandB>\""

    Source the file:

    source ~/.bashrc

    Now use the alias "yoursite" to logon and execute as many commands as you would like.

  • Install busybox using:

    apt-get install busybox

    busybox can work on Linux or embeded system both, then use ftpget and ftpput

    busybox ftpput -u USER -p PASSWD URL_FTPSERVER FILE_U_WANT
    busybox ftpget -u USER -p PASSWD URL_FTPSERVER FILE_U_WANT

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM

Tags used