预期从文件读取脚本问题

预期从文件读取脚本问题

我正在尝试更改一组基于 Linux 的路由器上的用户的密码,这些路由器从列表中提取 IP。iplist.txt 只是一个 IP 列表(每行一个)。这是我尝试使用的代码:

    #!/usr/bin/expect
    set timeout 20
    #Edit for User
    set user username
    #Edit for Old Password
    set old oldpassword
    #Edit for New Password
    set new newpassword
    #get IP List from iplist.txt
    set f [open "/iplist.txt"]
    set hosts [split [read $f] "\n"]
    close $f

    foreach host $hosts {
            spawn -noecho ssh -q -o "StrictHostKeyChecking=no" $user@$host
            expect "assword:"
            send "$old\r"
            expect ">"
            send "user set $user password=$new\r"
            expect ">"
            send "quit\r"
            expect eof
            close
    }

这对于列表中的第一个 IP 有效,但对于第二个 IP 则发送此错误:

    spawn_id: spawn id exp4 not open

我按照我想要的方式让它工作:

#!/usr/bin/expect
set timeout 30
#Edit for User
set user user
#Edit for Old Password
set old oldpassword
#Edit for New Password
set new newpassword
#get IP List from iplist.txt
set f [open "/iplist.txt"]
set data [read $f]
close $f

foreach line [split $data \n] {
        if {$line eq {}} continue
        spawn -noecho ssh -q -o "StrictHostKeyChecking=no" $user@$line
        expect "assword:"
        send "$old\r"
        expect ">"
        send "user set $user password=$new\r"
        expect ">"
        send "\r"
        expect ">"
        send "quit\r"
        send "\r"
        expect eof
}

我遇到的下一个问题是,如果设备没有旧密码,或者 Linux 机器无法通过 ssh 访问设备,则当它到达该 IP 时,会出错,并显示相同的 spawn id exp* not open,并且不会继续访问列表中的下一个 IP。我能否写一条语句,说明如果第二次出现“assword:”则转到下一个 IP,如果出现“>”,则继续执行脚本,然后在 spawn 命令后添加一行,如果它没有收到第一个 expect“assword:”,则转到列表中的下一个 IP?

任何帮助都将不胜感激。我对 expect 还不熟悉,但它似乎是一款非常好的工具,可用于在脚本中处理大量 ssh 进程。只是在调整它以使其不会在一项作业上出错而不是在出错时转到下一项作业时遇到了麻烦。

答案1

#!/usr/bin/expect
set timeout 30
#Edit for User
set user user
#Edit for Old Password
set old oldpassword
#Edit for New Password
set new newpassword
#get IP List from iplist.txt
set f [open "/iplist.txt"]
set data [read $f]
close $f

foreach line [split $data \n] {
        if {$line eq {}} continue
        spawn -noecho ssh -q -o "StrictHostKeyChecking=no" $user@$line
        expect {
                "assword:" {
                        send "$old\r"
                        expect {
                                "assword:" {
                                                close
                                                continue
                                                }}
                        expect {
                                "*" {
                                        send "user set $user password=$new\r"
                                        expect ">"
                                        send "quit\r"
                                        close
                                        continue
                                        }}}}
        expect {
                "*" {
                        close
                        continue
                        }}
        expect eof
}

脚本可能有点脏,但确实有效。现在,如果我能弄清楚如何导出成功、错误密码和超时日志,我就能知道是否有任何错误。

相关内容