bash - 期望脚本和旧网络设备输出 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
解决方案
推荐阅读
- opengl - 在OpenGL中,为什么我们在世界空间中默认位于(0,0,0)?
- phppgadmin - phpPgAdmin - 登录失败 - 虚拟类 - 无法实例化
- javascript - 在javascript中计算放大的图像分辨率
- python - 在 Python 中更改 Plotly 散点图(气泡图)的图例标记大小
- spring-tool-suite - Spring Tool Suite - 项目和外部依赖项 - 对 jar 进行排序?
- android - 在android studio中获取服务类下的触摸数据
- python - 无法加载库 libcudnn_cnn_train.so.8
- python - 如何编写列表理解,包括 If Else 条件
- c++ - C++ 静态函数/命名空间问题
- python - 当我有一个带有 continue 语句的函数时,即使它只在 while 循环中使用,它也会出错