3

I am looking for a Python solution for obtaining the current machine default domain name, one that will work on Linux, Mac OS X and Windows.

So far I discovered that:

  • Linux has dnsdomainname which returns it
sorin
  • 161,544
  • 178
  • 535
  • 806

2 Answers2

4

Try:

#!/usr/bin/env python

import socket

def get_dns_domain():
    return socket.getfqdn().split('.', 1)[1]

print (get_dns_domain()) # to match python>2.7
w_bufffet
  • 35
  • 5
rasebo
  • 957
  • 1
  • 13
  • 21
1

Try:

from socket import gethostname
machine_name=gethostname()
print(machine_name)
Nikita
  • 6,101
  • 2
  • 26
  • 44