expect script capturing garbage characters - need to remove

浪子不回头ぞ 提交于 2020-01-06 08:05:10

问题


I have a switch I'm trying to write to grab output from a hp switch. The source machine is an ubuntu 18.04. The code version on the switch is 16.04. I tried changing the terminal settings on the switch from the default of vty100 to ansi and none but no difference. How do I get rid of the extra special characters show below?

set timeout 20
spawn ssh -l manager 192.168.10.10
expect "manager@192.168.10.10'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"

I have this extra output of characters through each part of the script in the log file and the console. Need to remove these characters from output. ??

^[[?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

回答1:


Those are ANSI Escape sequences

You should be able to strip them out of the log file with:

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


来源:https://stackoverflow.com/questions/55050513/expect-script-capturing-garbage-characters-need-to-remove

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!