SSH and SCP HELP

Hi all
Iam using ssh and trying to change to a different user but im not able to do it.
--------
Script.sh
ssh user@hostname
sudo rootsh
whoami
--------

--------
Script2.sh
ssh user@hostname
su - username password
whoami
---------

When i run the Script.sh it is connecting to hostname however it is not showing username as "root", instead it is showing as "user".And also I have sudo access on hostname meaning if i login to that hostname and type "sudo rootsh" i can get into root fine.I tried to "su" (as in script2.sh) maybe im missing/dontknow how to enter the password.Let me know guys what im missing her.Appreciate your help.

Unless something has changed that I don't know about, you cannot su to another user when executing a command via ssh. Something about the su command needing you to be assigned a tty.

What you CAN do is sudo commands. So if you need to run something as root and you have sudo rights your script can do this:

ssh me@host "sudo somecommand"

That will ssh to "host" and execute "somecommand" as root.

Also, your script examples are a little flawed. You have ssh on it's own line and then some commands after that. That's now how that works. In your examples, you would ssh to the box in question and once you exited that box the remainder of the commands would be run. You can put your commands into a script on the remote server and then execute that script:

ssh me@host "/path/to/script"

NOTE: I keep putting the command in quotes but that's not required unless it contains spaces.

Even if you do this you're still going to have issues with switching users (I believe. you'll have to try it). If you need all the commands in the remote switch to run as root you can just "sudo /path/to/script".

Thanks for the response..As per your suggestion I tried like this

ssh user@hostname sudo rootsh---didnt work
or
ssh user@hostname "sudo rootsh"---didnt work either

and

ssh user@hostname "sudo /file/path"--asking password (for root)

do you think my SA blocked it or am i doing something else.Any suggestions??

First off, I don't know what "rootsh" is. Is that an app or a script you are trying to run? If so it would need to be in the path of the ssh user or in that users home directory. Otherwise you'll need to use the /full/path/to/rootsh.

Secondly, about the password, that's a configuration issue you'll need to bring up with the sys admin who controls that server (assuming it's not you). They would have to allow you to sudo without requiring a password to do it.

MG

Yea you are right..Spoke with my SA and he did his magic (in sudoers file) and it started working....

ssh user@hostname sudo /file/path--requires some tweaking frm SA before you run it...

Appreciate your help guys...