Operating System - HP-UX
1745811 Members
4033 Online
108722 Solutions
New Discussion юеВ

Re: Need help with ssh to multiple hosts with pipe

 
SOLVED
Go to solution
allanm77
Frequent Advisor

Need help with ssh to multiple hosts with pipe

Hi!

 

I have the following command which is NOT working, the way I want it to.

 

The intention is to ssh to a list of servers from an admin box and issue a command against a list of PID and find out the name of the PID.

 

for i in `cat /tmp/list`; do echo "-------------------$i----------------" ; ssh $i "lsof |grep db|grep EST|awk '{print $2}'|sort|uniq | while read i ;do ps -ef|grep $i;done " ; done 

 

 

Please help!

 

Thanks,

Allan.

13 REPLIES 13
Dennis Handly
Acclaimed Contributor

Re: Need help with ssh to multiple hosts with pipe

>which is NOT working, the way I want it to.

 

How is it failing?

for i in $(< /tmp/list); do

   echo "-------------------$i----------------"

   ssh $i "lsof | grep db | grep EST| awk '{print $2}' | sort | uniq | while read i ; do ps -ef | grep $i; done"

done

 

I can think of one thing.  The "grep $i" is expanded from the outer for loop variable.  Better to use different descriptive names and also quote the remote variable:  (And don't put it all on one  line.  :-)  Test it first though.

for node in $(< /tmp/list); do

   echo "-------------------$node----------------"

   ssh $node "lsof | grep db | grep EST | awk '{print $2}' | sort -u |

      while read file; do

         ps -ef | grep \$file

      done"

done

allanm77
Frequent Advisor

Re: Need help with ssh to multiple hosts with pipe

Thanks Dennis, but I am getting:-

Invalid null command.
while: Expression Syntax.
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
done: Command not found.
allanm77
Frequent Advisor

Re: Need help with ssh to multiple hosts with pipe

Is there a way to make it work using xargs instead of while ?
Dennis Handly
Acclaimed Contributor

Re: Need help with ssh to multiple hosts with pipe

>but I am getting: Invalid null command.  while: Expression Syntax.

 

Ah, one more "$" to quote:

awk '{print \$2}' | sort -u |

 

>Is there a way to make it work using xargs instead of while?

 

Why bother?  Use vector methods:

ssh $node "lsof | grep db | grep EST | awk '{print $2}' | sort -u > /var/tmp/fflist.\$\$;

               ps -ef | grep -f /var/tmp/fflist.\$\$; rm -f /var/tmp/fflist.\$\$"

allanm77
Frequent Advisor

Re: Need help with ssh to multiple hosts with pipe

Problem is that other machines have a different shell than what I am using on the admin box and its not accepting while loop.

The other boxes have t-csh.
allanm77
Frequent Advisor

Re: Need help with ssh to multiple hosts with pipe

tried giving ssh $i "bash|lsof ... " didnt work.
Dennis Handly
Acclaimed Contributor

Re: Need help with ssh to multiple hosts with pipe

>The other boxes have t-csh.

 

Any reason you are using a real shell and trying to talk to machines with scummy C shell?  ;-)


 >tried giving ssh $node "bash | lsof ... " didn't work.
 
You need either:
1) bash -c "lsof ... "  Which play havoc with your existing quotes and quoted "$".
2) echo "lsof ..." | bash
3) Or a here doc variant of 2).
allanm77
Frequent Advisor

Re: Need help with ssh to multiple hosts with pipe

Thanks for your continued support , i tried #2 and getting -

while: Expression Syntax.
bash: line 1: 1: command not found

also, #1 is also not working as expected.

Is there room for xargs?

Thanks,
Allan.
Dennis Handly
Acclaimed Contributor

Re: Need help with ssh to multiple hosts with pipe

>I tried #2 and getting - also, #1 is also not working as expected.

 

Unfortunately I no longer know exactly what you have, please provide examples.

 

>Is there room for xargs?

 

Have you tried my vector method with grep -f?  No loops there, just an extra temp file to clean up.