def get_hostnames():
    '''
    Get list of hostnames using multiple strategies
    '''
    h = []
    h.append(socket.gethostname())
    h.append(socket.getfqdn())

    # try socket.getaddrinfo
    try:
        addrinfo = socket.getaddrinfo(
            socket.gethostname(), 0, socket.AF_UNSPEC, socket.SOCK_STREAM,
            socket.SOL_TCP, socket.AI_CANONNAME
        )
        for info in addrinfo:
            # info struct [family, socktype, proto, canonname, sockaddr]
            if len(info) >= 4:
                h.append(info[3])
    except socket.gaierror:
        pass

    # try /etc/hostname
    try:
        name = ''
        with salt.utils.fopen('/etc/hostname') as hfl:
            name = hfl.read()
        h.append(name)
    except (IOError, OSError):
        pass

    # try /etc/nodename (SunOS only)
    if salt.utils.is_sunos():
        try:
            name = ''
            with salt.utils.fopen('/etc/nodename') as hfl:
                name = hfl.read()
            h.append(name)
        except (IOError, OSError):
            pass

    # try /etc/hosts
    try:
        with salt.utils.fopen('/etc/hosts') as hfl:
            for line in hfl:
                names = line.split()
                try:
                    ip = names.pop(0)
                except IndexError:
                    continue
                if ip.startswith('127.') or ip == '::1':
                    for name in names:
                        h.append(name)
    except (IOError, OSError):
        pass

    # try windows hosts
    if salt.utils.is_windows():
        try:
            windir = os.getenv('WINDIR')
            with salt.utils.fopen(windir + r'\system32\drivers\etc\hosts') as hfl:
                for line in hfl:
                    # skip commented or blank lines
                    if line[0] == '#' or len(line) <= 1:
                        continue
                    # process lines looking for '127.' in first column
                    try:
                        entry = line.split()
                        if entry[0].startswith('127.'):
                            for name in entry[1:]:  # try each name in the row
                                h.append(name)
                    except IndexError:
                        pass  # could not split line (malformed entry?)
        except (IOError, OSError):
            pass

    # strip spaces and ignore empty strings
    hosts = []
    for name in h:
        name = name.strip()
        if len(name) > 0:
            hosts.append(name)

    # remove duplicates
    hosts = list(set(hosts))
    return hosts
dataset/ETHPy150Open saltstack/salt/salt/utils/network.py/get_hostnames/original