Background: I'm using expect to connect between AMCE.sh and a managed network switch. I can connect to the switch via SSH, log in and issue commands but because of the way the switch manages SSL certificates I have to generate a new CSR every time. And the CSR is displayed on the terminal when you run the command, not as a file that can be downloaded. I need to save the CSR as a file so I can then feed it into ACME.sh.
Actual question:
I have the following in an expect script, the intention is to capture the output when the switch spits out the CSR into the file $csr
and stops capturing when the ending line comes in.
send -- "command to generate CSR"send -- "\r"log_file -noappend $csrexpect -- "-----END CERTIFICATE REQUEST-----"log_file
This runs fine and I get a file, the issue is I get a bunch of extra garbage from the terminal (truncated example below, there's usually about a KB of extra garbage). I've tried feeding this through sedz or
grep` but they don't seem to be able to handle the multi-line nature of what I want to extract from this.
^[[68;33Hc^[[68;33H^[[?25h^[~~~~~~*SNIP*~~~~~~[[?25h^[[68;1H^[[1;68r^[[68;1H-----BEGIN CERTIFICATE REQUEST-----^MMIICoDCCAYgCAQAwWzEfMB0GA1UEAxMWc3dpdGNoMS5ob21lLmdvc3NldC51azENM^MAsGA1UECxMEaG9tZTEPMA0GA1UECh~~~~~~*SNIP*~~~~~~3lsRknVNg/ZAATDaRC^MKR3EIZFS3izUZ/+3wYzni84QZcp6s6HmDaB2moRRarVThXnbcF0a5nvGQtU8j7jP1^MJ/RPoWPRQa7vsfnEgN+gjQj6EYHeD/uMKzYPWeKEyQ4VPmw7K/VLySbMg==^M-----END CERTIFICATE REQUEST-----^M^[[1;68r^[[68;1H^[[68;1H^[[2K^[[68;1H^[[?25h^[[68;1H^[[68;1Hswitch1.home.domain.uk(config)# ^[[68;1H^[[68;33H^[[68;1H^[[?25h^[[68;33H
If I run cat test.csr
I get most of the file out cleanly but when I try to extract what I need using sed
I can't get it to match what I need. Anxample of what I've tried sed -E 's/([\s\S]*)(-----BEGIN CERTIFICATE REQUEST-----[\s\S]*-----END CERTIFICATE REQUEST-----)([\s\S]*)/\2/m;t;d' test.csr
[68;33Hc {end of command to generate CSR}-----BEGIN CERTIFICATE REQUEST-----MIICoDCCAYgCAQAwWzEfMB0GA1UEAxMWc3dpdGNoMS5ob21lLmdvc3NldC51azENMAsGA1UECxMEaG9tZTE~~~~~~*SNIP*~~~~~~moRRarVThXnbcF0a5nvGQtU8j7jP1J/RPoWPRQa7vsfnEgN+gjQj6EYHeD/uMKzYPWeKEyQ4VPmw7K/VLySbMg==-----END CERTIFICATE REQUEST-----
So, long story short, how can I either get expect to give me clean output of just what I need, or how can I get sed
or another tool to clean it up after the fact?