Skip to content

Instantly share code, notes, and snippets.

@sanchezcarlosjr
Last active October 18, 2022 22:30
Show Gist options
  • Save sanchezcarlosjr/8ceadc87deeaa49e4678622a102c2448 to your computer and use it in GitHub Desktop.
Save sanchezcarlosjr/8ceadc87deeaa49e4678622a102c2448 to your computer and use it in GitHub Desktop.
ip-exercises.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyNhpTacrI7mKcb5D0FQgAQG",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/sanchezcarlosjr/8ceadc87deeaa49e4678622a102c2448/ip-exercises.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"class IpClassError(Exception):\n",
" pass\n",
"def identify_ip_class(ip):\n",
" if 0 <= ip[0] <= 126:\n",
" return 'A'\n",
" if 127 <= ip[0] <= 191:\n",
" return 'B'\n",
" if 192 <= ip[0] <= 223:\n",
" return 'C'\n",
" raise IpClassError(f\"{ip} is a invalid class\")"
],
"metadata": {
"id": "b1q6rr4qzSON"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def to_bin(ip):\n",
" return [(8-len(x))*\"0\"+x for x in list(map(lambda x: bin(x)[2:], ip))]"
],
"metadata": {
"id": "Ccw2ROi3x8q9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def generate_decimal_from_list_decimal(ip):\n",
" return int(\"\".join(to_bin(ip)), 2)"
],
"metadata": {
"id": "bT6-fKNCbnlI"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def generate_ip_from_decimal(decimal):\n",
" l=bin(decimal).replace('0b', '')\n",
" l=\"0\"*(32-len(l))+l\n",
" n=8\n",
" return [int(l[i:i+n],2) for i in range(0, len(l), n)]"
],
"metadata": {
"id": "4_Up69uHcp6r"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def generate_mask_from_number_of_ones(ones):\n",
" return int(ones*\"1\"+(32-ones)*\"0\",2)"
],
"metadata": {
"id": "XpAOxG_AgJ8g"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def calculate_subred(ip, mask):\n",
" return [ip[i]&mask[i] for i in range(0, 4)]"
],
"metadata": {
"id": "3kO6njFjzcEe"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def calculate_first_host(subred):\n",
" return [subred[0], subred[1], subred[2], subred[3]+1]"
],
"metadata": {
"id": "hFFQg2_gzdvB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"dec=1\n",
"bin(dec)[2:].count(\"0\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kQR9_VJpYtMq",
"outputId": "d5b534a6-9312-4937-f181-f5f610ef0d02"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0"
]
},
"metadata": {},
"execution_count": 441
}
]
},
{
"cell_type": "code",
"source": [
"def calculate_number_of_hosts(mask):\n",
" dec = generate_decimal_from_list_decimal(mask)\n",
" n=bin(dec)[2:].count(\"0\")\n",
" print(f\"hosts=2^{n}-2={2**n-2}\")\n",
" return (dec^0xFFFFFFFF)-1"
],
"metadata": {
"id": "r101JWQMpQuB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def calculate_last_of_hosts(brodcast):\n",
" return [brodcast[0], brodcast[1], brodcast[2], brodcast[3]-1]"
],
"metadata": {
"id": "Lg8oeLK6FceT"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def calculate_brodcast(ip, mask):\n",
" return [ip[i]|255^mask[i] for i in range(0, 4)]"
],
"metadata": {
"id": "MWBMi2FeExLl"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def generate_ip(sip):\n",
" return list(map(lambda x: int(x), sip.strip().split('.')))"
],
"metadata": {
"id": "3CIS_SLY04-d"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def calculate_parameters(ip, mask):\n",
" parameters=dict()\n",
" parameters['class'] = identify_ip_class(ip)\n",
" print(f\"IP={'.'.join(to_bin(ip))}\")\n",
" print(f\"MASK={'.'.join(to_bin(mask))}\")\n",
" parameters['subred'] = calculate_subred(ip, mask)\n",
" print(f\"Subnet IP={'.'.join(to_bin(parameters['subred']))}\")\n",
" parameters['first_host'] = calculate_first_host(parameters['subred'])\n",
" parameters['number_of_hosts'] = calculate_number_of_hosts(mask)\n",
" parameters['broadcast'] = calculate_brodcast(parameters['subred'],mask)\n",
" print(f\"broadcast={'.'.join(to_bin(parameters['broadcast']))}\")\n",
" parameters['last_host'] = calculate_last_of_hosts(parameters['broadcast'])\n",
" return parameters"
],
"metadata": {
"id": "IsQqK7ccXoA9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"calculate_parameters(generate_ip(\"200.157.142.5\"), generate_ip(\"255.255.192.0\"))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mLduVg0ES4mo",
"outputId": "df61bc0e-1986-4f37-d85b-f0e49151f126"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"IP=11001000.10011101.10001110.00000101\n",
"MASK=11111111.11111111.11000000.00000000\n",
"Subnet IP=11001000.10011101.10000000.00000000\n",
"hosts=2^14-2=16382\n",
"broadcast=11001000.10011101.10111111.11111111\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'class': 'C',\n",
" 'subred': [200, 157, 128, 0],\n",
" 'first_host': [200, 157, 128, 1],\n",
" 'number_of_hosts': 16382,\n",
" 'broadcast': [200, 157, 191, 255],\n",
" 'last_host': [200, 157, 191, 254]}"
]
},
"metadata": {},
"execution_count": 447
}
]
},
{
"cell_type": "code",
"source": [
"import math\n",
"def number_zeros(x):\n",
" n=math.ceil(math.log2(x))\n",
" print(f\"2^n≥{x}⇒n=ceil(log2({x}))={n}\")\n",
" return n"
],
"metadata": {
"id": "WG_bKBU_Dju7"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def calculate_mask(hosts):\n",
" zeros=number_zeros(hosts+2)\n",
" l=(32-zeros)*\"1\"+\"0\"*zeros\n",
" n=8\n",
" return [int(l[i:i+n],2) for i in range(0, len(l), n)]"
],
"metadata": {
"id": "EjzK-3wDkdcn"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"calculate_mask(255)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-1w7inNPk3pk",
"outputId": "3773c025-87a5-48ec-ff75-bd9d6c21e18e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2^n≥257⇒n=ceil(log2(257))=9\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[255, 255, 254, 0]"
]
},
"metadata": {},
"execution_count": 450
}
]
},
{
"cell_type": "code",
"source": [
"def calculate_parameters_type_2(subred, hosts):\n",
" parameters=dict()\n",
" parameters['mask'] = calculate_mask(hosts)\n",
" print(\"Mask: \"+\".\".join(to_bin(parameters['mask'])))\n",
" parameters['subred'] = subred\n",
" parameters['class'] = identify_ip_class(subred)\n",
" parameters['first_host'] = calculate_first_host(parameters['subred'])\n",
" parameters['number_of_hosts'] = calculate_number_of_hosts(parameters['mask'])\n",
" parameters['broadcast'] = calculate_brodcast(parameters['subred'], parameters['mask'])\n",
" parameters['last_host'] = calculate_last_of_hosts(parameters['broadcast'])\n",
" print(f\"Broadcast ={'.'.join(to_bin(parameters['broadcast']))}\")\n",
" print(f\"last_host ={'.'.join(to_bin(parameters['last_host']))}\")\n",
" print(f\"first_host ={'.'.join(to_bin(parameters['first_host']))}\")\n",
" return parameters"
],
"metadata": {
"id": "S6vXtq3hlWPW"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"calculate_parameters_type_2(generate_ip(\"90.56.0.0\"), 16383)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZrOuQ765oQAY",
"outputId": "47e252fa-6c84-4531-aa1e-fa82f87f64bc"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2^n≥16385⇒n=ceil(log2(16385))=15\n",
"Mask: 11111111.11111111.10000000.00000000\n",
"hosts=2^15-2=32766\n",
"Broadcast =01011010.00111000.01111111.11111111\n",
"last_host =01011010.00111000.01111111.11111110\n",
"first_host =01011010.00111000.00000000.00000001\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'mask': [255, 255, 128, 0],\n",
" 'subred': [90, 56, 0, 0],\n",
" 'class': 'A',\n",
" 'first_host': [90, 56, 0, 1],\n",
" 'number_of_hosts': 32766,\n",
" 'broadcast': [90, 56, 127, 255],\n",
" 'last_host': [90, 56, 127, 254]}"
]
},
"metadata": {},
"execution_count": 452
}
]
},
{
"cell_type": "code",
"source": [
"calculate_parameters_type_2(generate_ip(\"191.136.48.0\"), 1025)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "EnvKXcbtcJtf",
"outputId": "945cda91-6963-422a-a63f-794560010dee"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2^n≥1027⇒n=ceil(log2(1027))=11\n",
"Mask: 11111111.11111111.11111000.00000000\n",
"hosts=2^11-2=2046\n",
"Broadcast =10111111.10001000.00110111.11111111\n",
"last_host =10111111.10001000.00110111.11111110\n",
"first_host =10111111.10001000.00110000.00000001\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'mask': [255, 255, 248, 0],\n",
" 'subred': [191, 136, 48, 0],\n",
" 'class': 'B',\n",
" 'first_host': [191, 136, 48, 1],\n",
" 'number_of_hosts': 2046,\n",
" 'broadcast': [191, 136, 55, 255],\n",
" 'last_host': [191, 136, 55, 254]}"
]
},
"metadata": {},
"execution_count": 453
}
]
},
{
"cell_type": "code",
"source": [
"def flip_bits_from_decimal(a: int):\n",
" return ~a & ((1 << a.bit_length()) - 1)"
],
"metadata": {
"id": "lakfjOEuND9M"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def to_ip_bin(dec):\n",
" return \".\".join(to_bin(generate_ip_from_decimal(dec)))"
],
"metadata": {
"id": "Hc3icfi3hXe7"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def calculate_parameters_type_3(subred, mask, subnet_mask):\n",
" parameters=dict()\n",
" result_mask=mask^subnet_mask\n",
" print(f\"MASK={to_ip_bin(mask)}\")\n",
" print(f\"Subnet Mask={to_ip_bin(subnet_mask)}\")\n",
" x=to_ip_bin(result_mask)\n",
" print(f\"Result={x}\")\n",
" print(f\"number subnets=2^count({x},1)={2**x.count('1')}\")\n",
" parameters['hosts_by_subnet'] = flip_bits_from_decimal(result_mask)+1\n",
" parameters['number_subnets'] = (result_mask>>(parameters['hosts_by_subnet'].bit_length()-1))+1\n",
" print(f\"hosts by subnet=2^count({x},0)={parameters['hosts_by_subnet']}\")\n",
" parameters['subnets'] = []\n",
" for i in range(0, parameters['number_subnets']):\n",
" parameters['subnets'].append(dict())\n",
" subnet_ip = subred+i*parameters['hosts_by_subnet']\n",
" parameters['subnets'][i]['mask'] = bin(subnet_mask).count(\"1\")\n",
" parameters['subnets'][i]['subnet_ip'] = generate_ip_from_decimal(subnet_ip)\n",
" parameters['subnets'][i]['first_host_ip'] = generate_ip_from_decimal(subnet_ip+1)\n",
" parameters['subnets'][i]['last_host_ip'] = generate_ip_from_decimal(subnet_ip+parameters['hosts_by_subnet']-2)\n",
" parameters['subnets'][i]['broadcast'] = generate_ip_from_decimal(subnet_ip+parameters['hosts_by_subnet']-1)\n",
" return parameters"
],
"metadata": {
"id": "hKVo-WrupPgm"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"calculate_parameters_type_3(\n",
" subred=generate_decimal_from_list_decimal([220,120,90,0]),\n",
" mask=generate_mask_from_number_of_ones(23),\n",
" subnet_mask=generate_decimal_from_list_decimal([255,255,255,192])\n",
")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rA-Nkj0R4s9Y",
"outputId": "1a252c13-47a2-4900-b305-f28fedc49beb"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"MASK=11111111.11111111.11111110.00000000\n",
"Subnet Mask=11111111.11111111.11111111.11000000\n",
"Result=00000000.00000000.00000001.11000000\n",
"number subnets=2^count(00000000.00000000.00000001.11000000,1)=8\n",
"hosts by subnet=2^count(00000000.00000000.00000001.11000000,0)=64\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'hosts_by_subnet': 64,\n",
" 'number_subnets': 8,\n",
" 'subnets': [{'mask': 26,\n",
" 'subnet_ip': [220, 120, 90, 0],\n",
" 'first_host_ip': [220, 120, 90, 1],\n",
" 'last_host_ip': [220, 120, 90, 62],\n",
" 'broadcast': [220, 120, 90, 63]},\n",
" {'mask': 26,\n",
" 'subnet_ip': [220, 120, 90, 64],\n",
" 'first_host_ip': [220, 120, 90, 65],\n",
" 'last_host_ip': [220, 120, 90, 126],\n",
" 'broadcast': [220, 120, 90, 127]},\n",
" {'mask': 26,\n",
" 'subnet_ip': [220, 120, 90, 128],\n",
" 'first_host_ip': [220, 120, 90, 129],\n",
" 'last_host_ip': [220, 120, 90, 190],\n",
" 'broadcast': [220, 120, 90, 191]},\n",
" {'mask': 26,\n",
" 'subnet_ip': [220, 120, 90, 192],\n",
" 'first_host_ip': [220, 120, 90, 193],\n",
" 'last_host_ip': [220, 120, 90, 254],\n",
" 'broadcast': [220, 120, 90, 255]},\n",
" {'mask': 26,\n",
" 'subnet_ip': [220, 120, 91, 0],\n",
" 'first_host_ip': [220, 120, 91, 1],\n",
" 'last_host_ip': [220, 120, 91, 62],\n",
" 'broadcast': [220, 120, 91, 63]},\n",
" {'mask': 26,\n",
" 'subnet_ip': [220, 120, 91, 64],\n",
" 'first_host_ip': [220, 120, 91, 65],\n",
" 'last_host_ip': [220, 120, 91, 126],\n",
" 'broadcast': [220, 120, 91, 127]},\n",
" {'mask': 26,\n",
" 'subnet_ip': [220, 120, 91, 128],\n",
" 'first_host_ip': [220, 120, 91, 129],\n",
" 'last_host_ip': [220, 120, 91, 190],\n",
" 'broadcast': [220, 120, 91, 191]},\n",
" {'mask': 26,\n",
" 'subnet_ip': [220, 120, 91, 192],\n",
" 'first_host_ip': [220, 120, 91, 193],\n",
" 'last_host_ip': [220, 120, 91, 254],\n",
" 'broadcast': [220, 120, 91, 255]}]}"
]
},
"metadata": {},
"execution_count": 457
}
]
},
{
"cell_type": "code",
"source": [
"256/64"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CgiuaVmJScDO",
"outputId": "425b1a9d-86c2-467e-e49a-26ad1264f01f"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"4.0"
]
},
"metadata": {},
"execution_count": 458
}
]
},
{
"cell_type": "code",
"source": [
"to_bin(generate_ip_from_decimal(192))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WsNZq-LkV5hk",
"outputId": "efc2e2f1-7d6e-415e-d8e8-e4659de05b1e"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['00000000', '00000000', '00000000', '11000000']"
]
},
"metadata": {},
"execution_count": 459
}
]
},
{
"cell_type": "code",
"source": [
"generate_ip_from_decimal(generate_mask_from_number_of_ones(22))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sTPT31mKjFu3",
"outputId": "262d6e17-6caa-4040-be52-a912c496bba3"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[255, 255, 252, 0]"
]
},
"metadata": {},
"execution_count": 460
}
]
},
{
"cell_type": "code",
"source": [
"calculate_parameters(\n",
" ip=[192,168,23,0],\n",
" mask=generate_ip_from_decimal(generate_mask_from_number_of_ones(22))\n",
")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PsHZRFfIqggo",
"outputId": "5a2f4901-94cf-4b2e-b5cf-a361b02f72fe"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"IP=11000000.10101000.00010111.00000000\n",
"MASK=11111111.11111111.11111100.00000000\n",
"Subnet IP=11000000.10101000.00010100.00000000\n",
"hosts=2^10-2=1022\n",
"broadcast=11000000.10101000.00010111.11111111\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'class': 'C',\n",
" 'subred': [192, 168, 20, 0],\n",
" 'first_host': [192, 168, 20, 1],\n",
" 'number_of_hosts': 1022,\n",
" 'broadcast': [192, 168, 23, 255],\n",
" 'last_host': [192, 168, 23, 254]}"
]
},
"metadata": {},
"execution_count": 461
}
]
},
{
"cell_type": "code",
"source": [
"192|(255^255)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gO0XnnCxrDG-",
"outputId": "371cde00-5eea-46ec-b469-b2cd48cedb65"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"192"
]
},
"metadata": {},
"execution_count": 465
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "TwUfsFe-vZi8"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment