Running a shell script from your computer on a remote server using SSH is very useful. This is especially true for system administrators and developers who need to manage servers far away.
For example, imagine you are a system administrator. You need to update software on many servers that are not in the same location as you. Instead of logging into each server one by one, you can use a shell script to do this job for all servers at once. This saves you a lot of time and effort.
Here is a simple guide to show you how to do it step-by-step:
Prerequisites
- SSH access to the remote server.
- A local shell script that you want to execute remotely.
- SSH client installed on your local machine.
Step 1: Ensure SSH Access
Before proceeding, make sure you have SSH access to the remote server. You can test this by running:
ssh username@remote-server-ip
Replace username with your remote username and remote-server-ip with the IP address or hostname of the remote server.
Step 2: Create Your Local Script
Create a shell script on your local machine. For example, let’s name it check_mem.sh.
#!/bin/bash
echo "Current date and time: $(date)"
echo "Free memory on the server:"
free -h
#echo "Memory usage summary:"
#vmstat -s
Make sure the script is executable:
chmod +x check_mem.sh
Step 3: Execute Script Directly Over SSH
Instead of copying the script to the remote server, you can execute it directly using SSH. Use this command:
ssh username@remote-server-ip 'bash -s' < check_mem.sh
This command will run check_mem.sh directly on the remote server without needing to copy it first.
Conclusion
Running a shell script from your computer on a remote server using SSH is a powerful skill. It saves you time and makes managing multiple servers much easier. Just write your script, make it executable, and use SSH to run it on the remote server. This simple guide helps you do all this step-by-step.