Hosts File Processing

A python script which parses a text file that associates IPv4 addresses and hostnames, and outputs the requested information. The script takes in various options and a host file of a specific format.

0
0
Python

Hosts-File-Processing

Tools Used - Python and Terminal

Python Terminal



The script is invoked as:

python hosts.py option hosts_file

This works on a host file of the given format,

alt text

The script can be invoked with the following options:

-a

In this case, it prints all the hostnames in the order in which they appear in file hosts file.

-d

In this case the domain argument is a string that represents the top-level domain of a hostname (the string after the right-most dot in the hostname). For example, if in the sample hosts file, strings localdomain, edu, org and com are all top-level domains. The script in this case prints all the lines of file hosts file where the top-level domain of the hostname matches the given domain argument, in the order in which they appear in the file.

-c

Here the class argument is a string of one character that can only take values A, B or C (please note: only uppercase).

Example code: python hosts.py -c A hosts_file

In the case in which the value is A, the script prints all the lines of file hosts file where the number before the first dot in the IPv4 address field is between 0 and 127, included, in the order in which they appear in the file.

In the case in which the value is B, the script prints all the lines of file hosts file where the number before the first dot in the IPv4 address field is between 128 and 191, included, in the order in which they appear in the file.

In the case in which the value is C, the script prints all the lines of file hosts file where the number before the first dot in the IPv4 address field is between 192 and 255, included, in the order in which they appear in the file.

Demo

alt text