1833760 Members
2121 Online
110063 Solutions
New Discussion

Re: Running cmds via SSH

 
SOLVED
Go to solution
PatRoy
Regular Advisor

Running cmds via SSH

Hello.

I'm trying to issue the following cmd via SSH.

ssh -n hostname /usr/sbin/ioscan -funC disk | while read a b; do [[ $b = /dev/rdsk/* ]] && /usr/sbin/diskinfo $b; done | sed '/vendor/d'

This doesn't work because instead of launching the 'diskinfo' on the remote host, it's launched in the localshell, trying to diskinfo devices that might not exist on localhost!

The fix is to put the entire cmd between quotes like so:

ssh -n hostname '/usr/sbin/ioscan -funC disk | while read a b; do [[ $b = /dev/rdsk/* ]] && /usr/sbin/diskinfo $b; done | sed '/vendor/d''

HOWEVER, this is where I've got a problem. My sed part already uses single quotes, and I cannot escape them. If I remove the sed part, all works exactly has expected!

I'm trying to see how this can be accomplised *without* the need to write a script on the remote servers...

P.s.: don't question on why that cmd.. I know it's pretty useless. It's just an example of some things I'd like to do...

Regards,
Patrick


9 REPLIES 9
Patrick Wallek
Honored Contributor

Re: Running cmds via SSH

Patrick,

Use double quotes instead.

ssh -n hostname "/usr/sbin/ioscan -funC disk | while read a b; do [[ $b = /dev/rdsk/* ]] && /usr/sbin/diskinfo $b; done | sed '/vendor/d'"
PatRoy
Regular Advisor

Re: Running cmds via SSH

I already thought of that and no that doesn't work either:

sh: Syntax error at line 1 : `/dev/rdsk/*' is not expected.

is what I get in return
spex
Honored Contributor
Solution

Re: Running cmds via SSH

Hi,

In the example you gave, the sed command works without quotes:

ssh -n hostname '/usr/sbin/ioscan -funC disk | while read a b; do [[ $b = /dev/rdsk/* ]] && /usr/sbin/diskinfo $b; done | sed /vendor/d'

PCS
Steven E. Protter
Exalted Contributor

Re: Running cmds via SSH

Shalom Patrick,

Few things to look at.

Some systems don't provide environment properly, so make sure PATH and such is set.

Enclose commands in double quotes.

Example:

ssh root@hostname "ls -lrt *.iso"

For complex commands including say awk the \ for special characters will permit quotes within quotes.


SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
PatRoy
Regular Advisor

Re: Running cmds via SSH

You're right! it does work.

But now, what if I wanted to add an

awk '{print $2}'

?

I need to do a bunch a little scripts which collects various server information from approx. 17-18 servers.. (i.e BDFs, vgdisplay, etc.). That's why I need a bunch of theses...
PatRoy
Regular Advisor

Re: Running cmds via SSH

Steven E. Protter, no in cannot use '\' to escape my quotes. I've already tried that...

and has for having double quotes and even trying to escape my single quote won't do:

ssh -n host "/usr/sbin/ioscan -funC disk | while read a b; do [[ $b = /dev/rdsk/* ]] && /usr/sbin/diskinfo $b; done | awk \'{print $2}\'"

gives me:
sh: 2: Parameter not set
spex
Honored Contributor

Re: Running cmds via SSH

Patrick,

Use double-quotes, then single-quotes, and escape special characters, such as $.

Also, I believe the error you reported in your second posting was caused by not escaping (\) the LF between lines.

PCS
PatRoy
Regular Advisor

Re: Running cmds via SSH

spex, you've got it.

the following worked:

ssh -n host "/usr/sbin/ioscan -funC disk | while read a b; do [[ \$b = /dev/rdsk/* ]] && /usr/sbin/diskinfo \$b; done | awk '{print \$1}'"

and so does something like:

ssh -n host "/usr/sbin/ioscan -funC disk | while read a b; do [[ \$b = /dev/rdsk/* ]] && /usr/sbin/diskinfo \$b; done | sed '/vendor/d'"

so basically, when using double quotes, it's only $ I need to escape, and not the ' single quotes.

thanks again!

PatRoy
Regular Advisor

Re: Running cmds via SSH

as per last msg.