Executing local programs and scripts remotely

Theory

If you want to execute a local command (be it a script or a program) remotely, you can upload it via SCP first, and then execute it via SSH. But why make two connections, when one is enough?

To execute a local script or a program on a remote machine, do something like below:

or


It will copy the command, and execute it using just one SSH connection.

You can also remove the command after its execution (just add rm -f as the last command), "randomize" its name, etc.

It's useful for remote monitoring, with nagios, etc.


If the script doesn't need any arguments, this simple command will suffice:

Example usage with nagios

Nagios already has a plugin which enables checking services using SSH. Unfortunately, one of the prerequisites is that the plugin you are about to execute needs to be installed on the remote server.

Example plugin

This nagios plugin will upload and execute any nagios check on a remote machine, using SSH.

  • user nagios needs to be able to login to the remote machine using keys,
  • if the plugin already exists on the remote side, it won't be uploaded,
  • the plugin will be uploaded only once; all subsequent times it will use already uploaded plugin,
  • the plugins from your nagios server need to be exacutable on the remote server (they won't run if the remote architecture is different, i.e. your nagios server is a x86 box, and the remote machine runs on a MIPS CPU, or, when your glibc versions differ a lot) - if it's the case, you have to install correct plugins on the remote servers,
  • use only on the remote servers you trust; if you want to use it on the remote servers you don't trust, change the REMOTEDIR to the path only accessible by the nagios user.

Save the below script as check_remote to your nagios plugin directory.

Example nagios command definition

These two commands check system load and free space on remote servers.

Example nagios service definition

Two example service definitions - for checking system load and free space on remote servers.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.