1748039 Members
5175 Online
108757 Solutions
New Discussion юеВ

Re: script help?

 
SOLVED
Go to solution
Rick Garland
Honored Contributor

script help?

Drawing a blank on this.

An enduser is going to run a script. This script will ask questions and store the results. One of the questions is "What hosts are to have a copy of this log file?" Based on the answer, have this log file remote copied (using scp, of course!). The answer to the hosts question could be any number of hostnames from 1 to 40.

So my question is, in the script how would I account for a read that will have a varied number of responses?

Once I have these responses I need to loop through and scp the log file.


Thanks
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: script help?

Hi Rick:

You can always use a temporary file to collect the n-read results. Then, re-read the temporary file to process:

#!/usr/bin/sh
typeset NAMES=/tmp/mynames

while read LINE
do
echo ${LINE} >> ${NAMES}
done

while read LINE
do
echo "processing ${LINE}..."
done < ${NAMES}
exit

Regards!

...JRF...
Rick Garland
Honored Contributor

Re: script help?

I'm missing something.

What I am doing is making a useradd script for a shop of some 40 servers. Unfortunately NIS or LDAP is not is use and will not be (otherwise this would be easy)

On a server, the helpdesk will input the requisite values for the useradd. These values are stored in a log file.

Part of the script is to ask the helpdesk user what additional systems this user to be added to. Here the helpdesk user will type in the individual hostnames, each hostname followed by "ENTER", until the "X|x" character is pressed. At this time a log file of the entered hosts exists and is scp to the hosts that were entered in. Note, the number of servers will vary - could be 1 server or could be all 40 servers or could be any number of servers from 1-40.


Bill Hassell
Honored Contributor

Re: script help?

Well, you can make the script even more intelligent and just collect hostnames until nothing is entered (just a return). This is an ideal use for an array to store the names, something like this:


#!/usr/bin/sh
export PATH=/usr/bin
set -A HOSTNAMES

echo "Enter hostnames, one per line and nothing for the last name:"

COUNT=0
REPLY=start

while [ "$REPLY" != "" ]
do
read REPLY
if [ "$REPLY" != "" ]
then
let COUNT=COUNT+1
HOSTNAMES[$COUNT]="$REPLY"
fi
done

if [ $COUNT -eq 0 ]
then
echo "No hostnames entered - exiting"
fi

echo "There are $COUNT hosts"
for HOST in ${HOSTNAMES[@]}
do
echo scp to host: $HOST
done


The scp line can be changed to the actual scp command. For truly robust scripting for a helpdesk, I would look for duplicates in the list and also validate that each host is valid (like a nsquery or nslookup to find the host). And while you're at it, add a check for successful scp completion.


Bill Hassell, sysadmin
Patrick Wallek
Honored Contributor

Re: script help?

I wrote a script recently that involves handling a varied number of hostnames. It's function is to copy files to other hosts.

Here is how I handled it:

echo "Enter the system destination separated by spaces"
echo "(hqubl01, hquwh01, hquwh05, hquwh50, hquwh55, or all): "
read S36DEST

for SYS in ${S36DEST}
do
scp afile ${SYS}:adir
done
A. Clay Stephenson
Acclaimed Contributor

Re: script help?

If this were me, I would add an optional -f filename argument (using getopts, of course) to optionally read the hostnames from a file because the 2nd time I had to type in 40 hostnames would be one time too many --- but I'm lazy (or efficient, you pick).
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: script help?

If this script is for interactive usage as suggested, then please a some node-name validity testing, as each node it typed:
1) and array of known nodes in the script.
or
2) a quick lookup in a central file (/etc/hosts)
or
3) a ping

hth,
Hein.
Rick Garland
Honored Contributor

Re: script help?

Hey folks - thanks!