需求来源
- 因为有部分批次机器操作系统为Alibaba Group Enterprise Linux Server release 5.7 (CatFeces),不能使用ansible管理,但是需要经常修改密码,故编写此脚本
- 需要准备密码文件(格式为 ipaddress password)
- 自动生成密码文件脚本
``
#!/bin/bash producepass () { read -p "Please enter AddressSegment StartAddress StopAddress PassLength: " addr start stop leng for i in $(seq ${start} ${stop}) do echo ${addr}.${i}
cat /dev/urandom|tr -dc '0-9a-zA-Z!@#$%^&()_+-=?'|head -c ${leng}`
done
}
echo -e "\033[35m1:Generate password. \t# Need enter AddressSegment StartAddress StopAddress PassLength\033[0m"
read -p "Please select a function: " func
case $func in
1)
producepass
;;
*)
echo "Input option Error." &&exit 10
;;
esac- 自动连接远程服务器并修改密码
#!/bin/bash pass='$PASSWORD' user='$USERNAME' i=0 for ip_pass in $(cat test_ip.txt) do if [ `echo $[$i%2]` -eq 0 ] then ip=$ip_pass echo $ip elif [ `echo $[$i%2]` -eq 1 ] then new_pass=$ip_pass echo ${new_pass} /usr/bin/expect<<-EOF spawn ssh ${user}@${ip} expect { "*assword:" { send "${pass}\r" }; } expect "$" send "LANG=\r" send "sudo passwd ${user}\r" expect "${user}:" send "${pass}\r" expect "*assword:" send "${new_pass}\r" expect "password:" send "${new_pass}\r" expect "successfully." send "logout\r" expect eof EOF fi let i++ done