Operating System - HP-UX
1753783 Members
6877 Online
108799 Solutions
New Discussion юеВ

ssh using su and a different shell

 
SOLVED
Go to solution
eric lipede_1
Regular Advisor

ssh using su and a different shell

Easy high points here...

Im attempting to ssh to another box (as root), su as another user and change shell to run a "for loop".
so...

it should look like :

ssh -t hpbox su - auser -c "sh for i in 1\;do echo \$i\;done"

Yes (in advance) I know that one method would be to add the shell to auser another would be to create a script and run that etc.
Im really interested in this method.

ssh -t hpbox su - auser -c 'ls' -> works
ssh -t hpbox su - auser -c 'sh ls' -> this logs me into the shell without running ls...

All help appreciated and rewarded - thks in advance. I have looked at the related threads but they dont address all 3 things - cheers
7 REPLIES 7
Todd D Kirkham
Visitor
Solution

Re: ssh using su and a different shell

Your missing another -c.

ssh -t hpbox su - auser -c 'sh -c ls'

should work. A shell other then sh might be different.

TwoProc
Honored Contributor

Re: ssh using su and a different shell

Yep, I ran what Todd suggests, and it works fine.
We are the people our parents warned us about --Jimmy Buffett
Matti_Kurkela
Honored Contributor

Re: ssh using su and a different shell

Please try:

ssh -t hpbox su - auser -c 'sh -c ls'

Maintaining proper quoting through all the different command line parsing layers will be a bit of a challenge:

- local shell parses the command line once (removing the outermost set of quotes, and un-escaping backslash-escaped characters) before passing it to the ssh command

- root@hpbox's shell parses the su command line again (removing another set of quotes and possibly acting on special characters un-escaped in the previous step) before passing it to the su command

- auser@hpbox's "sh -c" should receive the payload part of the command line as one argument

MK
MK
Doug O'Leary
Honored Contributor

Re: ssh using su and a different shell

Hey;

There's a definite limit to the amount of complexity you can have on a ssh remote command. Heck, even awk or perl one-liners can get a bit entertaining with quoting, escaping $ signs etc.

Any type of looping, in my opinion, definitely falls into the 'easier to do other ways' category.

If this is for an operational requirement, I'd look into those, if I were you. (even generating the script file on the fly and scp'ing it over...).

If this is just a mental exercise: good luck!

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Viktor Balogh
Honored Contributor

Re: ssh using su and a different shell

Hi Eric,

Maybe the following is also an option for you, by running the for loop on the local server the escaping would be much less complicated:

for i in 1;
do
ssh -t hpbox su - auser -c 'sh -c echo '$i;
done

Regards,
Viktor
****
Unix operates with beer.
eric lipede_1
Regular Advisor

Re: ssh using su and a different shell

Viktor - thks but like i said the solution must be run from another node. O fcourse running it on the remote node would be great and we wouldnt be here otherwise ! -thks and points-ish assigned for your effort.
eric lipede_1
Regular Advisor

Re: ssh using su and a different shell

thks