Artificial intelligent assistant

How to export all certificates in a certificate chain to separate .crt files with a single command I would like to export all certificates in a certificate chain to separate `.crt` files with a single command. How can I do that? To provide some background information: 1. I would like to use the `openssl` bash utility: `(openssl s_client -showcerts -connect <host>:<port> & sleep 4)` 2. the above command **may** print more than one certificate, that is, it may print more than one string with the following pattern: `-----BEGIN CERTIFICATE-----` X.509 certificate encoded in base64 `-----END CERTIFICATE-----`. For example: -----BEGIN CERTIFICATE----- MIIFNzCCAx+gAwIBAgITUwAAAAJpqCKn3YTQ6gAAAAAAAjANBgkqhkiG9w0BAQsF... -----END CERTIFICATE----- 3. the contents of the `.crt` files should be exactly the printed base64 encoded certificates, including tags.

It turns out `awk` can be used to solve the problem:

`(openssl s_client -showcerts -connect : & sleep 4) | awk '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/{if(/-----BEGIN CERTIFICATE-----/){a++}; out="/tmp/"a".crt"; print > out}'`

Replace `` and `` with actual values. The `sleep` command is there to limit the timeout of the `openssl` command.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy d15a158b1699b60d353c33f4359f1535