Is there a way to write a script to do ftp login so I don't have to type things over and over again?

37,124

Solution 1

Usually, ftp command line clients support the configuration file ~/.netrc where you can configure credentials for remote systems, e.g.:

machine legacy.system.example.org
login juser
password keins

When you ftp legacy.system.example.org then you don't have to retype this information anymore.

If you need to do more automation, you can script ftp via piping commands into it, e.g.:

$ cat pushit.sh
# complex logic to set
# EXAMPLE_FILE=
ftp <<EOF
prompt
mput $EXAMPLE_FILE
quit
EOF

Sure, if the system does not support ssh, it probably does not support ftps either - but you can try it (e.g. via ftp-ssl) if you need to secure your connection.

LFTP

An alternative to one of the plain ftp commands is to use lftp, since it provides several features to automated login and command execution.

Example:

$ lftp -e 'source ~/login.lftp'
$ cat login.lftp
open sftp://juser:[email protected]
cd /path/to/favorite/dir

Note that this example shows automated password authentication to a SFTP server which is not supported by the standard OpenSSH sftp client.

The option -e instructs lftp to execute the commands at startup and stay interactive.

Such an lftp script might also source other scripts, automatically disconnect from the server, etc.

In contrast, with -c or -f lftp directly exits after executing the commands specified as argument or read from the specified file.

Solution 2

If you want to script out the whole thing:

#!/bin/sh
HOST='ftp.example.com'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0

See also this web page.

Solution 3

If you want to automatically log in, you can use a .netrc file. I would NOT recommend this as you would have to store your username and password in plain text which is unsecure. If you can use ssh, then I'd recommend using that with authentication keys to automatically log you in.

If you cannot use a secure method such as ssh with keys, you can configure your ftp information by following this guide.

Share:
37,124

Related videos on Youtube

csharpdev
Author by

csharpdev

Ninja is too unreliable;

Updated on September 18, 2022

Comments

  • csharpdev
    csharpdev over 1 year

    I am recently working extensively with a remote system via ftp connection, since the session time is so short that I have to re-login quite often when I finish my work on my local machine, so I need a way to create a custom command/shell script to login to the ftp server with just one word, but the question is how to do it.

    e.g.

    ~$ ftp domainname.com
     ...
     Name (domainname.com): MyName
     ...
     Password: xxxx
     ...
     ftp>
    
    • n0pe
      n0pe over 12 years
      Just a question, why ftp and not ssh? ssh is much more secure and logging in like this would just be a simple bash alias
    • sbtkd85
      sbtkd85 over 12 years
      Also, with SSH you can use authentication keys as well which will automatically log you in without having to store your username/pass in a .bashrc file as plain text.
    • csharpdev
      csharpdev over 12 years
      well, I suppose the remote system has support for ssh, but it doesn't.
  • Ekeyme Mo
    Ekeyme Mo over 7 years
    LFTP is much better.