21

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

ftp user:password@server
ftp: user:password@server: Unknown host
ftp> echo HELLO WORLD!
ftp> quit

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

ftp user:password@server -command "echo HELLO WORLD"

or

"echo HELLO WORLD" | ftp user:password@server 

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.

Kris Harper
  • 13,705
WernerCD
  • 652

7 Answers7

25

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" ftp.site.com

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.

Elder Geek
  • 36,752
24

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')

 open YourFtpServer.com
 user YourUserName YourPassword
 put localfilename remotefilename
 bye

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.

Jack
  • 241
2

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
Kulfy
  • 18,154
2

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 username@host echo "Hello World\!"

For example, this is me doing it to myself:

nick@AccessDenied:~$ ssh nick@localhost echo "Hello World\!"
Hello World!
1

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

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

Source the file:

source ~/.bashrc

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

muru
  • 207,228
Alan
  • 111
  • 4
0

Let's assume for the time being you are a hobbyist user or are doing this in a sandpit fashion.

curl() can be used for FTP, even to transmit. wget (wput) also. and there is ncftp (for background asynchronous transfer) and lftp.

.netrc is a good idea, even from root.

But your "echo" is disturbing. If you want easy remote commands install putty-tools and use "plink". There is also "pscp" if you would care to check it out. Professionals may decry these tools, but they are great for your home network, cron jobs, and quick scripting.

These are SSH tools, and can be as powerful as formal products such as Ansible. They require an SSH server, and open ports to reach it, and possibly identity credentials. I apologise if if SSH is off topic for you, the type of script seen in the popular answer by Jack is often seen in sftp transfers (note the "s").

As an aside, you can run CGI like commands from FTP, but this is not generally spoken of, nor publicly encouraged. It involves watching for the transmitted file or using a write only "dummy" file on a customised server.

(Arcane example follows). In the "TK4-" distribution of MVS3.8 on the Hercules (now Hyperion) mainframe emulator, remote job entry is effected by FTP transfer to the file AAINTRDR which stands in for a card reader to read the "cards" in your file.

mckenzm
  • 383
0

Coming to this late, but you can just do this:

ftp ftp://<FTP SERVER>/<FILE NAME> <FILE NAME>

Or, if your server isn't anonymous:

ftp ftp://<USER>:<PAS>@<FTP SERVER>/<FILE NAME> <FILE NAME>

Locane
  • 190
  • 1
  • 9