0
0
Bash Scriptingscripting~30 mins

SSH automation in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
SSH Automation with Bash Scripting
📖 Scenario: You are managing multiple remote servers and want to automate running a simple command on them using SSH. This will save you time and avoid typing the same commands repeatedly.
🎯 Goal: Create a bash script that stores a list of server IPs, sets a username variable, loops over the servers to run a command via SSH, and prints the output for each server.
📋 What You'll Learn
Create a bash array called servers with three IP addresses
Create a variable called user with the SSH username
Use a for loop with variable server to iterate over servers
Run ssh command to execute hostname on each server
Print the server IP and its hostname output
💡 Why This Matters
🌍 Real World
System administrators often need to run commands on many servers. Automating SSH commands saves time and reduces errors.
💼 Career
Knowing how to automate SSH tasks with bash scripting is a valuable skill for roles in IT support, DevOps, and system administration.
Progress0 / 4 steps
1
Create the list of servers
Create a bash array called servers with these exact IP addresses: 192.168.1.10, 192.168.1.20, and 192.168.1.30.
Bash Scripting
Need a hint?

Use parentheses () to create a bash array and double quotes "" around each IP.

2
Set the SSH username
Create a variable called user and set it to the exact string admin.
Bash Scripting
Need a hint?

Assign the string admin to the variable user using = and quotes.

3
Loop over servers and run SSH command
Use a for loop with variable server to iterate over servers. Inside the loop, run the SSH command ssh $user@$server hostname and store the output in a variable called host_name.
Bash Scripting
Need a hint?

Use for server in "${servers[@]}" to loop. Use $(ssh "$user@$server" hostname) to capture output.

4
Print the server and hostname output
Inside the for loop, add a echo statement to print: Server: $server, Hostname: $host_name.
Bash Scripting
Need a hint?

Use echo "Server: $server, Hostname: $host_name" inside the loop to print the output.