topic Re: Need help with ssh to multiple hosts with pipe in Languages and Scriptinghttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884421#M45234
The existing issue is with awk '{print $2}' not working with the remote ssh.Thu, 29 Nov 2012 20:19:30 GMTallanm772012-11-29T20:19:30ZNeed help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883211#M45220
<P>Hi!</P><P>&nbsp;</P><P>I have the following command which is NOT working, the way I want it to.</P><P>&nbsp;</P><P>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.</P><P>&nbsp;</P><PRE>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 </PRE><P>&nbsp;</P><P>&nbsp;</P><P>Please help!</P><P>&nbsp;</P><P>Thanks,</P><P>Allan.</P>Thu, 29 Nov 2012 06:44:13 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883211#M45220allanm772012-11-29T06:44:13ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883219#M45221
<P>&gt;which is NOT working, the way I want it to.</P><P>&nbsp;</P><P>How is it failing?</P><P>for i in $(&lt; /tmp/list); do</P><P>&nbsp;&nbsp; echo "-------------------$i----------------"</P><P>&nbsp;&nbsp; ssh $i "lsof | grep db | grep EST| awk '{print $2}' | sort | uniq | while read i ; do ps -ef | grep $i; done"</P><P>done</P><P>&nbsp;</P><P>I can think of one thing.&nbsp; The "grep $i" is expanded from the outer for loop variable.&nbsp; Better to use different descriptive names and also quote the remote variable:&nbsp; (And don't put it all on one&nbsp; line.&nbsp; :-)&nbsp; Test it first though.</P><P>for node in $(&lt; /tmp/list); do</P><P>&nbsp;&nbsp; echo "-------------------$node----------------"</P><P>&nbsp;&nbsp; ssh $node "lsof | grep db | grep EST | awk '{print $2}' | sort -u |</P><P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while read file; do</P><P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ps -ef | grep \$file</P><P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; done"</P><P>done</P>Thu, 29 Nov 2012 06:59:04 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883219#M45221Dennis Handly2012-11-29T06:59:04ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883255#M45223
Thanks Dennis, but I am getting:-<BR /><BR />Invalid null command.<BR />while: Expression Syntax.<BR />Usage: grep [OPTION]... PATTERN [FILE]...<BR />Try `grep --help' for more information.<BR />done: Command not found.Thu, 29 Nov 2012 07:44:46 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883255#M45223allanm772012-11-29T07:44:46ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883257#M45224
Is there a way to make it work using xargs instead of while ?Thu, 29 Nov 2012 07:45:45 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883257#M45224allanm772012-11-29T07:45:45ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883291#M45225
<P>&gt;but I am getting: Invalid null command.&nbsp; while: Expression Syntax.</P><P>&nbsp;</P><P>Ah, one more "$" to quote:</P><P>awk '{print \$2}' | sort -u |</P><P>&nbsp;</P><P>&gt;Is there a way to make it work using xargs instead of while?</P><P>&nbsp;</P><P>Why bother?&nbsp; Use vector methods:</P><P>ssh $node "lsof | grep db | grep EST | awk '{print $2}' | sort -u &gt; /var/tmp/fflist.\$\$;</P><P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ps -ef | grep -f /var/tmp/fflist.\$\$; rm -f /var/tmp/fflist.\$\$"</P>Thu, 29 Nov 2012 08:37:49 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883291#M45225Dennis Handly2012-11-29T08:37:49ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883329#M45226
Problem is that other machines have a different shell than what I am using on the admin box and its not accepting while loop.<BR /><BR />The other boxes have t-csh.Thu, 29 Nov 2012 08:39:54 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883329#M45226allanm772012-11-29T08:39:54ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883331#M45227
tried giving ssh $i "bash|lsof ... " didnt work.Thu, 29 Nov 2012 08:41:49 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883331#M45227allanm772012-11-29T08:41:49ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883591#M45228
<P>&gt;The other boxes have t-csh.</P><P>&nbsp;</P><P>Any reason you are using a real shell and trying to talk to machines with scummy C shell?&nbsp; ;-)</P><DIV><BR /><DIV><DIV><DIV>&nbsp;&gt;tried giving ssh $node "bash | lsof ... " didn't work.</DIV><DIV>&nbsp;</DIV><DIV>You need either:</DIV><DIV>1) bash -c "lsof ... "&nbsp; Which play havoc with your existing quotes and quoted "$".</DIV><DIV>2) echo "lsof ..." | bash</DIV><DIV>3) Or a here doc variant of 2).</DIV></DIV></DIV></DIV>Thu, 29 Nov 2012 11:52:52 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5883591#M45228Dennis Handly2012-11-29T11:52:52ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884297#M45230
Thanks for your continued support , i tried #2 and getting -<BR /><BR />while: Expression Syntax.<BR />bash: line 1: 1: command not found<BR /><BR />also, #1 is also not working as expected.<BR /><BR />Is there room for xargs?<BR /><BR />Thanks,<BR />Allan.<BR />Thu, 29 Nov 2012 18:22:11 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884297#M45230allanm772012-11-29T18:22:11ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884309#M45232
<P>&gt;I tried #2 and getting - also, #1 is also not working as expected.</P><P>&nbsp;</P><P>Unfortunately I no longer know exactly what you have, please provide examples.</P><P>&nbsp;</P><P>&gt;Is there room for xargs?</P><P>&nbsp;</P><P>Have you tried my vector method with grep -f?&nbsp; No loops there, just an extra temp file to clean up.</P><P>&nbsp;</P>Thu, 29 Nov 2012 18:33:37 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884309#M45232Dennis Handly2012-11-29T18:33:37ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884365#M45233
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.<BR /><BR />Thought?Thu, 29 Nov 2012 19:12:43 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884365#M45233allanm772012-11-29T19:12:43ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884421#M45234
The existing issue is with awk '{print $2}' not working with the remote ssh.Thu, 29 Nov 2012 20:19:30 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884421#M45234allanm772012-11-29T20:19:30ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884429#M45235
Its working now after escaping the \$2.<BR /><BR />Thanks so much for the vector method!Thu, 29 Nov 2012 20:24:56 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884429#M45235allanm772012-11-29T20:24:56ZRe: Need help with ssh to multiple hosts with pipehttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884983#M45236
<P>&gt;Thanks so much for the vector method!</P><P>&nbsp;</P><P>It would be nice if you would assign some kudos.</P>Fri, 30 Nov 2012 04:02:59 GMThttp://h30499.www3.hp.com/t5/Languages-and-Scripting/Need-help-with-ssh-to-multiple-hosts-with-pipe/m-p/5884983#M45236Dennis Handly2012-11-30T04:02:59Z