Quick bit of Python code that I came up with this evening. I was running into an issue inserting some open-resolv-conf configuration commands to the end of my VPN providers OpenVPN configuration files. These resolv-conf settings are important in order to fix DNS leaks when using a VPN tunnel. The issue was that spaces in the file name don’t play nicely when used in a simple shell script. So I cooked up this Pythonic way to do it.

import glob

openvpnfiles = glob.glob("./*.ovpn")

for singleFile in openvpnfiles:
    with open(singleFile, "a") as file:
        file.write("script-security 2\n")
        file.write("up /etc/openvpn/update-resolv-conf\n")
        file.write("down /etc/openvpn/update-resolv-conf")