Automated Command Execution on Multiple Remote Linux Machines Using Shell Scripts and SSH

Nov 14, 2025 · Programming · 15 views · 7.8

Keywords: Shell Scripting | SSH Remote Execution | Linux Automation | Batch Commands | RSA Fingerprint Authentication

Abstract: This technical paper provides a comprehensive analysis of writing Shell scripts to execute identical command sequences on multiple remote Linux machines via SSH. The paper begins with fundamental loop structures and SSH command execution mechanisms, then delves into handling sudo operations, automating RSA fingerprint authentication, and associated security considerations. Through complete code examples and step-by-step explanations, it demonstrates implementations ranging from basic to advanced, including host list management, error handling mechanisms, and security best practices. The paper concludes with deployment considerations and optimization recommendations for production environments.

Automation Requirements for Remote Command Execution

In modern IT operations and cloud computing environments, there is often a need to execute identical command sequences on multiple remote Linux machines. This requirement is particularly common in scenarios involving dynamically created virtual machines, batch deployments, and automated operations. Since the remote machines are dynamically created virtual machines, script files cannot be pre-deployed, necessitating remote command execution via the SSH protocol from a control machine.

Basic SSH Command Execution Framework

The core of implementing multi-host command execution using Shell scripts lies in combining loop structures with SSH commands. The basic implementation framework is as follows:

#!/bin/bash
USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
    ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
done

In this example, the script defines three key variables: username, host list, and the command sequence to execute. The loop structure iterates through each host, establishing an SSH connection and executing the specified commands. Command sequences can use semicolons to separate multiple commands, enabling complex operational logic.

Automating RSA Fingerprint Authentication

When connecting to a new remote host for the first time, SSH prompts for RSA fingerprint confirmation, which can interrupt automated scripts. To address this, the StrictHostKeyChecking option can be used:

ssh -o StrictHostKeyChecking=no -l username hostname "pwd; ls"

This option disables host key checking and automatically adds the host key to the known hosts list. To completely avoid leaving records in the known hosts file, the UserKnownHostsFile option can be combined:

ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l username hostname "pwd; ls"

Security Considerations and Risk Mitigation

Disabling host key checking introduces security risks, particularly increasing the threat of man-in-the-middle attacks. In security-sensitive environments, the following alternatives should be considered:

Strategies for Handling Sudo Operations

When sudo operations are required, several approaches can be taken. The simplest method is to include sudo directly in the command sequence:

SCRIPT="sudo apt-get update; sudo apt-get upgrade -y"

This approach requires that the remote machine is configured for passwordless sudo, or that the script can handle password input. A more secure method involves using SSH key authentication combined with sudoers file configuration, allowing specific users to execute certain commands without passwords.

Enhanced Error Handling Mechanisms

In production environments, robust scripts should include comprehensive error handling:

#!/bin/bash
USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"

for HOSTNAME in ${HOSTS} ; do
    echo "Connecting to ${HOSTNAME}..."
    if ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"; then
        echo "${HOSTNAME} command execution successful"
    else
        echo "${HOSTNAME} command execution failed" >&2
    fi
done

Advanced Features and Optimization

For more complex application scenarios, the following enhancements can be considered:

Practical Deployment Recommendations

When deploying such scripts in actual production environments, it is recommended to:

Through proper architectural design and security practices, SSH remote command execution scripts can become powerful tools for automated operations, significantly improving operational efficiency while ensuring system security.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.