【问题标题】:Expect Scripting Issue Reading From File预期从文件读取的脚本问题
【发布时间】:2016-03-06 07:21:30
【问题描述】:

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

#!/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,但在第二个时发送此错误:

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 访问设备,它会出错,并出现相同的 spawn id exp* not open 当它到达那个IP 并且不会继续到列表中的下一个 IP。无论如何我可以声明如果“assword:”第二次出现以移动到下一个 IP 并且如果“>”出现,就像它应该继续使用脚本一样,然后在 spawn 命令之后添加一行如果没有收到第一个期望“assword:”,将移动到列表中的下一个 IP?

任何帮助将不胜感激。我是新手,但似乎是脚本中大规模 ssh 进程的一个非常好的工具。只是在调整它以使其不会在一项工作中出错而不是在出错时转移到下一项工作时遇到了麻烦。

【问题讨论】:

    标签: scripting expect


    【解决方案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
        }
    

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

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 2011-09-28
      • 1970-01-01
      • 2013-11-20
      • 2016-02-03
      • 2013-08-29
      • 2015-10-19
      • 1970-01-01
      相关资源
      最近更新 更多