Operating System - HP-UX
1830932 Members
2280 Online
110017 Solutions
New Discussion

SSH script using a for loop

 
SOLVED
Go to solution
Adam Noble
Super Advisor

SSH script using a for loop

All,

Hi I'm generating a script to collect audit information from a list of servers. I want it to run from a central server and it will use ssh to communicate.

I hope this is a simple question...maybe not. I am trying to get the firmware revision of my root disks. I can do a vgdisplay to get the disks and then I want to do a diskinfo -v for each disk and grep out the firmware. I am unsure how to script it. Is it possible to do a for loop within an ssh command.

Adam
7 REPLIES 7
Dennis Handly
Acclaimed Contributor
Solution

Re: SSH script using a for loop

Would it be simpler to just copy a script to each machine and execute it?
Otherwise:
$ ssh machine "for i in 1 2; do echo \$i; done"
Dennis Handly
Acclaimed Contributor

Re: SSH script using a for loop

If you use "$" a lot you may not want to quote them with "\". I should have put the whole thing in single quotes. But then you wouldn't have been able to substitute your variables into the command.
$ ssh machine 'for i in 1 2; do echo $i; done'
Peter Godron
Honored Contributor

Re: SSH script using a for loop

Adam,
the way you describe it, you will have at least two options, either run a script/set of scripts on your clients (Dennis's solution)
OR
run your script on your server, but with multiple ssh connections being made.

So you would first send your vgdisplay -v, capturing the output on the server. A bit more server processing and you can then build your diskinfo script file on the server to run over ssh.

Either solution should work. You would have to evaluate multi-client maintenance versus multiple sessions.
Adam Noble
Super Advisor

Re: SSH script using a for loop

OK thanks another little problem I'm having. I am using functions and the 1st one is simply a ping test which determines whether I should even carry on with the additional steps of copying the script to the server and running it. I therefore do a ping test within the function and if it fails I want to break out of the main for loop. I am aware if this was a standard for loop without the functions I could use continue but this is not working within the function. Is it possible?
James R. Ferguson
Acclaimed Contributor

Re: SSH script using a for loop

Hi Adam:

> I am aware if this was a standard for loop without the functions I could use continue but this is not working within the function. Is it possible?

No. One solution, however, is from within your 'for' loop, call your function (subroutine). The function should return zero (0) for success or one (1) for failure. In you 'for' loop test the return and 'continue' or not:

function me
{
...
return 0 # ...or 1 if failure
}

for ...
me || continue
...

Thus, if 'me' fails, the 'continue' is exercised otherwise the remainder of the 'for' block executes.

Regards!

...JRF...
Ralph Grothe
Honored Contributor

Re: SSH script using a for loop

There are several possible drawbacks caused by insufficient read permissions on device and config files.
So only root can usually execute the diskinfo command because the disks' char devs cannot be read by others.
The same is the case for files like /etc/lvmtab .
If you don't want to relinquish permissions on these files your script must be run as root.
On the other hand permitting root logins (even via SSH) isn't a good habit for several reasons.
Usually one would login under a low priviledged maintenance account and then su to root.

Anyway, you could stuff something like this (of course better formatted) in a script on the clients, and make it root owned with setting the suid bit (another of the big Don'ts).

/usr/sbin/vgdisplay -v vg00|awk '/PV Name/{print$NF}'|sed s/dsk/rdsk/|xargs -n1 diskinfo|awk '/desc|rev/{print$NF}'

(n.b. at least one sed and awk call can be avoided by an appropiate awk block if you use awk functions like sub(), match() etc.)

E.g. executed on one of our boxes produces this:

/dev/rdsk/c2t2d0:
HPC2
/dev/rdsk/c1t2d0:
HPC2


As for the pinging, that doesn't necessarily proof if the to be contacted remote host is up or down.
We for instance, have most of our servers behind firewalls that only allow SSH connections (from our central managing host where one would run the SSH for loop) but silently drop all ICMP echo requests.
If this is not an issue for you,
then you could do as already shown by the others with some construct like

for $h in hostA hostB hostC; do
ping $h -n 1 -m 5 >/dev/null 2>&1 || continue
# do ssh stuff here
done


Btw, I cannot beleive that the FW revision of your Root VG disks change that often to not have this information stored in some more tangible table or file.
Madness, thy name is system administration
Adam Noble
Super Advisor

Re: SSH script using a for loop

thanks all. Ralph they change very little however we have no record of what they currently are so I'm trying to collate the info.

Anyway thanks all for the help