期待脚本捕获垃圾字符 - 需要删除

问题描述 投票:0回答:1

我有一个开关,我试图写一个来自hp开关的输出。源机器是ubuntu 18.04。交换机上的代码版本是16.04。我尝试将交换机上的终端设置从默认的vty100更改为ansi,但没有区别。如何摆脱下面显示的额外特殊字符?

set timeout 20
spawn ssh -l manager 192.168.10.10
expect "[email protected]'s password:"
send "admin\n"
expect "Press any key to continue"
send "j\n"
log_file -a hp.log
send "show vlan 10\n"
expect "labswitch#"
send "conf\n"
send "hostname newswitch\n"
expect "newswitch#"
send "exit\n"
expect "newswitch#"
send "logout\n"

我通过日志文件和控制台中脚本的每个部分获得了额外的字符输出。需要从输出中删除这些字符。 ??

^[[?6l^[[1;30r^[[?7h^[[2J^[[1;1H^[[1920;1920H^[[6n^[[1;1HYour previous successful login (as manager) was on 2019-03-07 $ from 192.168.10.4
^[[1;30r^[[30;1H^[[30;1H^[[2K^[[30;1H^[[?25h^[[30;1H^[[30;1labswitch# ^[[30;1H^[[30;11H^[[30;1H^[[?25h^[[30;11H^[[1;0H^$ Status and Counters - VLAN Information - VLAN 10

  VLAN ID : 10
  Name : Server_Lab
  Status : Port-based
  Voice : No
  Jumbo : Yes
  Private VLAN : none
  Associated Primary VID : none
  Associated Secondary VIDs : none

  Port Information Mode     Unknown VLAN Status
  ---------------- -------- ------------ ----------
  11               Tagged   Learn        Up
  12               Tagged   Learn        Up


^[[1;30r^[[30;1H^[[30;1H^[[2K^[[30;1H^[[?25h^[[30;1H^[[30;1H
expect
1个回答
1
投票

那些是ANSI Escape sequences

您应该能够将它们从日志文件中剥离出来:

send "logout\r"    ;# idiomatically, use \r to "hit enter"
log_file           ;# turn off logging

# read the log file
set fid [open hp.log r]
set contents [read -nonewline $fid]
close $fid

# remove the escape sequences
regsub -all {\033\[(?:\?|\d+;)?\d+[[:alpha:]]} $contents {} stripped

# write the new contents
set fid [open hp.log w]
puts $fid $stripped
close $fid
© www.soinside.com 2019 - 2024. All rights reserved.