SSH Remote Execution: Run Local Bash Scripts on Multiple Servers Without Agent Installation


17 views

When managing multiple servers, the simplest method to execute local scripts remotely is using SSH's pipe functionality. This requires no pre-installed agents and works with standard SSH access:


cat local_script.sh | ssh user@remote-server "bash -s"

For more robust execution across multiple servers, consider this enhanced approach:


#!/bin/bash
SCRIPT_PATH="/path/to/configure.sh"
SERVERS=("server1.example.com" "server2.example.com" "192.168.1.10")

for server in "${SERVERS[@]}"; do
  echo "Executing on ${server}"
  ssh -T "admin@${server}" < "${SCRIPT_PATH}"
done

To pass arguments to your remote script:


ssh user@remote-server 'bash -s' < local_script.sh arg1 arg2

When your script depends on environment variables:


ssh user@remote-server "env VAR1=value1 VAR2=value2 bash -s" < script.sh

For production use, implement proper error handling:


ssh user@remote-server "bash -s" < script.sh 2>&1 | tee -a remote_exec.log
exit_code=${PIPESTATUS[0]}
if [ $exit_code -ne 0 ]; then
  echo "Execution failed on ${remote-server}" >&2
  exit $exit_code
fi
  • Use SSH keys instead of passwords
  • Limit SSH access to specific IPs
  • Consider using ssh-agent for key management
  • Validate all remote inputs in your scripts

For large-scale deployments:


parallel -j 10 ssh user@{} 'bash -s' < script.sh ::: "${SERVERS[@]}"

When managing multiple remote servers, you often need to execute configuration scripts without installing additional software on target machines. SSH provides a perfect solution for this scenario.

The simplest method is to pipe your local script through SSH:

cat local_script.sh | ssh user@remote_host "bash -s"

This approach:

  • Requires no pre-installed software on remote machines
  • Works with any standard SSH server
  • Preserves your local script's variables and logic

You can pass arguments just like with local scripts:

cat configure.sh | ssh admin@server01 "bash -s -- --param1 value1 --param2 value2"

For executing across multiple hosts, use a loop:

#!/bin/bash
SCRIPT="./deploy_config.sh"
HOSTS=("server1.example.com" "server2.example.com" "server3.example.com")

for host in "${HOSTS[@]}"; do
    echo "Deploying to ${host}"
    cat "${SCRIPT}" | ssh "admin@${host}" "bash -s"
done

For scripts requiring environment variables:

ssh user@remote_host "export VAR1=value1 && export VAR2=value2 && bash -s" < local_script.sh

Implement proper error handling:

ssh user@remote_host "bash -s" < setup.sh 2>&1 | tee remote_execution.log
  • Use SSH keys instead of passwords
  • Limit SSH access to specific IPs
  • Consider using ssh-agent for credential management

For larger scripts, first copy then execute:

scp config_script.sh user@remote_host:/tmp/
ssh user@remote_host "chmod +x /tmp/config_script.sh && /tmp/config_script.sh"

Here's how to install CFEngine without manual intervention:

#!/bin/bash
# bootstrap_cfengine.sh
cat << 'EOF' | ssh root@new_host "bash -s"
#!/bin/bash
wget https://cfengine.com/pub/gpg.key
rpm --import gpg.key
yum install -y cfengine-community
systemctl start cfengine3
EOF