Re: Need help with ssh to multiple hosts with pipe (366 Views)
Reply
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 1 of 14 (375 Views)
Accepted Solution

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.

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,769
Registered: ‎03-06-2006
Message 2 of 14 (370 Views)

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

Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 3 of 14 (366 Views)

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.
Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 4 of 14 (365 Views)

Re: Need help with ssh to multiple hosts with pipe

Is there a way to make it work using xargs instead of while ?
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,769
Registered: ‎03-06-2006
Message 5 of 14 (355 Views)

Re: Need help with ssh to multiple hosts with pipe

[ Edited ]

>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.\$\$"

Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 6 of 14 (346 Views)

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.
Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 7 of 14 (345 Views)

Re: Need help with ssh to multiple hosts with pipe

tried giving ssh $i "bash|lsof ... " didnt work.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,769
Registered: ‎03-06-2006
Message 8 of 14 (331 Views)

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).
Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 9 of 14 (320 Views)

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.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,769
Registered: ‎03-06-2006
Message 10 of 14 (316 Views)

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.

 

Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 11 of 14 (314 Views)

Re: Need help with ssh to multiple hosts with pipe

The vector method seems to be working when I actually run it by sshing into the box but remote from admin box it return no output.

Thought?
Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 12 of 14 (307 Views)

Re: Need help with ssh to multiple hosts with pipe

The existing issue is with awk '{print $2}' not working with the remote ssh.
Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 13 of 14 (305 Views)

Re: Need help with ssh to multiple hosts with pipe

Its working now after escaping the \$2.

Thanks so much for the vector method!
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,769
Registered: ‎03-06-2006
Message 14 of 14 (290 Views)

Re: Need help with ssh to multiple hosts with pipe

>Thanks so much for the vector method!

 

It would be nice if you would assign some kudos.

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation