Skip to content

Instantly share code, notes, and snippets.

@Caustic
Created August 27, 2013 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Caustic/6358824 to your computer and use it in GitHub Desktop.
Save Caustic/6358824 to your computer and use it in GitHub Desktop.
vagrant bugreport
def getfqdn(name=''):
"""Get fully qualified domain name from name.
An empty argument is interpreted as meaning the local host.
First the hostname returned by gethostbyaddr() is checked, then
possibly existing aliases. In case no FQDN is available, hostname
from gethostname() is returned.
"""
name = name.strip()
if not name or name == '0.0.0.0':
name = gethostname()
try:
hostname, aliases, ipaddrs = gethostbyaddr(name)
except error:
pass
else:
aliases.insert(0, hostname)
for name in aliases:
if '.' in name:
break
else:
name = hostname
return name
ubuntu@graph:~$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 graph graph precise.example.com precise
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Last login: Tue Aug 27 13:22:48 2013 from 192.168.0.2
ubuntu@graph:~$ python
Python 2.7.3 (default, Apr 10 2013, 06:20:15)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostname()
'graph'
>>> socket.getfqdn()
'precise.cyanoptics.com'
>>> socket.gethostbyaddr('graph')
('graph', ['graph', 'precise.example.com', 'precise'], ['127.0.1.1'])
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment