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
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
<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)
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.
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.
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.
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?
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.
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
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.
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)