前置条件
需要安装expect
需要提前写入一个配置文件
#脚本如下
#!/bin/bash
FileNamePath=/root/list.conf
function Check(){
# shellcheck disable=SC2155
command -v expect >/dev/null 2>&1 || { echo "未安装expect工具,请安装后使用";exit; }
[ -s /root/list.conf ] || { echo "文件 /root/list.conf 不存在或为空"; exit 1; }
#检查字段是否为5
# shellcheck disable=SC2154
awk -F'[ \t]+' '{if (NF != 5) print "字段数量错误: " $0}' "$FileNamePath" || exit
#读取并丢掉第一行
# shellcheck disable=SC2002
cat "$FileNamePath" | {
read -r # 丢弃第一行
while IFS= read -r line; do
# 检查每一行是否符合预期的格式
if ! echo "$line" | grep -P '^([0-9]{1,3}\.){3}[0-9]{1,3}\s+\S+\s+\S+\s+\S+\s+\S+$' >/dev/null; then
echo "格式不匹配: $line"
exit 1
fi
done
}
}
function Expect(){
local IP="$1"
local User="$2"
local Passwd="$3"
local RtPasswd="$4"
local loginUser="$5"
/usr/bin/expect <<-EOF
set timeout 30
spawn ssh ${loginUser}@${IP}
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "${Passwd}\n" }
"Permission denied" { puts "SSH 登录失败: ${loginUser}@${IP}"; exit 1 }
"Connection refused" { puts "连接被拒绝: ${IP}"; exit 1 }
"Could not resolve hostname" { puts "无法解析主机名: ${IP}"; exit 1 }
"unexpected end of file" { puts "SSH 登录过程中发生意外文件结尾"; exit 1 }
}
expect "~" { send "su - root\r" }
expect {
"Password" { send "$RtPasswd\r" }
"Permission denied" { puts "密码错误,登录失败”;exp_continue;exit 1 }
}
expect {
"~" { send "chage -l $User\n" }
"does not exist" { puts "无该用户,不修改密码";exp_continue;exit 0 }
}
expect "~" { send "chage -M 99999 ${User}\n" }
expect "~" { send "exit\n" }
expect "~" { send "exit\n"}
expect eof
EOF
# shellcheck disable=SC2181
if [ $? -ne 0 ];then
echo "Expect 执行失败: ${User}@${IP}" &> /root/error.log
else
echo "修改主机${IP}的用户${User}成功"
fi
}
Check
grep -Ev "^\s*#" "$FileNamePath" | grep -Ev "^\s*$" | while IFS=$'\t ' read -r IP User Passwd RtPasswd loginUser; do
Expect "$IP" "$User" "$Passwd" "$RtPasswd" "$loginUser"
done