首页 > 解决方案 > 期望脚本和旧网络设备输出 ANSI 转义字符

问题描述

下午,

我已经用谷歌搜索了一整天,甚至尝试了 python 中的其他解决方案,但没有任何成功。

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

这导致我的期望脚本出现问题,而不是需要做 3 件事,1)登录 2)提升权限(想想 cisco enable)3)保存配置

试图设置终端类型但不确定 vt100 是否正确,我怀疑它是正确的。我还添加了睡眠命令,看看延迟是否可以解决它,没有骰子。

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

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

标签: bashexpect

解决方案


推荐阅读