Automated FTP script using .netrc to multiple FTP servers

Hi all,

I'm using the following script to automated ftp files to 1 ftp servers

host=192.168.0.1

/usr/bin/ftp -vi >> $bkplog 2>&1  <<ftp
open $host
bin
cd $\{directory\}
put $files 
quit

ftp

and the .netrc file contain

machine 192.168.0.1
login abc
password 12345

What i would like to achieve is to ftp files to more target servers.

The easiest option would be to add the second server in .netrc

machine 192.168.0.1
login abc
password 12345

machine 192.168.0.2
login ab
password 12345

and modify the script

host=192.168.0.1
host1=192.168.0.2

/usr/bin/ftp -vi >> $bkplog 2>&1  <<ftp
open $host
bin
cd $\{directory\}
put $files $\{files\#\#*/\}
quit

ftp

/usr/bin/ftp -vi >> $bkpg 2>&1  <<ftpj
open $host1
bin
cd $\{directory\}
put $files $\{files\#\#*/\}
quit

ftpj

Can you think of another solution of changing the script, let's say

if host1 is set than use

/usr/bin/ftp -vi >> $bkpg 2>&1 <<ftpj
open $host1 & $host
bin
cd ${directory}
put $files ${files##*/}
quit
ftpj

if not use only

/usr/bin/ftp -vi >> $bkpg 2>&1 <<ftpj
open & $host
bin
cd ${directory}
put $files ${files##*/}
quit
ftpj

Regards

you can parse the .netrc file the use a loop to go through the machines. If you have Python on your system, here's an alternative

#!/usr/bin/env python
import ftplib
machine=[]
login=[]
password=[]

def upload(ftp, file): 
    ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
        
for line in open("/etc/.netrc"): #read netrc file
    old=line.strip()
    line=line.strip().split()
    if old.startswith("machine"): machine.append(line[-1])
    if old.startswith("login"): login.append(line[-1])
    if old.startswith("password"): password.append(line[-1])
for i in range(len(machine)):
    try:
        ftp = ftplib.FTP(machine)
        ftp.login(login,password)
    except Exception,e:
        print e
    else:
        ftp.cwd("incoming")
        upload(ftp, "test.txt")

similarly done using shell scripting, parse the netrc for machines/login/password and using loop to go through them

or with bash you need to create only to files -- targes = your ip addresses -- and --- commands=you commands

from my point of view , is this one of the fastest

#!/bin/bash

IFS=$'\n'

echo "==============================="
echo " Please enter you enable pass! "
echo "==============================="

read -s -p "password: " pass
printf "%b" "\n"

cmd=`cat commands`

for ip in `cat targets`

do
echo "connect to $ip" &gt;&gt; log-uniq 
printf "USERNAME\\r\\n$pass\\r\\n$cmd\\r\\n" |  netcat $ip 21 | tee -a log-uniq

done

Thank you all for your help!
Finally i've decided to use the 2nd option where i've added a loop

 
if [-z $host1]
     then
 /usr/bin/ftp -vi >> $backupLog 2>&1 <<startftp
 open $host
 bin
 cd ${directory}
 put $files ${files##*/}
 quit
startftp
 
echo ${files##*/} Ftpd to $host >> $backupLog
(( i=i+1 ))
 
    else
      for p in $host $host1
       do
/usr/bin/ftp -vi >> $backupLog 2>&1 <<gg
 open $p
 bin
 cd ${directory}
 put $files ${files##*/}
 quit
gg
      done
  fi
 
echo ${files##*/} Ftpd to $p >> $backupLog
(( i=i+1 ))
 
done
 
echo >> $backupLog

Just have one line per machine in the .netrc file.
This also stops the password appearing in a "ps" display.

machine 192.168.0.1 login abc password 12345
machine 192.168.0.2 login ab password 12345
/usr/bin/ftp -vi $host >> $bkpg 2>&1 <<ftpj
bin
cd ${directory}
put $files ${files##*/}
quit
ftpj