使用expect前,我們是需要先安裝了,否則expect是不可用的,下面一起來給各位總結(jié)兩個expect批量修改用戶密碼腳本.
1、使用expect前,需要先安裝兩個rpm包,代碼如下:
- # rpm -ihv expect-5.43.0-8.el5.i386.rpm
- # rpm -ihv expect-devel-5.43.0-8.el5.i386.rpm
2、批量修改密碼的腳本,代碼如下:
- #!/usr/bin/expect
- #yemaosheng.com
- if { $argc<2 } {
- send_user "usage: $argv0 <host file> <cmd file> \n"
- exit
- }
- # 機(jī)器列表數(shù)據(jù)格式: IP 端口 舊密碼 新密碼
- set hostfile [ open [lindex $argv 0] ]
- # 命令列表數(shù)據(jù)格式: 一條命令一行
- set cmdfile [ open [lindex $argv 1] ]
- # 數(shù)據(jù)文件分割符,默認(rèn)為空格
- set part "\ "
- # 過濾關(guān)鍵字
- set key_password "password:\ "
- set key_init "\(yes/no\)\?\ "
- set key_confirm "'yes'\ or\ 'no':\ "
- set key_ps "*]#\ "
- set key_newpassword "UNIX password:\ "
- set timeout 30
- log_file ./exprct.log
- match_max 20480
- while {[gets $hostfile _hosts_] >= 0} {
- set hosts [string trim $_hosts_]
- set str_index [string first $part $hosts]
- set host [string trim [string range $hosts 0 $str_index]]
- set temp [string trim [string range $hosts [expr $str_index + 1] [string length $hosts]]]
- set str_index [string first $part $temp]
- if { $str_index == -1 } {
- set port 22
- set pass $temp
- set newpass $temp
- } else {
- set port [string trim [string range $temp 0 $str_index]]
- set temp_pass [string trim [string range $temp [expr $str_index + 1] [string length $temp]]]
- set str_index [string first $part $temp_pass]
- set pass [string trim [string range $temp_pass 0 $str_index]]
- set newpass [string trim [string range $temp_pass [expr $str_index + 1] [string length $temp_pass]]]
- }
- spawn ssh -p $port $host
- while {1} {
- expect {
- "$key_password" {
- send "$pass\r"
- }
- "$key_init" {
- send "yes\r"
- }
- "$key_confirm" {
- send "yes\r"
- }
- "$key_ps" {
- while {[gets $cmdfile cmd] >= 0} {
- send "$cmd\r"
- expect {
- "$key_ps" {
- continue
- }
- "$key_newpassword" {
- send "$newpass\r"
- expect "$key_newpassword" {
- send "$newpass\r"
- expect "$key_ps"
- continue
- }
- }
- }
- }
- seek $cmdfile 0 start
- send_user "\r"
- break
- }
- timeout {
- puts "$host timeout\n"
- break //Vevb.com
- }
- }
- }
- send "exit\r"
- close
- wait
- }
- close $hostfile
- close $cmdfile
- exit
3、批量修改密碼的腳本.,用whereis expect確定expect位置,代碼如下:
- [root@rac1 ~]# whereis expect
- expect: /usr/bin/expect
- #!/usr/bin/expect
- #設(shè)置變量
- set timeout 10
- set USERNAME etnet
- set PASSWORD 123456
- #一個循環(huán),說明對哪些機(jī)器進(jìn)行操作
- foreach host {
- 192.168.151.89
- 192.168.151.90
- } {
- spawn ssh
- -l root ${host}
- #ssh首次登陸的驗證,exp_continue會繼續(xù)執(zhí)行下一循環(huán)
- expect {
- "no)?" {send "yes\r";exp_continue}
- "password:" {send "123456\r"}
- }
- #每個expect捕獲一個提示符,send發(fā)送一條命令,命令以\r結(jié)尾。
- expect "]*"
- send "passwd ${USERNAME}\r"
- expect "password:"
- send "${PASSWORD}\r"
- expect "password:"
- send "${PASSWORD}\r"
- expect "]*"
- send "exit\r"
- }
補(bǔ)充:expect用法:
1.[#!/usr/bin/expect]
這一行告訴操作系統(tǒng)腳本里的代碼使用那一個shell來執(zhí)行,這里的expect其實和linux下的bash、windows下的cmd是一類東西.
注意:這一行需要在腳本的第一行。
2.[set timeout 30]
基本上認(rèn)識英文的都知道這是設(shè)置超時時間的,現(xiàn)在你只要記住他的計時單位是:秒 ,timeout -1 為永不超時.
3.[spawn ssh -l username 192.168.1.1]
spawn是進(jìn)入expect環(huán)境后才可以執(zhí)行的expect內(nèi)部命令,如果沒有裝expect或者直接在默認(rèn)的SHELL下執(zhí)行是找不到spawn命令的,所以不要用 “which spawn“之類的命令去找spawn命令,好比windows里的dir就是一個內(nèi)部命令,這個命令由shell自帶,你無法找到一個dir.com 或 dir.exe 的可執(zhí)行文件.
它主要的功能是給ssh運行進(jìn)程加個殼,用來傳遞交互指令.
4.[expect "password:"]
這里的expect也是expect的一個內(nèi)部命令,有點暈吧,expect的shell命令和內(nèi)部命令是一樣的,但不是一個功能,習(xí)慣就好了,這個命令的意思是判斷上次輸出結(jié)果里是否包含“password:”的字符串,如果有則立即返回,否則就等待一段時間后返回,這里等待時長就是前面設(shè)置的30秒.
5.[send "ispass\r"]
這里就是執(zhí)行交互動作,與手工輸入密碼的動作等效.
溫馨提示:命令字符串結(jié)尾別忘記加上“\r”,如果出現(xiàn)異常等待的狀態(tài)可以核查一下.
6.[interact]
執(zhí)行完成后保持交互狀態(tài),把控制權(quán)交給控制臺,這個時候就可以手工操作了,如果沒有這一句登錄完成后會退出,而不是留在遠(yuǎn)程終端上,如果你只是登錄過去執(zhí)行.
7.$argv 參數(shù)數(shù)組
expect腳本可以接受從bash傳遞過來的參數(shù),可以使用[lindex $argv n]獲得,n從0開始,分別表示第一個,第二個,第三個....參數(shù).
新聞熱點
疑難解答
圖片精選