寻求读取服务器列表文件并带密码登录的SSH执行脚本方案
Hey there! I’ve got a couple of practical, working solutions for you to automate this task. Let’s break them down step by step—no scripting expertise required to follow along.
Method 1: Using sshpass (Simpler, Straightforward Approach)
First, you’ll need to install sshpass on your local machine—it lets you pass the SSH password directly in commands, which is perfect for quick automation.
On Debian/Ubuntu-based systems:
sudo apt update && sudo apt install sshpass -y
On RHEL/CentOS-based systems:
sudo yum install sshpass -y
Now create a script named collect_uptime.sh with this content:
#!/bin/bash # Define where we'll save the final uptime results OUTPUT_FILE="/home/kauk2/uptime.out" # Clear the output file (or create it if it doesn't exist) > "$OUTPUT_FILE" # Check if our server list file exists first if [ ! -f "list.txt" ]; then echo "Error: list.txt not found in your current directory!" exit 1 fi # Loop through each server in list.txt while IFS= read -r server; do # Skip any empty lines in the list [ -z "$server" ] && continue echo "Collecting uptime from $server..." # Use sshpass to send the password, run the uptime command, and extract the runtime sshpass -p "abcd1234" ssh -o StrictHostKeyChecking=no "$server" "uptime" | awk -F, '{sub(".*up ",x,$1);print $1}' >> "$OUTPUT_FILE" # Optional: Add a separator between servers to make the output easier to read echo "---" >> "$OUTPUT_FILE" done < "list.txt" echo "All done! Uptime results saved to $OUTPUT_FILE"
How to use this script:
- Make sure
list.txthas one server IP or hostname per line (no extra spaces or blank lines unless you want them skipped). - Give the script permission to run:
chmod +x collect_uptime.sh - Execute it:
./collect_uptime.sh
Method 2: Using expect (More Flexible, No sshpass Needed)
If you’d rather avoid sshpass, expect is a tool built for automating interactive commands like SSH logins. Install it first:
On Debian/Ubuntu:
sudo apt install expect -y
On RHEL/CentOS:
sudo yum install expect -y
Create a script named collect_uptime_expect.sh:
#!/usr/bin/expect set output_file "/home/kauk2/uptime.out" set password "abcd1234" # Clear the output file before starting exec > "$output_file" # Read the server list from list.txt set list_file [open "list.txt" r] set servers [split [read $list_file] "\n"] close $list_file foreach server $servers { # Skip empty lines in the list if {$server eq ""} {continue} send_user "Collecting uptime from $server...\n" # Spawn the SSH connection and handle prompts spawn ssh "$server" "uptime" expect { "password:" { send "$password\r" exp_continue } "Are you sure you want to continue connecting" { send "yes\r" exp_continue } eof {} } # Process the uptime output and save it set uptime_output [join [split $expect_out(buffer) "\n"]] if {$uptime_output ne ""} { set processed [exec echo "$uptime_output" | awk -F, '{sub(".*up ",x,$1);print $1}'] exec echo "$processed" >> "$output_file" exec echo "---" >> "$output_file" } } send_user "Done! Uptime results saved to $output_file\n"
Usage steps:
- Make the script executable:
chmod +x collect_uptime_expect.sh - Run it:
./collect_uptime_expect.sh
Important Notes for Safety & Reliability:
- Security Warning: Storing passwords in plaintext scripts is not secure. Once you get this working, I highly recommend setting up SSH key-based authentication—this eliminates the need for passwords entirely and makes the process much safer.
- The
-o StrictHostKeyChecking=noin thesshpassmethod skips host key verification (to avoid manual prompts). For production use, add the servers’ host keys to your~/.ssh/known_hostsfile instead of using this option. - Double-check that your user has write permissions to
/home/kauk2/uptime.out. If not, adjust the path or fix permissions withchown/chmod.
内容的提问来源于stack exchange,提问作者Jörg Kauke

