How to execute shell commands, scripts on multiple remote Linux servers over SSH

Ganesh Velrajan
7 min readAug 19, 2021

When you have hundreds or thousands of linux servers or IoT devices or Raspberry Pi deployed in the field, it becomes very hard, challenging and time consuming to manually remote SSH login to each one of those IoT devices to run commands to re-configure, update software, or execute a script.

SocketXP OTA (Over The Air) feature eliminates the need to manually SSH login to a set of remote Linux devices to perform activities such as re-configuring, updating software and executing shell commands/scripts/Python programs. The OTA feature automates and simplifies all the above mentioned device software management tasks. SocketXP is absolutely free to try for all users.

Various Usecases & Scenarios:

In this section, we’ll discuss how to create and deploy SocketXP OTA updates to execute shell commands or scripts on multiple remote linux servers for the following usecases:

  • Execute a shell command or a shell script in multiple Linux servers
  • Execute a Python program or any program or application in multiple IoT devices
  • Update configuration file in multiple Linux devices
  • Update the application software running in multiple Linux devices
  • Update software packages or tools (such as Java, Python, NodeJS) installed in a group of IoT devices.

Pre-Requisites

You need to install the SocketXP remote SSH agent on your Linux servers or IoT devices to securely remotely access those Linux devices from anywhere. Follow the instructions here to setup Linux device remote SSH access over Internet. It takes less than 30 secs to setup SocketXP agent on your devices using the SocketXP Single-Touch Installation Command.

OTA Deployment Types

OTA updates or deployments can be classified into two broad categories:

  • Script File Deployment
  • Config File Deployment

In the following sections we’ll demonstrate how to perform various device software management tasks using these two OTA deployment types.

Script File Deployment

When you want to run a single shell command or a set of shell commands or a shell script or a python program in a group of Linux based IoT devices(or Linux servers), you could use Script File Deployment to automate the task. Script File Deployment uploads the shell script or the python program from your local machine to a specific directory path in your remote IoT devices and then executes the script execution command you specify to run the script or python program. Script File Deployment also collects the log (outputs and errors) from each one of the IoT devices in the group and presents it to you in the OTA dashboard.

Here are the two IoT Devices we’ll use for this demo:

Step #1:

Go to the OTA page in the portal (opens new window) . Click the Create a Deployment button to create a new deployment.

Step #2:

Click the Script File Deployment button to create a config file deployment.

Step #3:

Fill in the Script File Deployment form. Provide a deployment name that describes about the deployment task. Choose a script file to upload from your local machine to your IoT devices. Also specify a destination filepath in the IoT device where the script file needs to be uploaded. Finally, specify the Device Group to apply the deployment.

Note:

A deployment can be applied only to a specific device group. If you want to deploy the script file on all IoT devices that you manage, then create additional Script File Deployments for each one of the device groups.

Now submit the deployment by clicking the CREATE DEPLOYMENT button in the bottom right corner.

Step #4:

Track the status of the deployment from the OTA deployment summary page. Click the deployment to view more details about the set of IoT devices in which the deployment is being run.

Sample Script Files For Various Use Cases

Usecase #1: To Update SocketXP Agent on your Linux Servers or IoT devices:

#!/bin/sh curl -O https://portal.socketxp.com/download/arm/socketxp && 
chmod +wx socketxp &&
sudo mv socketxp /usr/local/bin
systemctl restart socketxp

You could use the sample script above to update your IoT application too. All you need to do is upload your IoT application to AWS S3 or DropBox or Google Drive. Make the artifact publicly accessible. Obtain the public URL to access your artifact from your cloud storage provider. Update the CURL command above with the appropriate public URL.

Usecase #2: To Install Python packages:

#!/bin/sh echo 'MOTOR_SPEED=300 LED_DISPLAY=true TEST_RUN=yes LOG_FILE="/var/log/mylogfile.log" DEBUG_LEVEL=2' > /var/lib/myapp/app.config systemctl restart myapp

Usecase #3: To Update a config file and restart an app

#!/bin/sh
# Name: script.sh
echo 'MOTOR_SPEED=300
LED_DISPLAY=true
TEST_RUN=yes
LOG_FILE="/var/log/mylogfile.log"
DEBUG_LEVEL=2' > /var/lib/myapp/app.config
systemctl restart myapp

Usecase #4: To Run a Python script/program

You could even run a python program on your IoT devices using the Script File Deployment. Upload a python program instead of a script file and specify the script execution command as python /home/john/get_system_info.py

For example, the following Python script could be used to retrieve system information such as CPU, Memory, and Disk Size.

# Name: get_system_info.py import psutil
import datetime
### *** CPU FUNCTIONS ***# Number of logical CPUs in the system
print ("psutil.cpu_count() = {0}".format(psutil.cpu_count()))
### *** DISK FUNCTIONS ***# List of named tuples containing all mounted disk partitions
dparts = psutil.disk_partitions()
print("psutil.disk_partitions() = {0}".format(dparts))
# Disk usage statistics
du = psutil.disk_usage('/')
print("psutil.disk_usage('/') = {0}".format(du))
### *** MEMORY FUNCTIONS ***# System memory usage statistics
mem = psutil.virtual_memory()
print("psutil.virtual_memory() = {0}".format(mem))
THRESHOLD = 100 * 1024 * 1024 # 100MB
if mem.available <= THRESHOLD:
print("warning, available memory below threshold")

Need help? Please write to us at support@socketxp.com if you need any assistance with creating a script or python program for your special use-case.

REST APIs

SocketXP also provides you with REST APIs to write your own tool or application around this OTA feature. You can find the REST API documentation here.

Config File Deployment

When you want to update a configuration file in a group of IoT devices, you could use the Config File Deployment to automate the task. Config File Deployment simply uploads a new config file from your local machine to a specific directory path in your remote IoT devices.

Here are the two IoT Devices we’ll use for this demo:

Step #1:

Go to the OTA page in the portal (opens new window) . Click the Create a Deployment button to create a new deployment.

Step #2:

Click the Config File Deployment button to create a config file deployment.

Step #3:

Fill in the Config File Deployment form. Provide a deployment name that describes about the deployment task. Choose a new config file to upload from your local machine to your IoT devices. Also specify a destination filepath in the IoT device where the config file needs to be uploaded. Finally, specify the Device Group to apply the deployment.

Note:

A deployment can be applied only to a specific device group. If you want to deploy the config file on all IoT devices that you manage, then create additional Config File Deployments for each one of the device groups.

Now submit the deployment by clicking the CREATE DEPLOYMENT button in the bottom right corner.

Step #4:

Track the status of the deployment from the OTA deployment summary page

Step #5:

Click the deployment to see more details about the IoT devices in which the config file is being deployed.

You can click the “VIEW LOG” button to see the output log and error log. For Config File Deployments, which involve a simple file copy, logs will be mostly empty.

If you wish, but not required, you could SSH into your devices to confirm the deployment is successful.

temp-sensor-1$ ls /var/lib/myapp/ config.json

Originally published at https://www.socketxp.com.

--

--

Ganesh Velrajan

Ganesh Velrajan is the founder of Ampas Labs Inc. Learn more about our SSH Remote Access Solutions at https://www.socketxp.com and https://www.bastionxp.com