如何一次性修改多臺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等工具進行安裝