0

我有一个期望脚本,我用它来一次性在大量新服务器上设置我的密码。它从作为参数提供的输入文件中读取。它从一个文件中读取需要更改的预设密码和从另一个文件中读取需要设置的密码。它实际上会逐步执行不同的评估并根据遇到的情况发送输入。

set timeout 45
set prompt {\$ $}
set file1 [open [lindex $argv 0] r]
set pw1 [exec cat /home/user/bin/.pw1.txt]
set pw2 [exec cat /home/user/bin/.pw2.txt]

while {[gets $file1 host] != -1} {
    puts $host
    spawn -noecho ssh -q $host
    expect {
        -re $prompt {
            send -- exit\r
            expect eof
        }
        "current" {
            send -- $pw2\r
            expect "New password"
            send -- $pw1\r
            expect "Retype new password"
            send -- $pw1\r
            expect eof
        }
        "continue connecting" {
            send "yes\r"
            expect {
                "current" {
                    send -- $pw2\r
                    expect "New password"
                    send -- $pw1\r
                    expect "Retype new password"
                    send -- $pw1\r
                    expect eof
                }
                -re $prompt {
                    send -- exit\r
                    expect eof
                }
            }
        }
    }
}

puts \r

我遇到的问题是参数文件中的最后一个主机没有更新。将显示输入新密码的提示。推测然后发送现有密码并且随后将新密码发送到随后的两个提示。但是,如果我在运行脚本后尝试登录,则实际上并未设置密码。这只发生在列表中的最后一个主机上。

我错过了什么?

编辑:脚本输出:

opensuse:bin:6476 $ ./host_check.exp hosts_files/cust_host.txt 
SERVER1
You are required to change your password immediately (root enforced)
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user user.
Changing password for user.
(current) UNIX password: 
New password: 
Retype new password: SERVER2
You are required to change your password immediately (root enforced)
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user user.
Changing password for user.
(current) UNIX password: 
New password: 
Retype new password: SERVER3
You are required to change your password immediately (root enforced)
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user user.
Changing password for user.
(current) UNIX password: 
New password: 
Retype new password: 
opensuse:bin:6477 $ 

编辑:带有“ext_internal 1”的脚本输出(已清理)

opensuse:bin:6534 $ ./host_check.exp hosts_files/cust_host.txt 
SERVER1
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {16084}
Gate keeper glob pattern for '\$ $' is '\$ '. Activating booster.

expect: does "" (spawn_id exp5) match regular expression "\$ $"? Gate "\$ "? gate=no
"current"? no
"continue connecting"? no
You are required to change your password immediately (root enforced)
Last login: Thu Nov  7 18:55:24 2013 from 10.152.84.124

expect: does "You are required to change your password immediately (root enforced)\r\nLast login: Thu Nov  7 18:55:24 2013 from 10.152.84.124\r\r\n" (spawn_id exp5) match regular expression "\$ $"? Gate "\$ "? gate=no
"current"? no
"continue connecting"? no
WARNING: Your password has expired.
You must change your password now and login again!

expect: does "You are required to change your password immediately (root enforced)\r\nLast login: Thu Nov  7 18:55:24 2013 from 10.152.84.124\r\r\nWARNING: Your password has expired.\r\nYou must change your password now and login again!\r\n" (spawn_id exp5) match regular expression "\$ $"? Gate "\$ "? gate=no
"current"? no
"continue connecting"? no
Changing password for user user.

expect: does "You are required to change your password immediately (root enforced)\r\nLast login: Thu Nov  7 18:55:24 2013 from 10.152.84.124\r\r\nWARNING: Your password has expired.\r\nYou must change your password now and login again!\r\nChanging password for user user.\r\n" (spawn_id exp5) match regular expression "\$ $"? Gate "\$ "? gate=no
"current"? no
"continue connecting"? no
Changing password for user.
(current) UNIX password: 
expect: does "You are required to change your password immediately (root enforced)\r\nLast login: Thu Nov  7 18:55:24 2013 from 10.152.84.124\r\r\nWARNING: Your password has expired.\r\nYou must change your password now and login again!\r\nChanging password for user user.\r\nChanging password for msnyder.\r\n(current) UNIX password: " (spawn_id exp5) match regular expression "\$ $"? Gate "\$ "? gate=no
"current"? yes
expect: set expect_out(0,string) "current"
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "You are required to change your password immediately (root enforced)\r\nLast login: Thu Nov  7 18:55:24 2013 from 10.152.84.124\r\r\nWARNING: Your password has expired.\r\nYou must change your password now and login again!\r\nChanging password for user user.\r\nChanging password for msnyder.\r\n(current"
send: sending "OldPASS\r" to { exp5 }

expect: does ") UNIX password: " (spawn_id exp5) match glob pattern "New password"? no

New password: 
expect: does ") UNIX password: \r\nNew password: " (spawn_id exp5) match glob pattern "New password"? yes
expect: set expect_out(0,string) "New password"
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) ") UNIX password: \r\nNew password"
send: sending "NewPASS" to { exp5 }

expect: does ": " (spawn_id exp5) match glob pattern "Retype new password"? no


expect: does ": \r\n" (spawn_id exp5) match glob pattern "Retype new password"? no
Retype new password: 
expect: does ": \r\nRetype new password: " (spawn_id exp5) match glob pattern "Retype new password"? yes
expect: set expect_out(0,string) "Retype new password"
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) ": \r\nRetype new password"
send: sending "NewPASS" to { exp5 }

我从来没有打开过这个选项,所以我不知道如何解释这一切。它看起来很简单,但有些地方超出了我的知识范围。

4

0 回答 0