如何从 expect 中删除特殊字符?

How can I remove special characteres from expect?

所以我正在使用下一个 expect 脚本来获取交换机的 运行-config,然后将其保存到文件中:

#!/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

除此之外它可以正常工作:

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

每次打印“--More--”时都会出现在 log.txt 中。

稍后使用 bash 删除“--More--”不是问题,但如果我这样做:

grep "^H" log.txt

没有输出,所以我无法删除它,因为它不匹配。

我试图找到一种方法来尽可能不输出带有 expect 的特殊字符,但没有找到,所以我在这里问一下,以防有人知道。

使用 bash 的解决方案也对我有帮助,但首选使用 expect

您可以使用 bash tr 实用程序。来自 man 页面

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.