jogle900
jogle900

Reputation: 13

expect script capturing garbage characters - need to remove

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 "[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"

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

Upvotes: 1

Views: 1386

Answers (1)

glenn jackman
glenn jackman

Reputation: 246774

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

Upvotes: 2

Related Questions