Digital Academic Solutions

Python script for hosts file processing: 32547 UNIX Systems Programming, Spring 2020

SOLUTION AVAILABLE AT: http://libraay.com/downloads/python-script-for-hosts-file-processing-32547-unix-systems-programming-spring-2020/

Problem:

In this assignment, you will write a Python program which parses a text file that associates IPv4 addresses and hostnames, and outputs the requested information. Purely for reference, the file is similar in format to the actual Unix file /etc/hosts.
These are the specifications for your Python program:
1. It must be named hosts.py

2. It should be invoked as:
python hosts.py option hosts_file
The program must check that argument hosts_file exists, is a file and is readable. If not, it must print an error message to the standard output and exit. The specifications for the hosts_file and option arguments are given below.

3. File hosts_file can have any arbitrary name. It must be a file of text with the following format:
a. The file consists of an arbitrary number of lines (including, possibly, zero lines).
b. Each line must contain three fields separated by one or more space characters.
c. The three fields are: IPv4 address, hostname, alias.
d. The IPv4 address field is a string of characters in the well-known IPv4 address dotdecimal format (four integers, each between 0 and 255, separated by dots; example:
103.246.98.253).
e. The hostname field is a string of characters that can include letters, digits, hyphens (also known as minus signs) and dots. Its length can vary from a minimum of 4 to a maximum of 253 characters.
f. The alias field is a string of the same type of characters, varying in length from a minimum of 1 character to an unlimited (yet sensible) number.
Fundamental note: your program is not expected to verify that file hosts_file complies with the above specifications. It will only be tested with compliant files.
The following example should be regarded as the reference specification for the format of file hosts_file:

127.0.0.1 localhost.localdomain sahara
192.168.1.10 foo.mydomain.edu foo
209.237.226.90 www.opensource.org picco
192.168.1.13 bar.mydomain.com bar
146.82.138.7 master.debian.org master

4. Your program can be invoked with option: -a. In this case, it must print all the hostnames in the order in which they appear in file hosts_file, in this format: Hostnames: … Example with file hosts_file given above: Command line: python hosts.py -a hosts_file Output: Hostnames: localhost.localdomain foo.mydomain.edu www.opensource.org bar.mydomain.com master.debian.org In the case in which file hosts_file is empty, your program must instead only print: No hosts

5. Your program can be invoked with option: -d domain. 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 instance, in the file hosts_file given above, strings localdomain, edu, org and com are all top-level domains. Note that there is no dot in the top-level domain. In this case, your program must print 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. This is an example with file hosts_file given above: Command line: python hosts.py -d org hosts_file Output: 209.237.226.90 www.opensource.org picco 146.82.138.7 master.debian.org master In the case in which no line of file hosts_file matches the domain argument (including the case where the file is empty), your program must print: No hosts in the given domain

6. Your program can be invoked with option: -c class. The class argument is a string of one character that can only take values A, B or C (please note: only uppercase). (Note: purely for reference, this option is inspired by a superseded feature of IP addresses
known as classful network design.)
In the case in which the value is A, your program must print 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. This is an example with file hosts_file given above:
Command line:
python hosts.py -c A hosts_file
Output:
127.0.0.1 localhost.localdomain sahara
In the case in which the value is B, your program must print 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. This is an example with file hosts_file given above:
Command line:
python hosts.py -c B hosts_file
Output:
146.82.138.7 master.debian.org master
In the case in which the value is C, your program must print 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. This is an example with file hosts_file given above:
Command line:
python hosts.py -c C hosts_file
Output:
192.168.1.10 foo.mydomain.edu foo
209.237.226.90 www.opensource.org picco
192.168.1.13 bar.mydomain.com bar
In the case in which no line of file hosts_file matches the class argument (including the case where the file is empty), your program must print: No hosts in the given class

7. Your program can be invoked with option: -v. In this case, it must only print your name, surname, student ID and date of completion of your assignment, in a format of your choice. Please note that argument hosts_file is still required.

8. No options can be used simultaneously. This means that your program can only be invoked with one of the options at a time.

9. If your program is invoked with any other syntax than those specified above, it must print a message of your choice to the standard output and exit.
Examples of incorrect syntax:
python hosts.py -Z hosts_file
python hosts.py -a
python hosts.py -c D

SOLUTION AVAILABLE AT: http://libraay.com/downloads/python-script-for-hosts-file-processing-32547-unix-systems-programming-spring-2020/

Leave a Reply

Your email address will not be published. Required fields are marked *



Solutions Authored by 15+ Years Experienced Industry Expert