我有一个类似于下面的文件(filename whatever.com)的输出......
[...]~ # tmsh list sys file ssl-cert whatever.com_2024
sys file ssl-cert whatever.com_2024 {
certificate-key-size 2048
checksum SHA1:2520:ab40df7776dbbccb62345560511f2205d
create-time 2024-08-12:19:34:07
created-by x.y
expiration-date 1754956799
expiration-string "Aug 11 23:59:59 2025 GMT"
fingerprint SHA256/D8:57:8E:8E:4A:1A:C3:3C:1B:6F:32:59:A7:36:66:92:6C
issuer "CN=DigiCert Global G2 TLS RSA SHA256 2020 CA1,O=DigiCert Inc,C=US"
key-type rsa-public
last-update-time 2024-08-12:19:34:07
mode 33188
revision 1
serial-number 0a:1d:ca:c7:09:7b:49:59:b2
size 2520
source-path /var/run/key_mgmt/RvGubB/ssl.crt/whatever.com_2024
subject "CN=whatever.com,O=XYZ,L=Toronto,ST=Ontario,C=CA"
subject-alternative-name "DNS:whatever.com"
updated-by x.y
version 3
我正在尝试根据第 17 行(以主题开头)上面的信息生成命令输出
cat whatever.com | awk 'BEGIN {F=" "; FS = "\n"; RS = ""; OFS = "\n"} { print "openssl req -new -nodes -sha256 -newkey rsa:2048 -out "substr($1,RSTART+19,length($1)-25)"_2025.csr -keyout "substr($1,RSTART+19,length($1)-25)"_2025.key -subj "/"substr($17,RSTART+14,length($17)-14)"\""}'
输出看起来不错
openssl req -new -nodes -sha256 -newkey rsa:2048 -out whatever.com_2025.csr -keyout whatever.com_2025.key -subj "/CN=whatever.com,O=XYZ,L=Toronto,ST=Ontario,C=CA"
但是它使用第 1 行的子字符串,该子字符串可能与第 17 行不同,所以我想获取文件名,而不是使用 substr($1,RSTART+19,length($1)-24) 根据“CN=”和“,O=”之间的字符串形式 $17 生成。
如果第 18 行包含字符串“DNS”,那么是否可以将输出修改为如下所示...
openssl req -new -nodes -sha256 -newkey rsa:2048 -out whatever.com_2025.csr -keyout whatever.com_2025.key -subj "/CN=whatever.com,O=XYZ,L=Toronto,ST=Ontario,C=CA" -addext "subjectAltName = DNS:whatever.com"
感谢大家的快速回答。文件名必须从文件中动态生成。包含的示例只是较大输出的一小部分。BEGIN 配置用于创建这些部分,此时可以按行单独处理。
处理 F5 负载平衡器。它们基于 Linux,但并不完全像普通服务器那样构建,这使得一些脚本编写具有挑战性。
我找到了使用 split() 的解决方案,但现在我遇到了另一个问题。当我单独运行该命令时,它似乎有效
~ # cat whatever.com | awk 'BEGIN {F=" "; FS = "\n"; RS = ""; OFS = "\n"} {split($17,a,/,/); print substr(a[1],RSTART+17)}'
whatever.com
然而,当添加到原始脚本中时,它会在前面添加数字“5”。
~ # cat whatever.com | awk 'BEGIN {F=" "; FS = "\n"; RS = ""; OFS = "\n"} { print "openssl req -new -nodes -sha256 -newkey rsa:2048 -out "split($17,a,/,/); print substr(a[1],RSTART+17)"_2025.csr -keyout "split($17,a,/,/); print substr(a[1],RSTART+17)"_2025.key -subj \"/"substr($17,RSTART+14,length($17)-14)"\""}'
openssl req -new -nodes -sha256 -newkey rsa:2048 -out 5
whatever.com_2025.csr -keyout 5
whatever.com_2025.key -subj "/CN=whatever.com,O=XYZ,L=Toronto,ST=Ontario,C=CA"