Need Shell Scripting Guru Help!!!

RyDogg1

Senior member
Jun 11, 2001
297
0
71
Hey all,

I'm writing a script to copy a file accross multiple servers in our trusted environment. I've got the single file to a single server part working great, but what I need is some idea as to how to copy the file to all the servers.

I can read from the .rhosts file, is there a way to take in a line at a time this file input it to an array and then rcp each element of the array?

Help please!

thanks,

Ryan
 

RyDogg1

Senior member
Jun 11, 2001
297
0
71
I'm sure these would help if I were using a MS product! :)

Thanks anyway, but I'm looking for UNIX shell programming.

Ryan
 

bdclary

Junior Member
Feb 20, 2002
7
0
0
How about perl?

#!/usr/local/bin/perl (or wherever perl is)

my $file = "rhosts"; # include the full path to rhosts

open(RHOSTS, "$file") || die("Can't open $file"); #opens rhosts file
while (<> ) {
chomp($_); #remove newline character at end of line
# do rcp stuff here
}
close(RHOSTS);

Hope this helps,
Brian
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Or Python

#/usr/bin/python
#a script to rcp a file (argument 1) to a path (argument 2) on every system named in .rhosts
import string

if len(sys.argv) != 3:
[tab]print "USAGE: pyrcp file target_path"
[tab]sys.exit()

file = open("~/.rhosts") #open the .rhosts file
lines = file.readlines() #read the whole file into a list (one line per element)
for line in lines:
[tab]if string.lstrip(line)[0] == "#" or len(line) <= 1: #if the line is blank or a comment, skip it
[tab][tab]continue
[tab]cmd = "rcp " + sys.argv[1] + " " + string.split(line)[0] + ":" + sys.argv[2] #form the rcp command for this system
[tab]err = system(cmd) #execute the rcp command
[tab]if err:
[tab][tab]print "ERROR: Failed copying file to ", string.split(line)[0], ":", sys.argv[2]


Ok, I've forgotten the trick for preserving whitespace in the forums. Something to do with , but I can't get it to work :disgust:
 

CSoup

Senior member
Jan 9, 2002
565
0
0
Which shell are you in? Why don't you try a for loop. Depending on the shell, there should be a way to take the .rhosts file as the input for the for loop. You can probably always cat the .rhosts file as the input. Not in front of a shell right now, so can't test it, but something similar to FOR i in ('cat .rhosts') do blah blah. Your shell might use FOREACH or some other for loop construct.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
what shell are you using? sh? ksh? csh?
i don't have unix account right now, but try this one out ...

#!/bin/sh
inputfile=/your/file/name
for serverlist in `cat $inputfile`
do
...rcp filename_to_copy $serverlist # sorry but i forgot how rcp works
...# whatever else you want to do
end

i think that should work... if it doesn't, let me know and i'll figure out how to do it in csh
but i like the above code (perl/phyton) better, although that means you'll have to make sure you have perl interpreter installed, etc....

-960-
 

RyDogg1

Senior member
Jun 11, 2001
297
0
71
Thanks for the help guys, you've given me some definite ideas to work with. I want this to be strictly a shell (bourne) so it is easily moved between Solaris 2.6, 7 and 8, so no PERL cause all the server don't have an interpreter.

I've never heard of Python, so that's interesting. More info on that?

Thanks again!

Ryan
 

Armitage

Banned
Feb 23, 2001
8,086
0
0


<< Thanks for the help guys, you've given me some definite ideas to work with. I want this to be strictly a shell (bourne) so it is easily moved between Solaris 2.6, 7 and 8, so no PERL cause all the server don't have an interpreter.

I've never heard of Python, so that's interesting. More info on that?

Thanks again!

Ryan
>>



Python is another scripting language, just like perl is.
That means that the python interpretor must be installed on whatever machine you intend to run on.

The good news is that you will likely find perl almost anywhere. Python is not quite as common, but its getting there. And if they aren't there, they are available for almost any common platform/OS (again, perl more then python probably).

I'd make a small wager that you may be more likely to find compatible versions of perl or python on your platforms then compatible versions of csh or bash. I just came across a csh script the other day that worked on fine on SGI, but not on Linux.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
the script i have up there is in bourne shell.... does it work?
(replace the three dots with tabs ... i just put them there for easy reading)
and btw, are all your servers running the same OS? what unix platform do you use?

