Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SSH as root but execute command as another user.

Status
Not open for further replies.

scfc1976

Programmer
May 19, 2009
12
GB
I am trying to automate a process of copying files from a CD over a network from a host to a remote terminal.

Due to the security setup we have I need to SSH as root.
However the commands to run a script need the environment of a different user in order to run correctly.

I have tried the following but this just wants to run the command locally before carrying out the SSH.

my code:-

ssh workstationA "su -c <./MY_SCRIPT> bob"

Any ideas?

Thanks scfc1976
 
Tried that but had error: unexpected end of file.

However tried a variation on it and got the result I was after.

ssh -n workstationA "su - bob; /path/to/script"

Thanks for the help.

scfc1976
 
Not entirely sure what your variation is, as it looks exactly the same as I typed.
However, glad it worked.
 
I probably to the syntax too literally.

However further study has found that bob didn't become the user to run the script and the ownership of the output belongs to root.

I may try to add a script on workstationA that will su - bob and then run the script from within that script. It probably isn't the most efficient way of doing this but I can't see any other way around it.

thanks again.
 
What about this ?
ssh -n workstationA "su - bob -c /path/to/script"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV.

I had tried that but was not getting bobs variables to run the commands.

Thanks anyway.
 
Try exporting the variables in the profile of bob.

Then PHV's suggestion should work.

Example;

$ cat .profile
THISVAR="I am bob"
export THISVAR

$ cat bobscript
#!/bin/ksh
echo "This is Bob"
echo $LOGNAME
echo $THISVAR

root> ssh -n workstationA "su - bob -c bobscript"
This is Bob
bob
I am bob

Hope this helps
 
I managed to find an old scripts hidden in our archive and found the following:-

su - bob -c "/path/to/script"

That has done the trick.

Thanks to both of you for your input.
 
The only difference that I can tell is the positioning of the quotation marks.

We both previously had placed the quotes prior to the su command.

eg ssh -n workstationA "su - bob -c /path/to/script"

The one I have now is:-

ssh -n workstationA su - bob -c "/path/to/script"

A subtle difference I know but did make a big difference.
Hope that clears the confusion up.
 
Further to my last post I have an updated version which works well and every time. The last one seemed tempremental. Anyway here goes:-

ssh -C workstationA 'su - bob -c "/path/to/script"'

Hope it helps someone else!!
 
Trojan.

It was considered, however during initial trials "bob's" variables were not passed to workstationA and the script wouldn't run. We kept getting an exit value of 1 : (syntax or usage error).

I should have mentioned that in my first post.

Thanks for the thought though.

scfc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top