How To SSH Multiple Servers Using a Bash Shell Script

ssh mutliple servers by automating SSH connection using bash shell script

How To SSH Multiple Servers Using a Bash Shell Script

Introduction:
In today’s technology-driven world, system administrators and DevOps professionals often find themselves managing multiple servers simultaneously. Manually connecting to each server can be time-consuming and tedious. However, by utilizing a bash shell script, you can automate the process and establish SSH connections to multiple servers effortlessly. This blog post will guide you through the steps required to create a bash script that enables you to SSH To multiple servers quickly and efficiently.

Step-by-Step Guide : SSH Multiple Servers Using Bash Shell Script | 

Learn how to use a bash shell script to establish SSH connections to multiple servers effortlessly. Follow our step-by-step guide and optimize your server management process.

Section 1: Understanding the Problem:
Managing multiple servers manually can be time-consuming and error-prone. A bash shell script can help automate this process and simplify server management.

Section 2: Creating the Bash Shell Script: To get started, open your preferred text editor and create a new file. Save it with a descriptive name, such as “ssh_multiple_servers.sh”. Make sure the file has executable permissions.

Section 3: Writing the Script: Copy the following script into your newly created file:

Note:- In this script we’re using password based authentication. But you can also use key based authentication
We’ll also give an example of the script for key based authentication at the end of this article.

#!/bin/bash
PS3="Select a host from the given list ==> "
select host in server1 server2 server3 .. servern
do
    case $host in
        server1)
            sshpass -p 'password' ssh username@server1-hostname;;
        server2)
            sshpass -p 'password' ssh username@server2-hostname;;
        server3)
            sshpass -p 'password' ssh username@server3-hostname;;
        .
        .
        servern)
            sshpass -p 'password' ssh username@servern-hostname;;
        *)
            echo "Wrong Input! Exiting..."
            exit;;
    esac
done

Section 4: Explanation of the Script:
Let’s break down the script to understand how it works:

    • The PS3 variable sets the prompt for the select menu, asking the user to choose a host from the provided list.

    • The select statement initializes a menu where you can select a host.

    • The case statement matches the selected host with the corresponding server hostname and establishes an SSH connection using sshpass and the ssh command.

    • The *) wildcard in the case statement handles any input other than the provided options.

    • If an incorrect input is provided, the script displays an error message and exits.

Section 5: Customization:
To use the script with your own servers, you need to make a few modifications:

    • Replace server1, server2, server3, etc., with the actual server names or IP addresses.

    • Replace 'password' with the actual password for the SSH connection.

    • Replace username with the appropriate username for each server.

    • Replace server1-hostname, server2-hostname, server3-hostname, etc., with the actual hostnames or IP addresses of the respective servers.

Section 6: Executing the Script:
Save the modifications to the script file. Open a terminal and navigate to the directory containing the script. Run the following command to make the script executable if needed:

chmod +x ssh_multiple_servers.sh

To execute the script, run the following command:

./ssh_multiple_servers.sh

The script will display a menu of hosts, and you can choose the desired server by entering the corresponding number.

Key Based Authentication Bash Shell Script:

#!/bin/bash

# Set the list of hosts
hosts=("server1" "server2" "server3" "servern")

# Function to handle SSH connection
connect_ssh() {
  local host=$1
  local ssh_key="path/to/private/key" 
  local username="your-username"       

  ssh -i "$ssh_key" "$username@$host-hostname"
}

# Print the host selection menu
PS3="Select host from the given list ==> "
select host in "${hosts[@]}"
do
  case $host in
    server1)
      connect_ssh "server1"
      ;;
    server2)
      connect_ssh "server2"
      ;;
    server3)
      connect_ssh "server3"
      ;;
    servern)
      connect_ssh "servern"
      ;;
    *)
      echo "Wrong Input! Exiting..."
      exit
      ;;
  esac
done

Here we are using functions, We’ll break it into multiple parts to understand

connect_ssh() {
  local host=$1
  local ssh_key="path/to/private/key"
  local username="your-username"

  ssh -i "$ssh_key" "$username@$host-hostname"
}

This is a function named connect_ssh that establishes an SSH connection to the specified host. It takes the hostname as an argument and uses the ssh_key and username variables to form the SSH command. You need to update ssh_key with the path to your private key file and username with your actual username.

PS3="Select host from the given list ==> "
select host in "${hosts[@]}"
do
  case $host in
    server1)
      connect_ssh "server1"
      ;;
    server2)
      connect_ssh "server2"
      ;;
    server3)
      connect_ssh "server3"
      ;;
    servern)
      connect_ssh "servern"
      ;;
    *)
      echo "Wrong Input! Exiting..."
      exit
      ;;
  esac
done

This is the main part of the script that displays a menu to select the desired host. The select loop waits for user input and assigns the selected value to the host variable. The case statement checks the value of host and calls the connect_ssh function with the corresponding hostname. If an invalid input is provided, the script prints an error message and exits.

Make sure to update the hosts array, ssh_key, and username variables with the appropriate values for your setup. Also, replace "server1-hostname", "server2-hostname", etc., with the actual hostnames or IP addresses of your servers.

Remember to set the correct permissions for the private key file, typically restricting access to only the owner.

Section 7: Conclusion:
In this blog post, we explored how to establish SSH connections to multiple servers using a bash shell script. By automating this process, you can easily manage ssh connections to multiple servers using bash script. For more Linux topics, click here

 

Nitin Kumar

Nitin Kumar is a skilled DevOps Engineer with a strong passion for the finance domain. Specializing in automating processes and improving software delivery. With expertise in cloud technologies and CI/CD pipelines, he is adept at driving efficiency and scalability in complex IT environments.

This Post Has One Comment

Comments are closed.