需求
服务器密码不统一,运维管理困难,需要将服务器密码统一进行设置
修改服务器密码流程
cd /opt/
wget https://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz --no-check-certificate
tar -zxvf tcl8.4.19-src.tar.gz
cd tcl8.4.19/unix && ./configure --prefix=/usr/tcl && make && make install
cd /opt/
wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz --no-check-certificate
tar -zxvf expect5.45.tar.gz
cd expect5.45/ && ./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.4.19/generic && make && make install
ln -s /usr/tcl/bin/expect /usr/bin/expect
mkdir /opt/scripts/passwd -p
cd /opt/scripts/passwd
#准备 action.exp,例如服务器密码统一设置为 123456
# cat action.exp
#!/usr/bin/expect
set ipaddr [lindex $argv 0]
spawn ssh root@$ipaddr
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "#"
send "echo '123456' |passwd --stdin root\r"
send "exit\r"
expect eof
#准备主机列表
# cat host.txt
10.12.1.0
10.147.100.113
#准备执行修改服务器密码的脚本
# cat passwd.sh
#!/bin/bash
for h in `cat host.txt`;do
expect action.exp $h
done
#执行脚本
# sh passwd.sh