Scripting repetitive tasks improves the efficiency of system administration.

That’s great for local machines, but what if you oversee remote servers?

Can you run a local script on a remote computer?

Laptop on a blue background showing a Linux command prompt.

fatmawati achmad zaenuri/Shutterstock.com

Remote Connections

Remote system administration usually involves making a connection to the remote computer over asecureshell connection.

TheSSH connectionprovides you with a command prompt on the remote computer.

you’ve got the option to then go right ahead and perform whatever system maintenance is required.

Using chmod to make a script executable

As time goes by, you’ll tweak and improve your scripts.

Bash and SSH provide a way to do just that.

Passwordless SSH Connections

The best way to do this is with passwordless connections, using SSH keys.

launching a local script to run on a remote server over SSH

If you’re already doing remote system administration on them, both of these requirements must already be satisfied.

You’re dropped onto a command prompt on the remote server without being prompted for a password.

Our script is very simple.

Using cat to look inside the timestamp.txt file

It writes a timestamp into a file called “timestamp.txt”, on the remote server.

Note that the script concludes with the exit command.

On our local machine, we’ll launch the script like this:

Here’s how this works.

The contents of the timestamp.txt file after several runs of the script

When the script runs we’re returned to the command prompt of the local machine.

Hopping over to our remote machine, we can use cat to look inside the “timestamp.txt” file.

We can see the timestamp of the last—and currently only—connection.

launching a local script with command line parameters to run on a remote server over SSH

Running the local script several more times adds corresponding timestamps to the remote file.

Of course, in a real-world situation, your script would do something more useful.

But even our trivial example does demonstrate that a local script is being executed on a remote server.

Checking that parameters to the script were received and processed correctly on the remote server

Passing Arguments to the Script

you’re able to pass command line arguments to the script.

We’ll modify our script to expect three command line parameters.

These are redirected into the “timestamp.txt” file along with the timestamp.

Launching script3.sh with a mix of local and remote processing

Save this script as “local2.sh”, and make it executable withchmod.

The double-hyphen “–” tells Bash that what follows shouldn’t be considered command line parameters for thesshcommand.

The three parameters for the script follow the script name, as usual.

The timestamp entry from script3.sh in the timestamp.txt file on the remote server

We can check withcatthat our parameters were received and handled correctly on the remote server.

Here documents allow us to redirect lines from a labeled section of a script into a command.

Local processing can be performed above and below the here document.

This is script “local3.sh”, which contains a here document.

We’re connecting as user “dave” on a remote server called “fedora-36.local.”

We’re also using the-T(disable pseudo-terminal allocation) option.

This prevents the remote server from providing an interactive terminal for this connection.

The redirection “«” is followed by the name of a label.

In this example, we’re using “_remote_commands.”

There’s nothing special about this label, it is simply a label.

All commands that appear on the lines following the redirection are sent over the SSH connection.

The redirection stops when the label is encountered.

The execution of the script then continues with the line following the label.

Let’s run our mixed local/remote processing script.

As expected, we see a new entry in the “timestamp.txt” file.

Related:How to Manage Linux Servers with the Cockpit Web Interface