Python Program To Get Ip, Hostname, Fqdn And Active Interface

Python Program To Get Ip, Hostname, Fqdn And Active Interface

Published on October 28 2021

In this tutorial, you will learn how to get the IP address, hostname, fully qualified domain name and all active interfaces of a local System in Python. We will also learn how to find FQDN for google website in this tutorial.

Source code

import socket
try:
    hostname = socket.gethostname() # returns hostname
    ip_address = socket.gethostbyname(hostname)  # returns IPv4 address with respect to hostname
    print('Hostname :', hostname)
    print('IP Address :', ip_address)
    fqdn = socket.getfqdn('www.google.com') # returns fully qualified domain name for name
    print('FQDN', fqdn)
    print(socket.gethostbyname_ex(hostname)) # Return a triple (hostname, aliaslist, ipaddrlist)

except:
    print('error while getting IP address or invalid hostname!')

Video

 



Next Tutorial