user3196028
user3196028

Reputation: 11

scp script to multiple hosts that require a password

I am trying to write a shell script that will, given source file, target directory, and hosts file, scp a file to multiple hosts. I have gotten the script to work however there are a few issues but the file gets there. The only problem is the network I am working on requires a password for every remote. I need to do this to 150 plus remotes and need this to run automatically, possibly with a logging feature. If any one could help me out it would be greatly appreciated. Here's what I got so far...

# This is a script to copy files from one host to a group of hosts
# There are three variables accepted via commandline
# $1 = first parameter (/source_path/source_filename)
# $2 = second parameter (/target_directory/)
# $3 = third paramter (file that contains list of hosts)
SOURCEFILE=$1
TARGETDIR=$2
HOSTFILE=$3
if [ -f $SOURCEFILE ]
then
    printf "File found, preparing to transfer\n"
    while read server
    do
        scp -p $SOURCEFILE ${server}:$TARGETDIR
done < $HOSTFILE
else
    printf "File \"$SOURCEFILE\" not found\n"
    exit 1
fi
exit 0

Upvotes: 0

Views: 3974

Answers (1)

Matt Might
Matt Might

Reputation: 121

SSH public-key authentication is a good solution here.

In short, run:

ssh-keygen -t dsa

To generate your public/private key pair.

Then, add ~/.ssh/id_dsa.pub to remote:~/.ssh/authorized_keys

Now, ssh will not ask for a password, since it will do a public-key authentication challenge instead.

I wrote an article here that describes this in some detail:

http://matt.might.net/articles/ssh-hacks/

You should not have to modify your script for this to work.

Upvotes: 2

Related Questions