Reputation: 43
I have a problem that’s been bothering me for a long time and I can’t find a solution.
I have a configuration file that I have to put in an openvpn certificate,
this certificate contains all types of characters,
at first I take the certificate and put it in a variable $OVPN :
OVPN=$(cat CRT.ovpn | sed 's/$/\\0A/' | tr -d '\n')
OVPN="openvpn_ovpn1=$OVPN"
OVPN="openvpn_ovpn1=client\0Adev tun\0Aproto udp\0Aremote test 6194\0Aresolv-retry infinite\0Anobind\0Auser nobody\0Agroup\0Apersist-tun\0Aremote-cert-tls server\0 3\0A<ca>\0A-----BEGIN CERTIFICATE-----\0AMIIDQjCCAiqgAwIBAgIUXJuwnzAUc8QCRU/Ksyot049mB2swDQYJKoZIhvcNAQEL\0ABQAwEzERMA8GA1UEAwwIQ2hhbmdlTWUwHhcNMjEwNjA0MTM1NTM2WhcNMzEwNjAy\0AMTM1NTM2WjATMREwDwYDVQQDDAhDaGFuZ2VNZTCCASIwDQYJKoZIhvcNAQEBBQAD\0AggEPADCCAQoCggEBAL3nr2mV4/uqd0LCDzQC7boiY5BUSzcrMox6Tuw9hREa6/Y5\0A4PRo5DtgLns+8tKeZ8EdkXI/YhjNMGWUkvbdCQKe+/J2uGi0VyGRHxIhW7ShlCZ/\0AOKb6ru7+UgbA328VQohbMukGxVVkZrJuG538gGQX3ZGjB52Ud662\0At8KwGZ9jref2ah9G88yEOs5BG2wzhgs9TgdwtyoykubSnT2ICHkHfG8+O97mOA1s\0A8b+L3J/NFNSAqPDf+1875rCRJf6lXnpmAGfselswYMO6ScOCJHVTAplROVMoH+b8\0AjtJ4SDztsHc6kq3kEmS/HVf7w8T2B2vM7QWqOK0CAwEAAaOBjTCBijAdBgNVHQ4E\0AFgQUD9lXXDvFuWa9l+b/HcSmsbsNwhIwTgYDVR0jBEcwRYAUD9lXXDvFuWa9l+b/\0AHcSmsbsNwhKhF6QVMBMxETAPBgNVBAMMCENoYW5nZU1lghRcm7CfMBRzxAJFT8qz\0AKi3Tj2YHazAMBgNVHRUdDwQEAwIBBjANBgkqhkiG9w0BAQsF\0AAAOCAQEAYtl5NH9yKGp94m+2RsR/rVatp5gvjwv96xKmpKUp2qoB8wKQf2fTIRpI\0AtAnivBkcXae+43Xr8UXctdvkXiglriRAhPE/lO5l/uxmFQMpRziGHHsuDxwEldi0\0ArvVL5lzK0mUc8X3Qc3QppkG1MLEg8tF9uIB/fBeFeuLxzJIDmbzUPSpeTEmgc3qr\0AIwgGd//KemPDBH9YQsHCbJMs5kfwbiv4JEDvfaydymKdlfWuJ1AZD9TXJ6pCjntY\0A5FBLYhRFZkOuAM5cOzw6cSo/I4+DuJuc0SDx95Ajxi\0AsAkFvzNuxV7yutuG1t+FCbHbzZ54hQ==\0A-----END CERTIFICATE-----\0A</ca>\0A\0A<tls-auth>\0A#\0A# \0A#\0A-----BEGIN OpenVPN Static key V1----"
after I want to replace the contents of the variable openvpn_ovpn1= in the configuration file, with the contents of the variable OVPN
I used this command :
sed -i "/openvpn_ovpn1=/c\#$OVPN" config.dat
it worked but by adding a # at the beginning of the variable (#openvpn_ovpn1=textofcertificat) this is not a problem for me, but the biggest problem is that this command removes the backslash of (\0A) which causes me a problem of operation after
i tried to use this command also :
awk -v var=$OVPN 'NR==331{print var}NR!=331' config_file > outputfile
to replace a content of the line 331, but it didn't work !
Upvotes: 1
Views: 493
Reputation: 3363
sed is stripping the backslash. You need an extra backslash.
$ var='\\0A'
$ echo hello | sed "/hello/c\\$var"
\0A
Upvotes: 1
Reputation: 58371
This might work for you (GNU sed):
cat <<! | sed -e '/openvpn_ovpn1=/{r /dev/stdin' -e 'd}' configFile
$OVPN
!
Put the variable into a temporary file and pipe it into the sed command.
On matching openvpn_ovpn1=
in the config file, read the temporary file in and then delete the original line.
Upvotes: 1