简体   繁体   English

如何使用Expect Script滚动终端输出?

[英]How can I scroll the terminal output with Expect Script?

I'm trying to come up with a simple expect script that allows me to login to a system and run a single command ... something like this: 我正在尝试提出一个简单的Expect脚本,该脚本允许我登录到系统并运行单个命令……类似这样:

The problem I have is that the output from a single show command overflows, the display screen is followed by the --More-- prompt. 我的问题是单个show命令的输出溢出,显示屏上显示--More--提示符。 At the --More-- prompt, you have these options: 在--More--提示符下,您具有以下选项:

  • Press Ctrl+C, q, or Q to interrupt the output and return to the command prompt. 按Ctrl + C,q或Q中断输出并返回命令提示符。

  • Press the spacebar to display an additional screen of output. 按空格键以显示其他输出屏幕。

  • Press Enter to display one more line of output. 按Enter显示另一行输出。

What I want at the end is to record all the output from the show command but my script is timing out at the first --More-- prompt. 最后,我想要记录show命令的所有输出,但是我的脚本在第一个--More--提示符下超时。 How can I make the script to send new line characters (ie spacebar ) everytime it gets the --More-- prompt? 如何使脚本在每次收到--More--提示时发送换行符(即空格键 )?

Thanks in advance, 提前致谢,

!/usr/bin/expect !/ usr / bin /期望

spawn telnet 192.168.0.1 生成telnet 192.168.0.1
expect "Username:" 期望“用户名:”
send "MyUsername\\r" 发送“ MyUsername \\ r”
expect "assword:" 期望“密码:”
send "MyPassword\\r" 发送“ MyPassword \\ r”
send "show ip int br\\r" 发送“ show ip int br \\ r”
interact timeout 5 互动超时5
send -- "exit \\r" 发送-“退出\\ r”

If you're dealing with a Cisco terminal (as it appears you are), you can set the terminal length to zero (by sending "term len 0"), as 0 means "no pausing". 如果您正在使用Cisco终端(如您所愿),则可以将终端长度设置为零(通过发送“ term len 0”),因为0表示“无暂停”。 That's the best approach to avoid the expect script from having to do "extra work" in dealing with paginated output. 这是避免期望脚本在处理分页输出时不得不做“额外工作”的最佳方法。

Otherwise you can do something like this: 否则,您可以执行以下操作:

 expect {
   -ex "--More--" { send -- " "; exp_continue }
   "*#" { send "exit\r" }
 }

The "-ex" is to avoid expect from thinking "--More--" is a flag since it starts with a hyphen. “ -ex”是为了避免期望“ --More--”是一个标志,因为它以连字符开头。 On match, you send a space to scroll another screen, and continue expecting until you get back to a prompt (such as the exec prompt), and exit. 匹配时,您发送一个空格来滚动另一个屏幕,并继续期待,直到返回到提示(例如exec提示),然后退出。

我使用了终端更多关闭选项,该选项禁用了more功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM