Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Korn Shell script to scp critical files from multiple servers

Status
Not open for further replies.

sunnysysadm

Technical User
Aug 16, 2006
5
0
0
US
I have a small script that follows that I need a little help on. The purpose of the script is to scp critical files from multiple servers to my Solaris workstation. The ssh keys are working properly and so I know that is not an issue.
Here is the script
--------------------
#!/usr/bin/ksh

SERVERS="server1 server2" <--have also tried catting file here that would contain the server names and that did not work either
files=`< /mydir/critfiles` <--contains /etc/passwd /etc/shadow and /etc/system

for x in ${SERVERS}
do
/usr/local/bin/scp $x:$files /mydir/${SERVERS}
/usr/local/bin/ssh $x "/usr/sbin/vxdisk list" >> /mydir/${SERVERS}/vxdisk_list
done

The directories /mydir/server1 and /mydir/server2 exist. The script seems to work fine when I use one server name, but when I begin using more than one server name it does not function.
Any suggestions would be appreciated.
 
You want something like:

Code:
for x in ${SERVERS}
do
/usr/local/bin/scp $x:$files /mydir/${x}
/usr/local/bin/ssh $x "/usr/sbin/vxdisk list" >> /mydir/${x}/vxdisk_list
done


I hope that helps.

Mike
 
Thanks Mike ! That definitely helps out alot. I now have a new problem with this script unfortunately, hopefully there is an easy fix for it too. It involves the files being scp'd from the servers to my system. In the "files" file mentioned in the script, there are three separate lines which are the following:
/etc/passwd
/etc/shadow
/etc/system

The /etc/passwd file correctly scp's over to my machine from the remote servers, however, the /etc/shadow and /etc/system files are being copied from my local machine instead of the remote servers into /mydir/${x}.

If I run the script as follows to view what is actually happening....

ksh -xv ./test.ksh then I see output as follows:

/usr/local/bin/scp server1:/etc/passwd /etc/shadow /etc/system /mydir/server1

So instead of handling each individual line in the "files" file, it is trying to do them all at the same time. Any tips or suggestions here would be greatly appreciated.

Sunny
 
Yes, the contents of $files is being 'put' after $x: and so you are not getting the 2nd, 3rd and subsequent filenames from $x: (but from the local server).

Using the same principle, try something like:
Code:
for x in ${SERVERS}
do
   for file in ${files}
   do
      /usr/local/bin/scp $x:$file /mydir/${x}
   done

   /usr/local/bin/ssh $x "/usr/sbin/vxdisk list" >> /mydir${x}/vxdisk_list
done


I hope that helps.

Mike
 
Thanks again Mike, that was spot on and resolved the issue. The script is working now as it was intended, thanks again for all your help and quick response...greatly appreciated.
 
does anyone knows how to get the server name that my script is running on?
 
couple ways to get server name are:

% uname -n

or

% hostname


-ram
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top