Operating System - HP-UX
1827283 Members
3436 Online
109717 Solutions
New Discussion

Writing a script for single file transfer to all servers

 
Evans Kelley
Occasional Advisor

Writing a script for single file transfer to all servers

Hi Everyone,

I am new to writing scripts and HP-UX, so the question I have, I am sure is very simple to a lot of you so please excuse.
I need to transfer a single file from one UNIX server to the rest of our UNIX servers in the same domain, I was wondering how (examples or suggestions) I could write a script that would help me do this so I don???t have to login into each and every server to copy this file. By the way I would like to do this in korn shell environment.

Thanks in advance,
Evans
12 REPLIES 12
harry d brown jr
Honored Contributor

Re: Writing a script for single file transfer to all servers

If the SENDING server is considered a TRUSTED server to the rest you can use the .rhosts file and use "rcp" to send the files, but if the SENDING server is not considered a TRUSTED server to the RECEIVING servers, then you should either use "ssh" (you need to install it) or "ftp"'s (which you can script easily using "perl").

live free or die
harry
Live Free or Die
Patrick Wallek
Honored Contributor

Re: Writing a script for single file transfer to all servers

If you have rcp access between the machine with the file and all others then something like:

#!/usr/bin/sh

MACHINES="daffy donald mickey minnie pluto goofy"

for i in $MACHINES
do
rcp /dir/filename $i:/dir/filenameecho "file copied to $i"
done

This should get you pointed in the right direction. I make no guarantees on the syntax since I don't have a machine to test on at the moment.
Steven Mertens
Trusted Contributor

Re: Writing a script for single file transfer to all servers

hi,

the way i should do it

- create a file ex. ftpserver.conf
here your put the servers where to
put a file

server1
server2
...

- Second you create the script :

login=
password=
remote_path=
remote_file=

while read server_name
do

ftp -nv << BeginFtp
open ${server_name}
user ${login} ${password}
prompt
cd ${remote_path}
mput ${remote_file}
bye
BeginFtp

done < ftpserver.conf

note that is just an example , did test
if it works , but normally it should.
It also not really secure because the
password is hardcoded in the script.

Regards,

Steven
Rich Wright
Trusted Contributor

Re: Writing a script for single file transfer to all servers

Here is a simple script that I use to copy the motd to remote servers.
#!/usr/bin/ksh
syslist="sys1 sys2 sys3 sys4 sys5"
syslist=$(echo $syslist | sed "s/$(hostname)//")
for sys in $syslist
do
if `rcp /etc/motd $sys:/etc/motd`
then echo "Copied to $sys..."
else echo "Copy to $sys FAILED..."
fi
done
echo "** Done now!"
Evans Kelley
Occasional Advisor

Re: Writing a script for single file transfer to all servers

Patrick,

Perfect, but now I need to change permission of owner and group to root and sys. Any suggestions I would appreciate.
Thanks everyone for the direction and quick response. Wow! You guys/gals are great! Nice forum.
Patrick Wallek
Honored Contributor

Re: Writing a script for single file transfer to all servers

OK, the new script would be:

#!/usr/bin/sh

MACHINES="daffy donald mickey minnie pluto goofy"

for i in $MACHINES
do
rcp /dir/filename $i:/dir/filename
remsh $i chown root:sys /dir/file
echo "file copied to $i"
done
harry d brown jr
Honored Contributor

Re: Writing a script for single file transfer to all servers


use

remsh REChostname chown root:sys filepath_on_RECHOST


live free or die
harry
Live Free or Die
Evans Kelley
Occasional Advisor

Re: Writing a script for single file transfer to all servers

Ok, last question???I promise :v)
The issue that I am running into now is that I get and error because the file already exists on these servers and when I try to move it to an old name (which I would like to do) I get permission denied. Now I am assuming this is because the ID that I am trying to use doesn???t have permission to do an rm or an mv.

Thanks again everyone.
Nick Wickens
Respected Contributor

Re: Writing a script for single file transfer to all servers

If you wish to keep early copies and still want the same file available to all the servers why not create a small filesystem on the system that creates the file into which you can store the file/copies and then use NFS to mount it on to all of the other servers.

That way you only have to maintain the file in one place.

To do this you need to create a filesystem, make sure that the nfs daemons are running, add the filesystem name to /etc/exports and then run the command exportfs -a.

Then on the other systems create a directory with the same name & then create an entry in /etc/fstab to mount the remote filesystem or at command line enter - mount machine1:/nfsdir /nfsdir

Its actually a little more involved than that but its way past my bedtime - This should give you a few pointers to do some searches on this forum for more in depth use of NFS (Also see SAMBA or CIFS.)
Hats ? We don't need no stinkin' hats !!
Andrew Cowan
Honored Contributor

Re: Writing a script for single file transfer to all servers

Hi Evans,

If you want to do this in a secure/trusted environment, install OpenSSH on all systems and create a common user with limited rights. Next switch to this user and create a public/private key pair, and distribute the public part to all hosts. Now you can ssh or scp to all of the systems, as this user, but without requiring a password. This gets over all the ".rhosts" type security problems and gives you an encrypted file transfer and remsh.
I use an unpriviledged user to transfer the files, then have a cron job that backs up certain system files before installing the new ones. This not only prevents a hacker from sending anything, but my script provides an audit trail.
Rainer von Bongartz
Honored Contributor

Re: Writing a script for single file transfer to all servers


Why play around with scripts and remsh and permission problems.

You could easily perfom this task and keep synchronized copies of files on all your servers use the rdist command

See: man rdist

Regards
Rainer
He's a real UNIX Man, sitting in his UNIX LAN making all his UNIX plans for nobody ...
Daimian Woznick
Trusted Contributor

Re: Writing a script for single file transfer to all servers