1829055 Members
3138 Online
109986 Solutions
New Discussion

remsh in a loop

 
SOLVED
Go to solution
Sundar_7
Honored Contributor

remsh in a loop

Folks,

Long time :-). I would like to think myself as a reasonably skilled scripter but this one baffled me. I just could not figure out what am I missing here.

I have this file serverlist that lists all the servers with some comments in between

# grep -v "^#" serverlist | wc -l
109
#

All I am trying to do is to execute a simple command via remsh to all of the machines from a node that has .rhosts entries

# grep -v "^#" serverlist | awk '{print $1}' | xargs -n1 | while read SERVERNAME
> do
> remsh $SERVERNAME "echo Hey"
> done
Hey
Hey
#

Why on the god's green earth the loop will exit just after two iteration? Even if there is something wrong, I should get an error message from remsh

# grep -v "^#" serverlist | awk '{print $1}' | xargs -n1 | while read SERVERNAME
> do
> echo $SERVERNAME
> done | wc -l
109
#

Am I missing something very obvious?

- Sundar.
Learn What to do ,How to do and more importantly When to do ?
15 REPLIES 15
harry d brown jr
Honored Contributor

Re: remsh in a loop

can you try:

xargs -i echo remsh {} "some command"

live free or die
harry d brown jr
Live Free or Die
Sundar_7
Honored Contributor

Re: remsh in a loop

Yes, Harry - that seem to work. But what is wrong with my "for" loop ? - I have successfully executed the similar "for" loops in the past.
Learn What to do ,How to do and more importantly When to do ?
harry d brown jr
Honored Contributor

Re: remsh in a loop

Check your "serverlist" file for funky characters.

check "which grep" you are using.
check "which awk" you are using.
check "which xargs" you are using.
check "chich remsh" you are using.

Also, try replacing the remsh $SERVERNAME line with this:

host ${SERVERNAME}

just to make sure you have "good" server names.

live free or die
harry d brown jr
Live Free or Die
Geoff Wild
Honored Contributor

Re: remsh in a loop

What if you try a for instead of while?

for SERVER in `grep -v "^#" serverlist | awk '{print $1}' | xargs -n1 `
do
echo "$SERVER"
remsh $SERVER "echo hey"
done

Can you post your serverlist file?

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Sundar_7
Honored Contributor

Re: remsh in a loop

for loop works just fine. I know how to work around. So that is not what I am looking for.

Harry - if there are funky names, how do you think xargs -i remsh {} is working ?

grep -v "^#" serverlist | awk '{print $1}' | xargs -i remsh {} date

this work. Doesnt it confirm the grep, awk ,xargs and the serverlist are just fine ?
Learn What to do ,How to do and more importantly When to do ?
harry d brown jr
Honored Contributor

Re: remsh in a loop

how about changing
remsh $SERVERNAME "echo Hey"
to
remsh $SERVERNAME /usr/bin/echo Hey

live free or die
harry d brown jr
Live Free or Die
Sundar_7
Honored Contributor

Re: remsh in a loop

Again, if /usr/bin/echo must be used instead of echo, how does the remshell with in the "for" loop works with just echo ?
Learn What to do ,How to do and more importantly When to do ?
Biswajit Tripathy
Honored Contributor

Re: remsh in a loop

Sunder,

Just a wild guess :-)

Does your serverlist file have a comment which
starts in the middle of a line? In other words, do
you have a '#' char at some place other than
first column?

- Biswajit
:-)
harry d brown jr
Honored Contributor

Re: remsh in a loop

because echo locally is one thing. Specifying the absolute path on the remote host is another.

Can you post your "input" file "serverlist" ??

thanks,


live free or die
harry d brown jr
Live Free or Die
Bill Hassell
Honored Contributor

Re: remsh in a loop

To amplify Harry's response a bit, which (and whereis) are not the tools to identify what your script is doing. When you run a script or issue a shell command, the text on the command line is subject to filename expansion, shell builins as well as aliases, none of which the which or whereis command will locate. To discover exactly what will be run, use the type command as in:

type grep awk xargs remsh

(type is an automatic alias wo whence -v)
AS far as the 3rd item failure, echo the value of $SERVERNAME just before issuing the remsh command and also check the validity of the server name with getip:

grep -v "^#" serverlist | awk '{print $1}' | xargs -n1 | while read SERVERNAME
do
MYIP=$(getip $SERVERNAME)
echo "$SERVERNAME: IP=$MYIP"
remsh $SERVERNAME "echo Hey"
done


Bill Hassell, sysadmin
Sundar_7
Honored Contributor

Re: remsh in a loop

hmm what am I missing ?

IF

for SERVER in $(grep -v "^#" serverlist | awk '{print $1}')
do
remsh $SERVER echo Hello
done

AND

grep -v "^#" serverlist | awk '{print $1}' | xargs -i remsh {} echo Hello

WORKS, WHY

grep -v "^#" serverlist | awk '{print $1}' | xargs -n1 | while read SERVER
do
remsh $SERVER echo Hello
done

IS NOT WORKING ?

I am using the same

1) grep
2) awk
3) xargs
4) serverlist file
5) same remsh command


Learn What to do ,How to do and more importantly When to do ?
Ermin Borovac
Honored Contributor
Solution

Re: remsh in a loop

Try running remsh with -n option. It should work with all loops.

From remsh(1)

If a command and the option -n are specified, then standard input is redirected to remsh by /dev/null. If -n is not specified (the default case), remsh reads its standard input and sends the input to the remote command. This is because remsh has no way to determine whether the remote command requires input. This option is useful when running a shell script containing a remsh command, since otherwise remsh may use input not intended for it.
Geoff Wild
Honored Contributor

Re: remsh in a loop

Just in case there isn't something strange in the serverlist file, can you create a simple newserverlist file that ONLY contains a server name - one line at a time.

Put in say 10 servers - then test your script...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
RAC_1
Honored Contributor

Re: remsh in a loop

How does this output look like??

grep -v "^#" serverlist | awk '{print $1}' | vis

And
grep -v "^#" serverlist | awk '{print $1}'|vis|xargs -n1

Anil
There is no substitute to HARDWORK
Bob Smith_23
Advisor

Re: remsh in a loop

Could it be the shell you are using?

ksh, csh, sh variances ?