Created
November 9, 2022 18:02
-
-
Save Tr-reny/997eb55a4e82bc76823bd82480f7ae12 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "736d6fdd", | |
"metadata": {}, | |
"source": [ | |
"## CS 553 Lab 2\n", | |
"### Name: XXX\n", | |
"\n", | |
"This week you were assigned chapters 5 - 8 in our textbook. That's a lot of reading but the important topic of those chapters is the characteristics of analog and digital signals and how to fundamentally transmit those signals over various data links. To set the context of the data link layer, it's useful to consider a model of a network: specifically looking at a network as a combination of layers. The highest layer represents the software application (like a Web browser) and the lowest layer represents a physical communication medium (like a copper wire or an Radio Frequency signal). The two popular network models are the OSI Model and the TCP/IP model. The OSI Model specifies seven layers that data passes through when transmitted or received. Here's one on-line video that helps explain it: [link](https://www.youtube.com/watch?v=jlp8HL_iIqo) Feel free to do your own Web surfing to find other information.\n", | |
"\n", | |
"### OSI Model (Open Systems Interconnect)\n", | |
"The seven layers of the OSI Model are listed in this table. For each layer, the table also identifies one or more protocols that are used at that layer. <mark>In the column 'My Notes', identify each protocol (write the formal name of the protocol):</mark>\n", | |
"\n", | |
"| Layer | Protocol(s) | My Notes |\n", | |
"|-------|-------------|-----------|\n", | |
"| Application | HTTP | |\n", | |
"| | FTP | |\n", | |
"| Presentation | SSL | |\n", | |
"| | SSH | |\n", | |
"| Session | Sockets | (N/A) |\n", | |
"| Transport | TCP | |\n", | |
"| | UDP | |\n", | |
"| Network | IP | |\n", | |
"| | ICMP | |\n", | |
"| Data Link | HDLC | |\n", | |
"| Physical | Coax |(N/A) |\n", | |
"| | Fiber| (N/A) |\n", | |
"| | Wireless | (N/A) |\n", | |
"\n", | |
"### The TCP/IP Model\n", | |
"The set of TCP/IP protocols were established before the OSI Model and have become the 'standard' implementation model for common networks. The different TCP/IP protocols don't exactly map to the seven layers of the OSI Model. Figure 2.3 in our text illustrates the four layers of the TCP/IP model.\n", | |
"\n", | |
"### Explore Network Configuration\n", | |
"On your Rivier Virtual Machine (Windows) you can look at a summary of the machine's network support using the Windows command IPCONFIG. <mark>Run the following Python code and capture the output in this notebook.</mark>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "b3dd4cac", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"import platform\n", | |
"#\n", | |
"# Test whether we're on Windows or a Linux-like (or MacOS-like) machine:\n", | |
"if platform.system().lower() == \"windows\":\n", | |
" c = 'IPCONFIG /all'\n", | |
"else :\n", | |
" c = 'ifconfig'\n", | |
"\n", | |
"# Run the command 'c':\n", | |
"result = os.popen(c).read()\n", | |
"print(result)\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "905e88be", | |
"metadata": {}, | |
"source": [ | |
"Now we're going to use Python to look at the same network configuration data. To get the rest of the Python code in this lab to work, you will need to follow these instructions:\n", | |
"1. Start->Command Prompt (for Windows, or open a new Terminal window for MacOS, or open a new shell window for Linux)\n", | |
"1. Type the following commands:\n", | |
"```\n", | |
" pip install dnspython\n", | |
" pip install netifaces\n", | |
" ```\n", | |
"Then, from your Jupyter notebook window, restart the Python kernel.\n", | |
"\n", | |
"First, we're going to display the IP address of the current machine. Note that there is a \"reserved\" IP address for any local machine. <mark>Run the following code and capture the output.</mark>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "029a3899", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# We need one of the Python libraries for this:\n", | |
"import socket\n", | |
"\n", | |
"try:\n", | |
" # Use the reserved name 'localhost:'\n", | |
" machine_IP = socket.gethostbyname('localhost')\n", | |
" # Display results\n", | |
" print('IP: ', machine_IP)\n", | |
"except:\n", | |
" print('Uh-oh, there was an error!') \n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "79c0a1ed", | |
"metadata": {}, | |
"source": [ | |
"But we can use another Python library to examine the actual IP address of your local machine (as opposed to the \"reserved\" IP address). <mark>Run the following code and capture the output.</mark>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "a83b7b98", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import netifaces\n", | |
"import os\n", | |
"import platform\n", | |
"\n", | |
"# On Windows, we'll need to look at Registry: \n", | |
"try:\n", | |
" import winreg as wr\n", | |
"except ImportError:\n", | |
" pass\n", | |
"\n", | |
"# Here is a function we'll use to look up a Windows Registry key and return \n", | |
"# a meaningful name:\n", | |
"def translate_long_names(iface_guids):\n", | |
" iface_names = ['(unknown)' for i in range(len(iface_guids))]\n", | |
" reg = wr.ConnectRegistry(None, wr.HKEY_LOCAL_MACHINE)\n", | |
" reg_key = wr.OpenKey(reg, r'SYSTEM\\CurrentControlSet\\Control\\Network\\{4d36e972-e325-11ce-bfc1-08002be10318}')\n", | |
" for i in range(len(iface_guids)):\n", | |
" try:\n", | |
" reg_subkey = wr.OpenKey(reg_key, iface_guids[i] + r'\\Connection')\n", | |
" iface_names[i] = wr.QueryValueEx(reg_subkey, 'Name')[0]\n", | |
" except FileNotFoundError:\n", | |
" pass\n", | |
" return iface_names\n", | |
"\n", | |
"# This returns long confusing names that are actually Windows Registry keys (on the Rivier VM)\n", | |
"x = netifaces.interfaces()\n", | |
"print(x)\n", | |
"\n", | |
"# We should only have to translate these on Windows:\n", | |
"if platform.system().lower() == \"windows\":\n", | |
" print('Translated:\\n')\n", | |
" print(translate_long_names(x))\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "56e7678d", | |
"metadata": {}, | |
"source": [ | |
"That output should have displayed each network interface that is available on your machine. Go back to the output of the IPCONFIG command and see if you can identify the interface or interfaces of interest. Then, use the long name of that interface (including the curly braces if there are any!) in the following code and capture the output." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "be8425cb", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import netifaces\n", | |
"# Enter the interface here (in place of the 'xxx')\n", | |
"i = 'xxx'\n", | |
"addrs = netifaces.ifaddresses(i)\n", | |
"\n", | |
"# Now the variable 'addrs' stores the link-layer addresses as well as the IP layer\n", | |
"# addresses for that interface. You can examine each of those here:\n", | |
"print('Link layer: ', addrs[netifaces.AF_LINK])\n", | |
"print('IP layer: ', addrs[netifaces.AF_INET])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "03a7b761", | |
"metadata": {}, | |
"source": [ | |
"Examine the output from the previous code execution. <mark>In this markdown cell, explain why the IP address is a unique \"dotted\" number but the 'broadcast' address has the value '255' in it:</mark>\n", | |
"*(Write your answer here)*" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "13751e6e", | |
"metadata": {}, | |
"source": [ | |
"Finally, let's look at the IP Address. Chapters 1 and 2 in our text introduced the concept and we'll read more details about the IP Address in Chapter 14. For now, let's use some Python code to examine valid and invalid IP Addresses. Use the following list of potential IP addresses:\n", | |
"- 127.0.0.1\n", | |
"- 192.168.2.1\n", | |
"- 0.0.0.0\n", | |
"- 255.255.256.1\n", | |
"- 10.10.10.0\n", | |
"- 143.33.15.256\n", | |
"\n", | |
"Run the following Python code, substituting each of these potential IP addresses in the code. Then, update the list above by writing 'valid' or 'invalid' and, for the 'invalid' addresses explain why." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "2c139987", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import ipaddress\n", | |
"\n", | |
"# We'll define a function so you can just call the function in a \n", | |
"# separate Jupyter cell\n", | |
"def verifyAddress(testip):\n", | |
" retVal = \"Valid address\"\n", | |
" try :\n", | |
" ip = ipaddress.ip_address(testip)\n", | |
" except ValueError:\n", | |
" retVal = \"Invalid address\"\n", | |
" return retVal" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "734e2250", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Here's an example:\n", | |
"verifyAddress('1.2.3.4')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "19e0bd03", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.11.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment