1

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
pynexj
  • 19,215
  • 5
  • 38
  • 56
jogle900
  • 13
  • 5

1 Answers1

2

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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 1
    Great. That did it! When the session ends, I get these leftover characters but those must fall outside the log. Must be seomthing to do with the original ANSI character issue. They are not in the log, just at the prompt. ??? " admin@server1:~$ ;120R" – jogle900 Mar 07 '19 at 20:37
  • @jogle900 same here, it's definitely not ideal and I want to get rid of them – nmz787 Mar 31 '22 at 22:44