如何一次性修改多台linux服务器的密码,这是个问题,
我给你们提供一个脚本,是前一段我刚刚写的,但愿能对你们有所帮助
一 , 需求:linux环境下运行,须要tcl和expect支持
原理说明:利用expect的摸拟交互的功能,登陆到指定的多台服务器上修改密码
共2个程序文件 passall.sh和passone
另外用户须要自建一个ip地址列表文件,每行一个ip
二,程序代码:
//-----------------------------------程序文件一---------------------------------------
passall.sh
#!/bin/bash
if [ "$1" = "" ] || [ "$2" = "" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]
then
echo "usage: passall.sh /path/ip_list.txt /path/passone"
exit
fi
cat $1 | while read line
do
# if ==null
[ -z $line ] && continue
$2 $line;
done
echo -e "\n\npass mod OK!\n"
passone
//-----------------------------------程序文件二---------------------------------------
#!/usr/bin/expect -f
#-------------------------------------------------- about us
# product: passone
# Author: liuhongdi <
[email]liuhongdi@gmail.com[/email]>
# Last Modified: 2008-05-13
# version: 0.2.2
# user:this script will help you to modify password for many linux(unix) machine
# license: this script is based GNU GPL
#-------------------------------------------------- set the variable,you can modify the value
set loginuser "testuser"
set loginpass {testpass}
set ifsu 1
set su_user "suroot"
set su_pass {surootpass}
set passuser "passusername"
set newpass "passuserpass"
set ipaddr [lrange $argv 0 0]
set timeout 300
set cmd_prompt "]#|~]?"
set ifsshtest 1
#-------------------------------------------------- login by ssh
spawn ssh
$loginuser@$ipaddr
set timeout 300
expect {
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
} -re "assword:" {
send "$loginpass\r"
} -re "Permission denied, please try again." {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}
expect {
-re "assword:" {
send "$loginpass\r"
}
-re $cmd_prompt {
send "\r"
}
}
#---------------------------------------------------- if we need su
expect {
-re $cmd_prompt {
if {$ifsu==1} {
send "su $su_user \r"
expect -re "assword:"
send "$su_pass\r"
} else {
send "\r"
}
}
}
#---------------------------------------------------- now,we modfiy the password
send "passwd $passuser \r";
expect {
"New UNIX password:" {
send "$newpass\r"
}
"passwd: Only root can specify a user name." {
exit
}
}
expect {
"Retype new UNIX password:" {
send "$newpass\r"
}
}
#---------------------------------------------------- now,need do a test?
if {$ifsshtest==1} {
if {$ifsu==1} {
expect -re $cmd_prompt
send "exit\r"
}
expect -re $cmd_prompt
send "exit\r"
spawn ssh
$passuser@$ipaddr
expect {
-re "assword:" {
send "$newpass\r"
} -re "Permission denied, please try again." {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}
}
expect {
-re $cmd_prompt {
send "\r"
send "\r"
puts "ssh login test OK!"
send "\r"
}
}
#--------------------------------------------------- ok,we exit
expect -re $cmd_prompt
send "\r"
exit
#interact
三,程序说明:
1,passall.sh:bash脚本,遍历ip地址的列表文件,而后分别登陆到各台机器上修改密码,
接受的参数有两个: 分别是 ip地址列表的完整路径,passone的完整路径
ip地址的列表文件:放置ip地址的列表,只须要每行一个地址便可
2,passone: expect脚本,
接受的参数只有一个,就是ip地址
其中可修改的变量的说明:
set loginuser "testuser" //登陆到服务器上的用户名
set loginpass {testpass} //登陆到服务器上的密码
set ifsu 1 //修改密码前是否需切换到有权限的用户
set su_user "suroot" //su到的用户名
set su_pass {surootpass} //su到的用户的密码
set passuser "passusername" //要修改的用户的用户名
set newpass "passuserpass" //要修改的用户的新密码
set ipaddr [lrange $argv 0 0] //接收到的参数,ip地址
set timeout 300 //超时的时间
set cmd_prompt "]#|~]?" //登陆到的服务器的命令提示符,用 | 隔开
set ifsshtest 1 //是否经过再次ssh登陆检测修改密码是否成功
四:用法举例:
/home/passuser/passall.sh /home/passuser/mod_ip_list.txt /home/passuser/passone
说明: 命令行中的passall.sh和passone请你们使用上面的代码,并作相应修改便可
mod_ip_list.txt是须要用户自建的ip列表文件,内容举例以下:
192.168.1.5
192.168.1.6
192.168.1.7
五:这个要求在运行的机器上安装有expect和tcl,你们能够用yum或apt等工具进行安装