Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash and Expect: Is there a way to ignore or remove ANSI control sequences from Expect buffer?

Tags:

linux

bash

expect

I'm using Expect to connect to my server over a (virtual) serial port. (HP iLo, to be specific)

When booting from a Linux OS ISO image, you eventually get to the 'boot:' prompt. When my server reaches that prompt, I would like to enter my own custom boot options and press enter. Easy, right?

This is how the boot prompt looks when you're watching my Expect script execute (looks normal):

boot:

However, I have not been able to match 'boot:'. Looking at the Expect Buffer in my logfile, this is what is being captured for that line:

ESC\[25;01HbbESC\[25;01HESC\[25;02HooESC\[25;02HESC\[25;03HooESC\[25;03HESC\[25;04HttESC\[25;04HESC\[25;05H::ESC\[25;05HESC\[25;06H  ESC\[25;06HESC\[25;07H"

I think all those control sequences are screwing up my match. If you look closely 'boot:' is actually in there, but it's surrounded by what I believe are ANSI control sequences.

In fact, the logfile is absolutely full of ANSI control characters.

Relevant pieces of the Expect script I've been playing around with:

bash #] expect -d -c '
    .....
    # SSH to the Virtual Serial Port Management server
    spawn ssh [email protected]
    .....
    # Access the Virtual Serial Port for the server being booted
    send "vsp\r"
    .....
    # After rebooting the server, when the boot: prompt appears, enter boot options
    expect {
        "boot:" {send $bootOptions \r\n"}
        timeout {send_user "Never found boot prompt\n"; send_user "$expect_out(buffer)"; exit 1}
    }
    .....
    exit'

Any ideas about what the best way to handle those control characters would be? I've tried exporting TERM=dumb and TERM=vt1000 on the machine I'm running the script on. Didn't make much of a difference.

like image 394
user3311719 Avatar asked Oct 30 '25 22:10

user3311719


1 Answers

Not sure if this will help, but you could create a wrapper for ssh and exec that instead of ssh and then have

ssh <host> | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b

perhaps take out the col -b which filters newlines if you don't need that.

like image 68
Goblinhack Avatar answered Nov 01 '25 17:11

Goblinhack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!