sysfiend
sysfiend

Reputation: 601

How can I remove special characteres from expect?

So I'm using the next expect script in order to get the running-config of a switch and then save it to a file:

#!/usr/bin/expect

set timeout 9
set username [lindex "foo"]
set password [lindex "bar"]
set hostname [lindex "1.2.3.4"]

spawn ssh -q $username@$hostname

expect {
  timeout { send_user "\nTimeout!\n"; exit 1 }
  "*assword"
}

send "$password\n"

expect {
  timeout { send_user "\nWrong password\n" ; exit 1 }
  "switch-00>"
}

send "enable\n"

expect {
  "switch-00#"
}

send "show running-config\n"
log_file -noappend log.txt

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

send "exit\n"

close

It works as it should except for this:

--More--^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H

which is appearing in log.txt every time "--More--" gets printed.

It's not an issue to remove "--More--" later on using bash but if I do:

grep "^H" log.txt

there's no output, so I cannot remove it as it doesn't match.

I was trying to find a way to not output special characteres with expect if possible but didn't find any, so I'm asking here in case anyone knows.

A solution using bash would help me aswell but using expect is prefered.

Upvotes: 1

Views: 704

Answers (1)

Inian
Inian

Reputation: 85560

You could use the bash tr utility. From the man page

NAME
tr -- translate characters

DESCRIPTION
The tr utility copies the standard input to the standard output with sub-
situation or deletion of selected characters.

SYNOPSIS
tr [-Ccsu] string1 string2
tr [-Ccu] -d string1

-C     Complement the set of characters in string1, that is ``-C ab''
       includes every character except for `a' and `b'.

-c     Same as -C but complement the set of values in string1.

-d     Delete characters in string1 from the input.

To Strip out non-printable characters from file1.

tr -cd "[:print:]\n" < file1   # This is all you need.

Upvotes: 1

Related Questions