ssh批量登录并查询系统信息shell
2024-04-09 / shell / 515 次围观 / 0 次吐槽 /#!/bin/bash
# 检查并创建必要的输出文件
for file in login.txt out.txt
do
if [ ! -f "$file" ]; then
touch "$file"
fi
done
# 读取serverlist.txt文件
while IFS=' ' read -r ip root_password user_password; do
# 使用普通用户weihu和遍历到的普通用户密码登录目标服务器
if ! sshpass -p "$user_password" ssh -o StrictHostKeyChecking=no weihu@$ip <<-'END_SSH'
# 如果成功登录,输出欢迎信息(这里仅为示例,可自定义)
echo "Successfully logged in as weihu on ${ip}"
# 添加您需要以weihu身份执行的命令或操作
# 查看系统版本信息
system_version=$(cat /etc/*release)
# 验证匹配到的root密码是否正确
if echo "$root_password" | su -l root -c "id"; then
root_password_status="Valid"
else
root_password_status="Invalid"
fi
# 查看网卡bond0与bond1绑定的IP,并过滤掉用于SSH登录的IP
bond_ips=$(ip addr show bond0,bond1 | grep -Po 'inet \K[\d.]+')
filtered_bond_ips=$(echo "$bond_ips" | grep -v "^$ip$")
# 输出到控制台(保留,便于实时观察)
echo "System Version:"
echo "$system_version"
echo "Root Password Status: $root_password_status"
echo "Bond IPs (excluding SSH login IP):"
echo "$filtered_bond_ips"
# 将相关信息追加到out.txt文件
echo "IP: $ip" >> out.txt
echo "System Version: $system_version" >> out.txt
echo "Root Password Status: $root_password_status" >> out.txt
echo "Bond IPs (excluding SSH login IP):" >> out.txt
echo "$filtered_bond_ips" >> out.txt
echo "==============================" >> out.txt
END_SSH
then
# 登录失败时,将IP、匹配到的weihu密码以及密码错误文本追加保存到login.txt文件中
echo "${ip} ${user_password} Password authentication failed for user weihu" >> login.txt
fi
# 检查上一条命令(sshpass)的退出状态,非0表示失败,打印错误信息并继续下一台服务器
if [ $? -ne 0 ]; then
echo "Failed to login or perform operations on ${ip}. Skipping to the next server..."
fi
done < serverlist.txt#!/bin/bash
# 定义起始和结束IP地址
start_ip="138"
end_ip="140"
# 定义用户密码(注意:这不是一个好做法,最好使用SSH密钥)
root_password=''
weihu_password=''
# 生成IP列表
ip_list=()
for (( i=$start_ip ; i<=$end_ip ; i++ )); do
ip_list+=("10.142.19.$i")
done
# 检查并创建必要的输出文件
for file in info.txt err.txt; do
if [ ! -f "$file" ]; then
touch "$file"
fi
done
# 遍历IP列表
for ip in "${ip_list[@]}"; do
echo "开始登录 IP: $ip"
# 使用sshpass和明文密码登录目标主机(注意:这不是一个好的做法)
if ! sshpass -p "$weihu_password" ssh -o StrictHostKeyChecking=no weihu@$ip << EOF
set -e
# 验证root密码是否正确
if [ $(echo "$root_password" | su -l root -c "id -un") ]; then
root_password_status="正确"
else
root_password_status="错误"
fi
echo "root密码状态: $root_password_status"
# 查看网卡绑定的IP,并过滤掉用于SSH登录的IP
bond_ips=$(ip addr show | grep -Po 'inet \K[\d.]+')
filtered_ips=$(echo "$bond_ips" | grep -v -e "^$ip$" -e "127.0.0.1")
echo "网卡绑定的IP: $filtered_ips"
# 执行cat /etc/os-release命令并提取PRETTY_NAME的值
os_release_info=$(cat /etc/*-release 2>/dev/null)
if [ -n "$os_release_info" ]; then
pretty_name=$(echo "$os_release_info" | grep '^PRETTY_NAME=' | cut -d'=' -f2- | tr -d '"')
echo "系统版本: $pretty_name"
else
echo "无法获取系统版本信息"
fi
# 将相关信息追加到info.txt文件
echo "带内IP: $ip"
echo "系统版本: $pretty_name"
echo "root密码状态: $root_password_status"
echo "绑定IPs: $filtered_ips"
echo "=============================="
EOF
then
echo "$ip 登录失败"
echo "$ip@weihu 登录失败" >> err.txt
fi
# 添加延迟 1秒
sleep 1
donePowered By Cheug's Blog
Copyright Cheug Rights Reserved.