Re: Need help with ssh to multiple hosts with pipe (804 Views)
Reply
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 1 of 14 (814 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.

Acclaimed Contributor
Dennis Handly
Posts: 25,303
Registered: ‎03-06-2006
Message 2 of 14 (809 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

Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 3 of 14 (805 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.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 4 of 14 (804 Views)

Re: Need help with ssh to multiple hosts with pipe

Is there a way to make it work using xargs instead of while ?
Acclaimed Contributor
Dennis Handly
Posts: 25,303
Registered: ‎03-06-2006
Message 5 of 14 (794 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.\$\$"

Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 6 of 14 (785 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.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 7 of 14 (784 Views)

Re: Need help with ssh to multiple hosts with pipe

tried giving ssh $i "bash|lsof ... " didnt work.
Acclaimed Contributor
Dennis Handly
Posts: 25,303
Registered: ‎03-06-2006
Message 8 of 14 (770 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).
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 9 of 14 (759 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.
Acclaimed Contributor
Dennis Handly
Posts: 25,303
Registered: ‎03-06-2006
Message 10 of 14 (755 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.

 

Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 11 of 14 (753 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?
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 12 of 14 (746 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.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 13 of 14 (744 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!
Acclaimed Contributor
Dennis Handly
Posts: 25,303
Registered: ‎03-06-2006
Message 14 of 14 (729 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.

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.