Chapter 6. IP and DNS

Every computer that is connected to a network needs an IP address. In Chapter 1, Network Programming and Python, an introduction to TCP/IP networking was presented. The IP address labels a machine's network interface with a numeric identifier, which also identifies the location of the machine, albeit with limited reliability. Domain Name System (DNS) is a core network service that maps the names to the IP addresses and vice-verse. In this chapter, we will mainly focus on manipulating the IP and DNS protocols with the help of Python. In addition to this, we will briefly discuss the Network Time Protocol (NTP), which helps in synchronizing the time with a centralized time server. The following topics will be discussed here:

  • Retrieving the network configuration of a local machine
  • Manipulating the IP addresses
  • The GeoIP look-ups
  • Working with DNS
  • Working with NTP

Retrieving the network configuration of a local machine

Before doing anything else, let's ask in the Python language, What's my name?. In networking terms, this is equivalent to finding out the machine's name or the host's name. On the shell command-line, this can be discovered by using the hostname command. In Python, you can do this by using the socket module.

>>> import socket
>>> socket.gethostname()
'debian6box.localdomain.loc'

Now, we would like to see the local machine IP. This can be seen by using the ifconfig command in Linux and by using the ipconfig command in the Windows OS. But, we'd like to do this in Python by using the following built-in function:

>>> socket.gethostbyname('debian6box.localdomain.loc')
'10.0.2.15'

As you can see, this is the IP of the first network interface. It can also show us the IP of the loopback interface (127.0.0.1) if your DNS or hostfile has not been configured properly. In Linux/UNIX, the following line can be added to your /etc/hosts file for obtaining the correct IP address:

10.0.2.15       debian6box.localdomain.loc      debian6box

This process is known as a host-file based name resolution. You can send a query to a DNS server and ask for the IP address of a specific host. If the name has been registered properly, then you will get a response from the server. But, before making a query to the remote server, let us first discover some more information about the network interface and the gateway machine of your network.

In every LAN, a host is configured to act as a gateway, which talks to the outside world. In order to find the network address and the netmask, we can use the Python third-party library netifaces (version > 0.10.0 ). This will pull all the relevant information. For example, you can call netifaces.gateways() for finding the gateways that are configured to the outside world. Similarly, you can enumerate the network interfaces by calling netifaces.interfaces(). If you would like to know all the IP addresses of a particular interface eth0, then you can call netifaces.ifaddresses('eth0'). The following code listing shows the way in which you can list all the gateways and IP addresses of a local machine:

#!/usr/bin/env python
import socket
import netifaces

if __name__ == '__main__':    
    # Find host info
    host_name = socket.gethostname()
    ip_address = socket.gethostbyname(host_name)
    print("Host name: {0}".format(host_name))
    
    # Get interfaces list
    ifaces = netifaces.interfaces()
    for iface in ifaces:
        ipaddrs = netifaces.ifaddresses(iface)
        if netifaces.AF_INET in ipaddrs:
            ipaddr_desc = ipaddrs[netifaces.AF_INET]
            ipaddr_desc = ipaddr_desc[0]
            print("Network interface: {0}".format(iface))
            print("	IP address: {0}".format(ipaddr_desc['addr']))
            print("	Netmask: {0}".format(ipaddr_desc['netmask']))
    # Find the gateway
    gateways = netifaces.gateways()
    print("Default gateway: {0}".format(gateways['default'][netifaces.AF_INET][0]))

If you run this code, then this will print a summary of the local network configuration, which will be similar to the following:

$ python 6_1_local_network_config.py
Host name: debian6box
Network interface: lo
  IP address: 127.0.0.1
  Netmask: 255.0.0.0
Network interface: eth0
  IP address: 10.0.2.15
  Netmask: 255.255.255.0
Default gateway: 10.0.2.2
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.88.110