linux中expect批量修改使用者密碼的指令碼

來源:互聯網
上載者:User

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
}
 
# 機器列表資料格式:  IP  連接埠  舊密碼  新密碼
set hostfile    [ open [lindex $argv 0] ]
# 命令列表資料格式:  一條命令一行
set cmdfile    [ open [lindex $argv 1] ]
 
# 資料檔案分割符,預設為空白格
set part "\ "
 
# 過濾關鍵字
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
            }
        }
    }
    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
#設定變數
set timeout 10
set USERNAME etnet
set PASSWORD 123456
#一個迴圈,說明對哪些機器進行操作
foreach host {
192.168.151.89
192.168.151.90
} {
spawn ssh
-l root ${host}
#ssh首次登陸的驗證,exp_continue會繼續執行下一迴圈
expect {
        "no)?" {send "yes\r";exp_continue}
        "password:" {send "123456\r"}
}
#每個expect捕獲一個提示符,send發送一條命令,命令以\r結尾。
expect "]*"
send "passwd ${USERNAME}\r"
expect "password:"
send "${PASSWORD}\r"
expect "password:"
send "${PASSWORD}\r"
expect "]*"
send "exit\r"
}


 

補充:expect用法

1. [#!/usr/bin/expect]

這一行告訴作業系統指令碼裡的代碼使用那一個shell來執行。這裡的expect其實和linux下的bash、windows下的cmd是一類東西。

注意:這一行需要在指令碼的第一行。

2. [set timeout 30]

基本上認識英文的都知道這是設定逾時時間的,現在你只要記住他的計時單位是:秒   。timeout -1 為永不逾時

3. [spawn ssh -l username 192.168.1.1]

spawn是進入expect環境後才可以執行的expect內部命令,如果沒有裝expect或者直接在預設的SHELL下執行是找不到spawn命令的。所以不要用 “which spawn“之類的命令去找spawn命令。好比windows裡的dir就是一個內部命令,這個命令由shell內建,你無法找到一個dir.com 或 dir.exe 的可執行檔。

它主要的功能是給ssh運行進程加個殼,用來傳遞互動指令。

4. [expect "password:"]

這裡的expect也是expect的一個內部命令,有點暈吧,expect的shell命令和內部命令是一樣的,但不是一個功能,習慣就好了。這個命令的意思是判斷上次輸出結果裡是否包含“password:”的字串,如果有則立即返回,否則就等待一段時間後返回,這裡等待時間長度就是前面設定的30秒

5. [send "ispass\r"]

這裡就是執行互動動作,與手工輸入密碼的動作等效。

溫馨提示: 命令字串結尾別忘記加上“\r”,如果出現異常等待的狀態可以核查一下。

6. [interact]

執行完成後保持互動狀態,把控制權交給控制台,這個時候就可以手工操作了。如果沒有這一句登入完成後會退出,而不是留在遠程終端上。如果你只是登入過去執行

7.$argv 參數數組

expect指令碼可以接受從bash傳遞過來的參數.可以使用[lindex $argv n]獲得,n從0開始,分別表示第一個,第二個,第三個....參數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.