Automate your ftp operations

In our day to day operations , sometimes we will need to automate ftp operations. Today one of my client asked me to setup a cronjob to download files from remote machine using ftp

We can do this in two ways

1) FTP automation

vi /usr/local/scripts/ftp-auto.sh

#!/bin/bash
HOST='adminlogs.info'
USER='ftpadmin'
PASSWD='password'
ftp -n -v $HOST << EOT
ascii
user $USER $PASSWD
prompt n Interactive mode Off
mkdir linux
cd linux
bye
EOT
sleep 3

I have included an example , you can add your ftp operation’s after “prompt “

 

2) SFTP automation

vi /usr/local/scripts/sftp-auto.sh

#!/bin/bash
HOST="adminlogs.info"
USER="ftpadmin"
PASS="password"
FIRE=$(expect -c "
spawn /usr/bin/sftp -o \"BatchMode no\" -b /tmp/commandfile¬† [email protected]$HOST
expect \"password:\"
send \"$PASS\r\"
interact
")
echo "$FIRE"

Note : –

You should install ” expect ” , using¬† yum install expect

You can add your own ftp commands in ” /tmp/commandfile ” .