Operating System - HP-UX
1753325 Members
5277 Online
108792 Solutions
New Discussion юеВ

Script for scp of file to multiple servers using ssh.

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Script for scp of file to multiple servers using ssh.

From a begining scritper, how would this be done? I want to push out one file to the same location on multiple servers. I am still researching this, but if someone has the answer, it would save me a lot of time.
8 REPLIES 8
Biswajit Tripathy
Honored Contributor

Re: Script for scp of file to multiple servers using ssh.

If you don't mind using rcp, then you could do
something like this:

SERVER_LIST="server1 server2 server3"
for server in $SERVER_LIST
do
rcp /local/file $server:/remote/file
done

The problem with this is, you will have to configure
$HOME/.rhosts file in each server and this is
considered, in general, as a bad practice.

- Biswajit
:-)
Robert Bennett_3
Respected Contributor

Re: Script for scp of file to multiple servers using ssh.

for i in server1 server2 server3
do
scp -p /roots/bin/XXXX $i:/roots/bin
done

That should do it fairly easily - just change the locations of where your file is and where you want to put it.

Hope this helps

B
"All there is to thinking is seeing something noticeable which makes you see something you weren't noticing which makes you see something that isn't even visible." - Norman Maclean
Denver Osborn
Honored Contributor

Re: Script for scp of file to multiple servers using ssh.

LIST="hostA hostB hostC"
FILE="/path/myfile"

for NODE in $LIST
do
echo xfer $FILE to $NODE
scp -p $FILE $NODE:$FILE
done



If you wanted to automate the task by using public key auth, use "scp -B -p -i identity_file" syntax instead.
Steven E. Protter
Exalted Contributor

Re: Script for scp of file to multiple servers using ssh.

I'd use this very simialar technique


read -r servername
do
scp -p filename $servername:/location
done < serverlist


serverlist is a manually done list of servers.

This assumes you have password free ssh/scp etc set up, working and testing before you begin.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Nguyen Anh Tien
Honored Contributor

Re: Script for scp of file to multiple servers using ssh.

rcp is much easier than scp
for i in ser1 ser2 ser3
do
rcp -p /source $i:/destination
done
To SEP:
how to provide password to server when server ask you password when You use scp to copy
HP is simple
Muthukumar_5
Honored Contributor
Solution

Re: Script for scp of file to multiple servers using ssh.

You can use this script as,

==server.txt==
server1
server2

#!/bin/ksh
# copy files

# File to be copied
FILE="/tmp/testfile"
LOCATION="/tmp/"

while read server; do

scp -p $FILE $server:$LOCATION

done < server.txt

It will do it.
Easy to suggest when don't know about the problem!
Naveej.K.A
Honored Contributor

Re: Script for scp of file to multiple servers using ssh.

Hi,

I would go with scp and not with Tienna's solution for rcp. Following is the procedure for setting up passwordless ssh

Passwordless SSH using public/private keys

First server setup
ssh-keygen -t dsa(press enter twice to give a blank password)

cd
cd .ssh
vi .configPress "i" to enter insert mode and copy this into the file:

Host remotehost
User remoteuser
Compression yes
Protocol 2
RSAAuthentication yes
StrictHostKeyChecking no
ForwardAgent yes
ForwardX11 yes
IdentityFile /home/localuser/.ssh/id_remotehost_dsaDo NOT change the last line - it is supposed to say remotehost (not an actual host name). Now,

:wq(save and exit vi)

vi id_dsa.pubIt should look like this:

ssh-dss AAAA..............v root@HOSTNAMEOFSRV01

where there is lots of random letters/numbers where the dots are. Select it all and copy it. Make sure that it is all on one line with no spaces at the start or finish (which will happen if you copy it using putty on windows; test it by pasting it into notepad)
Tip: To copy from putty on windows select the text from within vi and pres Ctrl + Shift. To paste text enter insert mode and press the right mouse button.

Second Server Setup
cd
vi .ssh/authorized_keys

Enter insert mode (press i) and paste the key, again ensuring that there are no spare newlines or spaces. Save the file and exit vi (press :wq then return, as above)

Testing passwordless SSH
On the first server, type

ssh srv02where srv02 = the hostname of the second server. It could be an IP address too.

If it just logs you in (no passwords), then you are done.

Regards,
Naveej
practice makes a man perfect!!!
Patrick Ware_1
Super Advisor

Re: Script for scp of file to multiple servers using ssh.

I found my answer! It worked great!