Linux Expect脚本不会在文件的最后一次输入时执行

Linux Expect脚本不会在文件的最后一次输入时执行,linux,expect,Linux,Expect,我有一个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/b

我有一个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”(已清理)的脚本输出


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

输入文件的最后一行是否缺少换行符?可能。我马上去看看。我将不得不收集另一个服务器列表,我需要在这些服务器上运行此程序,以检查这是否解决了问题。最后回到这个问题,@glennjackman。我已经确认,当输入文件的最后一行之后有或没有换行符时,问题都存在。我想知道这是否与第二次输入新密码后没有正确发送换行有关。我已经用输出的内容更新了这个问题,让大家了解我的意思。文件中的最后一行是否是某个特定主机的特定想法?尝试交换最后一行和最后第二行。另外,减少代码重复。“继续连接”块可以看起来像
{send“yes\r;exp\u continue}
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 }