Um amigo meu escreveu um script para executar essa função.
#/usr/bin/perl
use strict;
use warnings;
my $inputFile = "sample.conf";
my $outputFile = "sample.conf.modified";
my $searchString = "Header set Public-Key-Pins";
my $updateString = " Header set Public-Key-Pins ";
my $numberOfPins = $#ARGV + 1;
if ($numberOfPins < 1) {
die "Error: Minimum 1 command line argument required";
}
my $pinCounter = 1;
my $onePin;
foreach my $argNum (0 .. $#ARGV) {
$onePin = $ARGV[$argNum];
if ($pinCounter == "1") {
$updateString = $updateString . "\"";
}
$updateString = $updateString . "pin-sha256=\\"$onePin\\"";
if ($pinCounter < $numberOfPins) {
$updateString = $updateString . "; ";
}
else {
$updateString = $updateString . "; max-age=000000; includeSubDomains\"";
}
++$pinCounter;
}
open INFILE, $inputFile or die "Can't read from $inputFile!\n";
my $numberOfMatches = 0;
my @lines;
while (my $line = <INFILE>) {
if (index($line, $searchString) != -1) {
push @lines, "$updateString\n";
++$numberOfMatches;
} else {
push @lines, $line;
}
}
close INFILE;
open OUTFILE, '>', $outputFile or die "Can't write to $outputFile!\n";
print OUTFILE @lines;
close OUTFILE;
if ($numberOfMatches != "1") {
die "Error: expected 1 match but found: $numberOfMatches matches\n";
}