9

I want to automate a call to ftp in a shell script. If I type

$ftp somehost.domain.com

I am prompted for a username and password. I want to give that username and password as part of the call to ftp. The man page for ftp says I can issue a user command at the ftp prompt -- but I want to login to ftp all in one go. Is that possible? I don't see anything in the flags for ftp. I see that the -s option give me the option of specifying some ftp commands once I have the ftp prompt -- but I need to give the user name to get to the prompt...

  • expect (a tcl/tk extension of sorts) is a program that's ideal for this sort of things - ie. wait for some prompt; give a reponse... wait for an answer; give a new response... expect wait for "ogin:" and send you username, then it wait for "assword:" and send your password. – Baard Kopperud Aug 18 '14 at 9:46
9

Use a .netrc file in your home directory.

The content is:

# machine <hostname> login <username> password <password>
machine ftp.example.com login myuser password $ecret

If this is something you're doing programmatically, write the .netrc before connecting, delete it when you're done.

  • is this file specific to ftp or is it read by other programs also? – terdon Feb 11 '14 at 20:17
  • The netrc(5) man page says that it's "for ftp(1)" but I'm not sure that ftp is the only thing that will use it. – bahamat Feb 11 '14 at 21:51
  • If you want to find your ".netrc" file just use this command: locate '.netrc' – goamn Jun 5 '17 at 22:52
1

You can also write a c shell script using a here doc which would go something like this:

#!/bin/sh
HOST='someftpserver.com'
USER='username'
PASSWD='password'
FILES='*.txt'
#
ftp -nv $HOST>$FTPLOG <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd somedirectory
binary
mget $FILES
quit
END_SCRIPT
0

You could also consider wput or wget as alternatives, depending on your needs.

I haven't used wget recently but I have used wput because I didn't want to create or modify files as suggested in some earlier answers.

For wput, the syntax is:

wput <filename> ftp://<username>:<password>@<address>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.