Re: How to get ssh to work in a loop

From: Mike Cross (
Date: 12/01/03

Date: Mon, 1 Dec 2003 14:19:34 +0000 (UTC)

The for loop scrambles stdin, which upset, ssh/rsh, I get around ths by
using while loop:

while read line
rsh $line date
done < file

works OK


Peter Jones wrote:
> Dear group,
> I am having a problem getting ssh to work in a "for" loop. The script
> I am working on is run on a Solaris system and uses a host list to
> contact HP, AIX and Sun systems on the network to find out if any
> oracle filesystems are not largefile enabled. I have written the
> script for the sun systems which was quite easy as you only need to
> run one command and grep for largefiles. I am now trying to get the
> script to do the same for the HP systems which is proving more
> difficult.
> Here is the script for the HP systems:
> hp_systems() {
> for HOSTFILE in hp.hosts_temp
> do
> echo "\n** $HOSTFILE **"
> for HOST in `cat $BASEDIR/$HOSTFILE|awk '{print $1}'`
> do
> ping $HOST 1 > /dev/null
> if [ $? -ne 0 ]
> then
> echo "ERROR: $HOST not contactable."
> else
> echo $HOST > $LOGFILE
> ssh $HOST "cut -d ' ' -f2 /etc/mnttab | grep ora > /tmp/lrgfiles"
> for i in `ssh $HOST cat /tmp/lrgfiles`
> do
> echo $i >> $LOGFILE
> ssh $HOST fsadm -F vxfs $i | grep nolarge >> $LOGFILE
> done
> fi
> done
> done
> }
> In order to get the script to work for HP, I first have to get a list
> of mounted filesystems. I then have to run the fsadm command on each
> mounted filesystem by ssh to HP host and then creating a file called
> /tmp/lrgfiles. I then do a "for" loop and ssh to the host for each
> file in the /tmp/lrgfiles file. The outcome of this is that I end up
> doing loads of ssh to each host. There must be an easier way to do
> this - I'd like to have a for loop in which there is only one ssh to
> each host.
> Another problem I have is that each ssh connection attempt outputs an
> ssh banner to the screen - how do I stop ssh from outputting to the
> screen when run in a script?
> Many thanks
> Pete