Shell登陸遠程伺服器

來源:互聯網
上載者:User

標籤:

  現場服務器較多,密碼3個月到期,在到期時需更改密碼。

  使用expect編寫,嘗試登陸2次後退出(防止密碼錯誤時帳號鎖定),逾時重試一次。

  shell指令碼調用並定時執行,登陸成功後執行一條命令,如:hostname、uname等,根據退出狀態判斷密碼是否到期。

  0--正常

  1--傳入參數錯誤

  2--timeout

  3--密碼錯誤或到期

  指令碼如下: 

#!/usr/bin/expect################################################################ 串連遠程主機proc do_login {passwd} {    set timeout 30    set done 1    set timeout_case 0    set ps1 {PS1="doraemon#";export PS1}    while {$done<3} {        expect {            *assword* {                send $passwd\r                incr done            }            \[$%>#] {                set done 5                send $ps1\r\r                break            }            timeout {                set done 1                switch -- $timeout_case {                    1 { send_user "try again ...\n" }                    2 { exit 2                        expect eof                    }                }                incr timeout_case            }        }    }    if {$done==3} {        exit 3        expect eof    }}################################################################ 執行命令proc exec_cmd {cmd} {    expect -re "doraemon#$"    send_user "\$cmd: $cmd\n"    send $cmd\r}################################################################ 退出proc logout {} {    expect -re "doraemon#$"    send exit\r    expect eof}###############################################################if {$argc < 4} {    send_user "Usage:$argv0 user pass ip command\n"    exit 1}set user [lindex $argv 0]set pass [lindex $argv 1]set ip [lindex $argv 2]set num 3spawn ssh -o StrictHostKeyChecking=no -l $user $ip do_login $passwhile {$num<$argc} {    set proc [lindex $argv $num]    # 捕獲輸出用    set cmd "echo ==$ip==$user==`$proc`=="    exec_cmd $cmd    incr num}           logout

 

Shell登陸遠程伺服器

相關文章

聯繫我們

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