0

I'm trying to script the deployment of a postfix server, and creating DNS records accordingly. SPF ; DMARC and DKIM. The first two are pretty simple, but i'm struggling to extract the record from the file opendkim generated.

This is the file that opendkim gives me :

mail._domainkey IN  TXT ( "v=DKIM1; h=sha256; k=rsa; t=y; "
      "p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDK9yGy7orNIceonobdyTxr0USLo9XlWoo2/hg5MU5Ix+7bKFN0exJIUEeNLDAOYXWZe/0vQZan3+vnry9v3pVxqwpNp/92/xbp0pILJBzc1i5YXFe60XAlBBWq+Y9UAY2uXXsiFY4IUmhGZdMCubuHguWy/R2HDmCwrtN5vn0XfQIDAQAB" )  ; ----- DKIM key mail for localhost

I would like an output suitable for dns records like

"v=DKIM1; h=sha256; k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDK9yGy7orNIceonobdyTxr0USLo9XlWoo2/hg5MU5Ix+7bKFN0exJIUEeNLDAOYXWZe/0vQZan3+vnry9v3pVxqwpNp/92/xbp0pILJBzc1i5YXFe60XAlBBWq+Y9UAY2uXXsiFY4IUmhGZdMCubuHguWy/R2HDmCwrtN5vn0XfQIDAQAB"

I've tried several things, but i can't even extract what's between parenthesis with the commands i've found here : grep: regex only for match anything between parenthesis

It looks like there's some line return problems or i don't know

Thanks for reading, sorry for bad english ! Have a nice day

schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57
KayZnn
  • 1

2 Answers2

0

Using GNU awk, you could use FPAT to split each record into the key-value pairs, loop over the fields and append them to a string. In the END section, print the string plus the opening and closing ".

awk -v FPAT='.=[^;"]+(; )?' '{
    for (i=1;i<=NF;i++) s=s $i
}
END {
    print "\"" s "\""
}' file
Freddy
  • 25,172
  • 1
  • 21
  • 60
0

Assuming GNU grep, I would pick out the quoted strings and then join them together

grep -oP '".*?"' file | tr -d '\n' | sed 's/" *"//g'; echo

Result from your example (shortened for brevity)

"v=DKIM1; h=sha256; k=rsa; t=y; p=MIGfMA0GCSqG…QAB"

Alternatively you could use perl,

perl -e '
    $_ = join(" ", (map {chomp; $_} <>));    # Read and join lines
    s/^.*?(".*").*$/$1/;                     # Strip leading and trailing text outside quoted strings
    s/"\s*"/ /g;                             # Merge adjacent quoted strings
    s/\s\s+/ /g;                             # Reduce multiple spaces to one
    print                                    # Output the result
' file
roaima
  • 107,089
  • 14
  • 139
  • 261