views:

27244

answers:

9

Hi,

could you please suggest me how to run a shell script on remote machine? I have ssh configured on both machine A and B. My script is on machine A which will perform a task on machine B.

Awaiting response! Will be great help for me.

Regards, Arun

+10  A: 
<hostA_shell_prompt>$ ssh user@hostB "ls -la"

That will prompt you for password, unless you have copied your hostA user's public key to the authorized_keys file on the home of user .ssh's directory. That will allow for passwordless authentication (if accepted as an auth method on the ssh server's configuration)

Vinko Vrsalovic
ssh is configured so it will not asking any passwd .....right ....thanks
Voted you up. This is a valid solution. Obviously, the keys must be protected, but they can also be invalidated just like a password via the server side.
Abyss Knight
I don't think this answers the question. The example shows how to run a remote command, but not how to execute a local script on a remote machine.
Jason R. Coombs
Not certain but can't you pipe your script on hostA to run on hostB using this method?
nevets1219
+4  A: 

If your script is in Machine A, you can't run that on Machine B without copying it over. First, copy the script over to Machine B using scp

[user@machineA]$ scp /path/to/script user@machineB:/home/user/path

Then, just run the script

[user@machineA]$ ssh user@machineB "/home/user/path/script"

This will work if you have given executable permission to the script.

Baishampayan Ghose
hii applied recommended suggession but it give me following error[oracle@node1 ~]$ ssh oracle@node2:./home/oracle/au/fs/conn.shssh: node2:./home/oracle/au/fs/conn.sh: Name or service not known[oracle@node1 ~]$
'ssh oracle@node2:./home/oracle/au/fs/conn.sh'? Wrong command line, the command name should be separated from the user@host part with a space, not a colon.
bortzmeyer
I'm downvoting this because it's primary claim that it can't be run without copying it over is incorrect.
Jason R. Coombs
Jason: No problem :)
Baishampayan Ghose
+2  A: 

Assuming you mean you want to do this automatically from a "local" machine, without manually logging into the "remote" machine, you should look into a TCL extension known as Expect, it is designed precisely for this sort of situation. It's home page below looks kind of crappy but don't let that dissuade you; I've also provided a link to a script for logging-in/interacting via SSH.

http://expect.nist.gov/

http://bash.cyberciti.biz/security/expect-ssh-login-script/

George Jempty
i have tried it before... posting this issue on stackoverflow but not geting proper result.....thanks
+7  A: 

Also, don't forget to escape variables if you want to pick them up from the destination host.

This has caught me out in the past.

For example:

user@host> ssh user2@host2 "echo \$HOME"

prints out /home/user2

while

user@host> ssh user2@host2 "echo $HOME"

prints out /home/user

Another example:

user@host> ssh user2@host2 "echo hello world | awk '{print \$1}'"

prints out "hello" correctly.

dogbane
A: 

I am tryin to run a remote script something like ssh user@remote sh script.unx

script.unx on remote machine, runs several commands, but it says

commando not found, it looks like remote script doesnt read enviroment variables

any idea?

I found commando here - http://www.imdb.com/title/tt0088944/
Jason R. Coombs
+1  A: 

Capistrano provides a nice scripting system that automates scripting on one or more servers. It's very handy if you routinely want to run a script on a remote server or servers without the hassle of manually logging in over and over.

Andrew Flanagan
A: 

I am tryin to run a remote script something like ssh user@remote sh script.unx

script.unx on remote machine, runs several commands, but it says

commando not found, it looks like remote script doesnt read enviroment variables

any idea?

try running ssh user@remote sh ./script.unx

Jeremy
This only works if the script is in the default (home) directory on the remote. I think the question is how to run a script stored locally on the remote.
Simeon Fitch
+5  A: 

If Machine A is a Windows box, you can use Plink (part of PuTTY) with the -m parameter, and it will execute the local script on the remote server.

plink root@MachineB -m local_script.sh

If Machine A is a Unix-based system, you can use:

ssh root@MachineB 'bash -s' < local_script.sh

You shouldn't have to copy the script to the remote server to run it.

Jason R. Coombs
+1  A: 

This is an old question, and Jason's answer works fine, but I would like to add this:

ssh user@host <<'ENDSSH'
#commands to run on remote host
ENDSSH

This can also be used with su and commands which require user input. (note the ' escaped heredoc)

Yarek T