期望脚本和旧网络设备输出ANSI转义字符

时间:2019-03-12 16:10:09

标签: bash expect

下午

我整天用Google搜索,甚至尝试用python解决其他问题,但没有成功。

我有一些老式的网络设备,当您通过SSH连接到它们时,它们似乎具有一种奇怪的终端类型,即CTRL + h是退格键!

这导致我的期望脚本出现问题,而不是需要做的三件事:1)登录2)升级特权(认为启用cisco)3)保存配置

试图设置终端类型,但不确定vt100是否正确,我怀疑是这样。我还添加了sleep命令,看看是否可以解决延迟,而不是掷骰子。

到目前为止,这是我的脚本

#!/usr/bin/expect
set ::env(TERM) vt100
## Get username
send_user "Username: \n"
expect_user -re "(.*)\n" { set user $expect_out(1,string) }

## Get pass
stty -echo
send_user "Password: \n"
expect_user -re "(.*)\n" { set pass $expect_out(1,string) }
stty echo

## Get list of hosts
set f [ open "hosts.txt"]
set hosts [ split [read $f] "\n"]
set hosts [ lreplace $hosts end end ]
close $f

## iterate host
foreach host $hosts {
        spawn ssh "$user\@$host"
        expect {
                "continue connecting" { send "yes\r"; exp_continue }
                "assword" { send "$pass\r" }
        }

        expect "Copyright (c)" {
                sleep 1
                send "\r"
                sleep 1
                send "en 14\r"
                sleep 1
                send "$pass\r"
                sleep 1
                send "config save\r"
                sleep 1
                send "exit\r"
        }
}

脚本输出

deanmoore@laptop% ./zyxel
Username:
dean.moore
Password:
spawn ssh dean.moore@host1
dean.moore@host1's password:
Copyright (c) 1994 - 2013 ZyXEL Communications Corp.
host1> ^[[47;223R%                                                                                                                                                                                                     
deanmoore@laptop% 7;223R

0 个答案:

没有答案