Assignment06.py
user_9560538
python
a year ago
4.5 kB
14
Indexable
# CSCI 355 Web Technology
#Summer 2024
#Assignment 6: "Network Addressing and Forwarding"
#ACK - Class
from subprocess import check_output
# [1] Write a function execute_command(cmd) to execute a windows shell command ("cmd").
def exec_cmd(cmd):
return check_output(cmd, shell=True)
# [2] Define a function get_routing_table by using the function from the previous step to run "route print" and then parsing the output into a table (2-D list).
def get_routing_table(routing_data):
s = routing_data.decode()
s = s[s.find("Destination"): s.find("Persistent Routes") - 1].replace("=", "").strip()
lines = s.split('\n')
headers = [get(lines[0], 0, 17), get(lines[0], 18, 35), get(lines[0], 36, 49), get(lines[0], 50, 60), get(lines[0], 61, 70)]
print(headers)
data = [[get(x, 0, 17), get(x, 18, 35), get(x, 36, 50), get(x, 51, 68), int(get(x, 69, 80))] for x in lines[1:]]
for row in data:
print(row)
return headers, data
def get(s, i, j):
return s[i:j+1].strip()
# [3] Prompt the user to enter an IPv4 address in dotted-decimal notation. If the IP address is 0 or empty, this is our cue to quit. Otherwise proceed to next step
def get_ip():
ip = input("Enter IP Address in dotted-decimal notation: ") or "123.123.123.123"
return ip
# [4] Define a function validate_address() to validate the address in two ways:
def validate_address(ip,verbose=False):
octets = ip.split(".")
if len(octets) != 4:
print("Does not have four octets")
return False
for octet in octets:
if not octet.isnumeric():
print("Has a non-numeric octet: ", ip)
return False
if int(octet) < 0 or int(octet) > 255:
print("Octet is out of 0-255: ", ip)
return False
return True
# [5] Define a function get_binary_address() to find the binary equivalent of an IP address in dotted decimal notation and use it on the inputted IP address.
def get_binary_address(ip_addr):
binary = "".join([bin(int(octet))[2:].zfill(8) for octet in ip_addr.split('.')])
return binary
# [6] Define a function bitwise_and() to do the “bitwise-AND” of two bit-strings.
def bitwise_and(bits1, bits2):
res = ""
n = min(len(bits1), len(bits2))
for i in range(n):
res += "1" if bits1[i] == "1" and bits2[i] == "1" else "0"
return res
# [7] Define a function get_classful_address_type() to determine its class A thru E.
def get_class(binary_address):
if binary_address.startswith('0'):
return "A"
elif binary_address.startswith('10'):
return "B"
elif binary_address.startswith('110'):
return "C"
elif binary_address.startswith('1110'):
return "D"
elif binary_address.startswith('1111'):
return "E"
else:
return "Unknown"
def find_prefix_match(binary_and):
return binary_and.find("0")
# [8] Define a function get_next_hop() to loop through the rows of the routing table from step [2] and determine the “Next Hop” for the user-inputted address.
def get_next_hop(table,dest_ip):
dest_ip_bin = get_binary_address(dest_ip)
best_row = None
best_prefix = -1
for row in table:
table_dest = row[0]
if validate_address(table_dest):
table_dest_bin = get_binary_address(table_dest)
binary_and = bitwise_and(dest_ip_bin, table_dest_bin)
prefix = find_prefix_match(binary_and)
if (prefix > best_prefix) or (prefix == best_prefix and row[4] < best_row[4]):
best_prefix = prefix
best_row = row
return best_row,best_prefix
def process_ip(ip,table):
print()
print(f'IP: {ip}')
ip_status = validate_address(ip, verbose=True)
print(f'IP state is: {ip_status}')
if ip_status:
ip_binary = get_binary_address(ip)
best_row, best_prefix = get_next_hop(table,ip)
print(f'Binary is: {ip_binary}')
print(f'Best row is: {best_row}' )
print(f'Best prefix is: {best_prefix }' )
def main():
routing_data = exec_cmd("route print")
headers, data = get_routing_table(routing_data)
ips_to_test = ["abc.def.ghi.jkl", "111-111-111-111", "613.613.613.613", "123.123.123", "225.225.225.225", "241.242.243.244", "127.0.0.1", "52.3.73.91", "216.239.63.255"]
for ip in ips_to_test:
process_ip(ip, data)
if __name__ == '__main__':
main()
Editor is loading...
Leave a Comment