Running Commands On A Remote Machine

How can I make a script run a command on my remote machine?

Sometimes I’m surprised by all the things that I don’t know. This was one of them.

There are excellent automation tools to make local workflows less tedious, but only one of those would be a good solution for running remote commands.

And using Fabric requires creating a project and a lot of thought. Too much effort for a one-off casual command.

So, here’s an overview as the result of my research today. There are multiple options, varying in complexity and closeness to bash:

SSH

If you need to run a single command, without changing users, you can use SSH for it!

I didn’t know either. Sure, you can use it to log into a machine, but you can also just run a single command.

Here’s how it looks like:

ssh YOURMACHINE systemctl reload caddy

You can execute most commands, instead of the systemctl reload caddy part - but that’s the one I needed. Fancier versions are harder, but if your requirements are more elaborate and workflows more complex, maybe a more powerful tool is a better fit.

Fabric

My Python-powered automation tool of choice, for automating remote and local, but mostly remote workflows.

There probably are other options, like Capistrano (a ruby-powered alternative to my best knowledge), but I haven’t tried them personally.

Ansible

Now we go into the configuration management direction of tools. Ansible can be used to set up whole machines, but it can also help you run single commands.

It would be overkill for simple, single-command use cases, and might be a big step if you’re not used to it.

You can read more here. If Ansible is part of your toolbelt, it could be a good solution.

Salt

And finally, Salt. It’s a configuration management tool, similar to Ansible in a way. You can use it to do fancy stuff, and it can run in various modes.

For running one-off commands, its agent-less mode of operation is what you should know about. Instead of having to install an agent on the machine where you want to run your commands, Salt can be configured to run commands on a remote machine via SSH under the hood.

Anything Else?

I’m sure each of those tools has its own downside, nasty gotchas and scary-feeling learning curve.

Personally, I can’t see myself using anything else apart from SSH and Fabric to run one-off commands on remote machines. But it’s useful to know that more complex tools can help you with this simple task as well.

I hope this overview helped! All the best with not-having-to-SSH-to-remote-machines manually anymore!

Subscribe to my newsletter!
You'll get notified via e-mail when new articles are published. I mostly write about Docker, Kubernetes, automation and building stuff on the web. Sometimes other topics sneak in as well.

Your e-mail address will be used to send out summary emails about new articles, at most weekly. You can unsubscribe from the newsletter at any time.

Für den Versand unserer Newsletter nutzen wir rapidmail. Mit Ihrer Anmeldung stimmen Sie zu, dass die eingegebenen Daten an rapidmail übermittelt werden. Beachten Sie bitte auch die AGB und Datenschutzbestimmungen .

vsupalov.com

© 2024 vsupalov.com. All rights reserved.