Removing unsafe characters with Python and regex

While working on a program which uses data from an internal CMDB to build ssh aliases, I ran into a problem. Some of the devices have special characters in the hostname, for example:

DEBUG pre-fix: wcm-449-wu-fw-0/stby
DEBUG pre-fix: wcm-449-wu-fw-0/act

To remove the unsafe characters, the hostname variable is passed to a cleaning function each time it is set:

1
2
3
4
5
6
def fix_hostname(hostname):
    hostname = re.sub(r'[\\/:"*?<>|]+', "-", hostname)
    hostname = hostname.lower()
    return hostname

hostname = fix_hostname(device["name"])

The unsafe characters are converted to -:

DEBUG post-fix: wcm-449-wu-fw-0-stby
DEBUG post-fix: wcm-449-wu-fw-0-act

comments powered by Disqus