Expect脚本不会在文件的最后一个输入上执行

时间:2013-10-10 22:57:53

标签: linux expect

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

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 }

我从未改变过这个选项,所以我不知道如何解释所有这些。它看起来很简单,但有些东西让我知之甚少。

0 个答案:

没有答案