expect實現批量修改linux密碼指令碼分享_linux shell

來源:互聯網
上載者:User

最近對linux批量執行的指令碼高度興趣,在網上到處找有關expect批量執行指令碼,今天就給大家共用一個批量修改密碼的指令碼.

指令碼內容:


複製代碼 代碼如下:

#!/usr/bin/expect
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.