期待从文件

时间:2016-03-05 18:50:35

标签: scripting expect

我正在尝试在一堆基于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但是在第二个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 *错误,当它到达时IP并且不会继续到列表中的下一个IP。无论如何,我可以发表一个声明,说明如果“assword:”第二次出现进入下一个IP,如果“>”就像它应该继续使用脚本一样,然后在spawn命令之后添加一行,如果它没有收到第一个期望的“assword:”那么它将移动到列表中的下一个IP?

任何帮助将不胜感激。我是新手,但似乎是脚本中大规模ssh进程的一个非常好的工具。只是在调整它时不能将错误输出到1个作业而不是在出错时转移到下一个作业。

1 个答案:

答案 0 :(得分:0)

    #!/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
    }

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