How can I 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.
Join Stack Overflow to learn, share knowledge, and build your career.
|
How can I 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. |
|||||||||||||||||||||
closed as off topic by Adam Lear♦ Nov 30 '11 at 4:02Questions on Stack Overflow are expected to relate to programming within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question. |
|||||||||||||||||||||
|
|||||||||||||||||||||
|
This is an extension to YarekT's answer to combine inline remote commands with passing ENV variables from the local machine to the remote host so you can parameterize your scripts on the remote side:
I found this exceptionally helpful by keeping it all in one script so it's very readable and maintainable. Why this works. ssh supports the following syntax:
In bash we can specify environment variables to define prior to running a command on a single line like so:
That makes it easy to define variables prior to running a command. In this case echo is our command we're running. Everything before echo defines environment variables. So we combine those two features and YarekT's answer to get:
In this case we are setting ARG1 and ARG2 to local values. Sending everything after user@host as the remote_command. When the remote machine executes the command ARG1 and ARG2 are set the local values, thanks to local command line evaluation, which defines environment variables on the remote server, then executes the bash -s command using those variables. Voila. |
|||||||||||||||||
|
|
This is an old question, and Jason's answer works fine, but I would like to add this:
This can also be used with su and commands which require user input. (note the Edit: Since this answer keeps getting bits of traffic, i would add even more info to this wonderful use of heredoc: You can nest commands with this syntax, and thats the only way nesting seems to work (in a sane way)
You can actually have a conversation with some services like telnet, ftp, etc. But remember that heredoc just sends the stdin as text, it doesn't wait for response between lines Edit: I just found out that you can indent the insides if you use
(I think this should work) |
|||||||||||||||||||||
|
|
I've started using Fabric for more sophisticated operations. Fabric requires Python and a couple of other dependencies, but only on the client machine. The server need only be a ssh server. I find this tool to be much more powerful than shell scripts handed off to SSH, and well worth the trouble of getting set up (particularly if you enjoy programming in Python). Fabric handles running scripts on multiple hosts (or hosts of certain roles), helps facilitate idempotent operations (such as adding a line to a config script, but not if it's already there), and allows construction of more complex logic (such as the Python language can provide). |
||||
|
|
|
First, copy the script over to Machine B using scp
Then, just run the script
This will work if you have given executable permission to the script. |
|||||||||||||||||||||
|
|
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.
If Machine A is a Unix-based system, you can use:
You shouldn't have to copy the script to the remote server to run it. |
|||||||||||||||||||||
|
try running ssh user@remote sh ./script.unx |
|||||||||
|
|
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:
prints out /home/user2 while
prints out /home/user Another example:
prints out "hello" correctly. |
|||||||||||||
|
|
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. |
|||
|
|
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) |
|||||||||||||||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?