bourne shell pretty much works the same way across different types of *nix
that's why it's one of the preferred "basic" shell scripts
like ergeorge mentioned above, csh and/or bash can differ from one *nix to another, because they came in later to add more functionalities to the basic bourne shell (sh) and korn shell (ksh)

<< I've never heard of Python, so that's interesting. More info on that? >>
what ergeorge said: phyton, like perl, is a scripting language. you'll need to install the interpreter on the machines for it to be able to run phyton.... heard that it's comparable to perl (in terms of speed), and it's sometimes used by qa people to run stress tests to debug their programs (at least in my prev company)

the good news is if you are using those scripts for basic operations, you can pretty much download and install them on the machines. however, if you want to use them for complicated things, you may want to rebuild your own interpreter on your machines ... but then again, that's a different story altogether

btw, care to give us the basics on how rcp works? :) (i only know cp and scp)

-965-
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
rcp = remote cp
It lets you copy files directly between machines without having to go through nfs, ftp, etc.

rcp is an 'r' service, like rsh, rlogin, etc., so it is insecure. You don't want the 'r' services turned on if you can't trust/control your network environment.
 

RyDogg1

Senior member
Jun 11, 2001
297
0
71


<< the script i have up there is in bourne shell.... does it work?
(replace the three dots with tabs ... i just put them there for easy reading)
and btw, are all your servers running the same OS? what unix platform do you use?
>>



I haven't tried it yet, but I'm working on the script today. I did a man lookup on FOREACH, FOR and that might be the way to go. All the servers in the building are SUN enterprise boxes all running different flavors of Solaris 2.6, 7 and 8.

This is a trusted environment so I'm able to use r commands without any problems.

rcp can be done one or two different ways. The best is to have a trusted environment so you won't get an access denied when trying to copy a file. the command is like this

rcp <<source_file>> <<Hostname>>:<<destination_file>>

or

rcp <<hostname>>:<<source_file>> <<destination_file>>

What I want to accomplish I guess is like this

FOREACH <<hostname>> do
rcp hostname:source_file destination_file
done

Not sure how else to do this?

Ryan
 

RyDogg1

Senior member
Jun 11, 2001
297
0
71
ALRIGHT!!! Got it working!! Thanks for everyone's help, here's how it works:

echo "
Copy File to:

[O]ne Server
[A]ll Servers
E[x]it
"
echo "Enter Your Choice: "
read choice
case $choice in
o|O) echo "Which server? "
cat /.rhosts
echo "Please type in the name of the server: "
read server_name
echo "Please type in the name of the file to copy: "
read file_name
echo "Please type in the location where the file is going: "
read location
echo "Copying..."
rcp $file_name $server_name:$location
echo "Copied!"
;;

a|A) echo "Please type in the name of the file to copy: "
read file_name
echo "Please type in the location where the file is going: "
read location
cat servers.txt | while read serverlist; do
rcp $serverlist:$file_name $location;
done
;;
x|X) echo "Goodbye!!!
";;

Works great as long as the location that the file is going to is there, but if it's not, it doesn't create the dir, just creates a file called scripts. If anyone knows how the r command for making a dir, please post here!

Ryan
 

Armitage

Banned
Feb 23, 2001
8,086
0
0


<< If anyone knows how the r command for making a dir, please post here!

Ryan
>>



Just use rsh

rsh hostname mkdir dirname

 

RyDogg1

Senior member
Jun 11, 2001
297
0
71
Ah, open up a shell session and just run it. Hmmm...it boggles me how I even remember to breath.

Thanks,

Ryan
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
It's not really a full shell session. It just executes the specified command on the remote host, and returns the output.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
btw, just to make the script a little shorter ...
i noticed that you asked for the filename and destination to put the file to on each different case (one or all server). you could have put this outside the case statement (eg: before you begin the case block), so to save some stuffs... but of course, it's not much of difference in this case

btw, you may also want to add a check to see if the file on your source directory exist
if [ ! -f $filename ]; then
...echo file does not exist
...exit 1
fi


also, if you want to create directory on a remote location, you may want to consider using the -p option with mkdir, so it will create the parent directory as well if it does not exist
if [ ! -d $destination_directory ]; then
...mkdir -p $destination_directory
fi


-967-