{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5a3f09e7-6178-4c0d-b9e0-379357e894b3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "remove_cell",
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "my_notebook_text = \"\"\"\n",
    "# Homework 4: Ubuntu network config and ipcalc.\n",
    "\n",
    "Due Date: Tuesday, September 29, 2026\n",
    "\n",
    "1. Log into your Virtual Desktop (VD).\n",
    "2. Create a working directory named `HW_4`:\n",
    "   ```bash\n",
    "   mkdir HW_4\n",
    "   cd HW_4\n",
    "   ```\n",
    "3. Inside the `HW_4` directory, create a file named `README`.\n",
    "4. Document all commands used to complete the following exercises inside your `README` file.\n",
    "\n",
    "\n",
    "## Ubuntu network configuration.\n",
    "\n",
    "- Modify the network settings of `netplan` VM to change the IP address to 192.168.122.192. Post the updated yaml file in the README.\n",
    "- Modify the network settings of `netplan` VM to configure them on DHCP. Post the updated yaml file in the README.\n",
    "\n",
    "\n",
    "## Using ipcalc for network calculations.\n",
    "\n",
    "- Complete exercise 4.17 to have ```ipcalc``` installed on your VD.\n",
    "- We have the following IP addresses and netmasks:\n",
    "        170.15.25.34/27\n",
    "        170.15.25.192/27\n",
    "        170.15.25.100/27\n",
    "        138.10.10.1/25\n",
    "        138.10.10.15/25\n",
    "        138.10.10.250/25\n",
    "    A) How many subnets do we have?\n",
    "    B) Which of the IP addresses belong to the same subnet?\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d6361d6c-6e26-4a3d-8a09-87f40151852d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "remove_cell",
     "remove-cell"
    ]
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Homework 4: Ubuntu Network Configuration and `ipcalc`\n",
      "\n",
      "**Due Date:** Tuesday, September 29, 2026\n",
      "\n",
      "1. Log into your Virtual Desktop (VD).\n",
      "2. Create a working directory named `HW_4`:\n",
      "   ```bash\n",
      "   mkdir HW_4\n",
      "   cd HW_4\n",
      "   ```\n",
      "3. Inside the `HW_4` directory, create a file named `README`.\n",
      "4. Document all commands used to complete the following exercises inside your `README` file.\n",
      "\n",
      "## Ubuntu Network Configuration\n",
      "\n",
      "- Modify the network settings of the `netplan` Virtual Machine (VM) to change the static IP address to `192.168.122.192`. Post the updated YAML configuration file contents in your `README`.\n",
      "- Modify the network settings of the `netplan` VM to configure them for Dynamic Host Configuration Protocol (DHCP). Post the updated YAML configuration file contents in your `README`.\n",
      "\n",
      "## Using `ipcalc` for Network Calculations\n",
      "\n",
      "- Complete exercise 4.17 to ensure `ipcalc` is installed on your Virtual Desktop.\n",
      "- Given the following IP addresses and netmasks:\n",
      "  - `170.15.25.34/27`\n",
      "  - `170.15.25.192/27`\n",
      "  - `170.15.25.100/27`\n",
      "  - `138.10.10.1/25`\n",
      "  - `138.10.10.15/25`\n",
      "  - `138.10.10.250/25`\n",
      "\n",
      "  **A)** How many subnets do we have?  \n",
      "  **B)** Which of the IP addresses belong to the same subnet?\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import time\n",
    "import base64 \n",
    "from google import genai\n",
    "from google.genai.errors import APIError\n",
    "\n",
    "os.environ[\"GEMINI_API_KEY\"] = \"AQ.Ab8RN6K9G5S-h6IWgpUgfmHbIk7b8iZlWTuYptMFqOT4Ph6FnQ\"\n",
    "client = genai.Client()\n",
    "\n",
    "# Use triple quotes \"\" \" so you can paste long paragraphs easily!\n",
    "\n",
    "prompt_instructions_simple = f\"Rewrite this text for a university course. Make it clear and write WCAG 2.1 AA compliant alt text descriptions for any charts mentioned: {my_notebook_text}\"\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "prompt_instructions_detailed5 = (\n",
    "    \"You are processing raw course text meant for Jupyter Notebook markdown cells.\\n\\n\"\n",
    "    \n",
    "    \"CRITICAL RULE: Do NOT delete, alter, or remove any original image links, image paths, \"\n",
    "    \"or image tags (e.g., lines starting with `![...](...)` or containing `<img ...>`). \"\n",
    "    \"Leave them exactly where they are in the original text sequence.\\n\\n\"\n",
    "    \n",
    "    \"INSTRUCTION:\\n\"\n",
    "    \"Rewrite the text to make it clear and university-appropriate.Make it clear and write WCAG 2.1 AA compliant. Use the following strict sequence \"\n",
    "    \"when dealing with image placements:\\n\\n\"\n",
    "    \n",
    "    \"1. First, insert the NEW Text-Based Structured Alternative ABOVE the image:\\n\"\n",
    "    \"   - For numerical data charts or grid plots: Provide a clean Markdown Data Table.\\n\"\n",
    "    \"   - For hardware architecture, network layouts, or OS Process lifecycles (like fork(), process states, execution threads): \"\n",
    "    \"Provide a high-level text flowchart inside a preformatted ```text code block.\\n\"\n",
    "    \"   - For Terminal Commands or Pipelines (like stdin/stdout piping, shell interactions): \"\n",
    "    \"Provide a vertical box-and-arrow sequence map illustrating data flow from one utility to the next.\\n\"\n",
    "    \"   - Keep diagram characters perfectly aligned and compatible with static HTML page exports.\\n\\n\"\n",
    "    \"   - CRITICAL RULE FOR VIRTUAL DESKTOP LOGIN CONTEXT:\\n\"\n",
    "    \"     If the text or diagram is about logging into the Virtual Desktop or using Ravada, \"\n",
    "    \"the text diagram sequence MUST start exactly with this workflow path:\\n\"\n",
    "    \"     [Web access to Ravada portal with authentication] ===> [Start VM] ===> [Access VM via SSH with provided port]\\n\\n\"\n",
    "    \n",
    "    \"2. Second, place the ORIGINAL untouched image link exactly as it was.\\n\\n\"\n",
    "    \n",
    "    \"3. Third, immediately BELOW the original image link, add the Comprehensive Detailed Description:\\n\"\n",
    "    \"Inside this caption block, you MUST start with the exact text '**Accessible Image Description:**'. Place it always under the image.\"\n",
    "    \"   - Overview: One sentence describing what the visual shows at a high level.\\n\"\n",
    "    \"   - Setup: Explain the primary components, labels, axes, or code pathways without hyper-granular specs.\\n\"\n",
    "    \"   - Key Takeaways: Core conceptual trends or execution results shown.\\n\\n\"\n",
    "\n",
    "    f\"Course Text:\\n{my_notebook_text}\"\n",
    ")\n",
    "prompt_instructions = prompt_instructions_detailed5\n",
    "\n",
    "# Trying the 2.5-flash model which often has lower server congestion\n",
    "try:\n",
    "    response = client.models.generate_content(\n",
    "        model='models/gemini-3.5-flash-lite',\n",
    "        contents= prompt_instructions,\n",
    "        )\n",
    "\n",
    "    #print(response.text)\n",
    "\n",
    "    output_text = response.text\n",
    "    print(output_text)\n",
    "    \n",
    "    # --- THE MAGIC TRICK ---\n",
    "    # This forces Jupyter to create a new cell below and paste the AI response inside it!\n",
    "    #get_ipython().set_next_input(response.text)\n",
    "    \n",
    "except APIError as e:\n",
    "    if e.code == 503:\n",
    "        print(\"Servers are still busy. Waiting 5 seconds before a quick automatic retry...\")\n",
    "        time.sleep(5)\n",
    "        # Automatic second attempt\n",
    "        response = client.models.generate_content(\n",
    "            model='models/gemini-3.5-flash-lite',\n",
    "            contents= prompt_instructions,\n",
    "            )\n",
    "        print(response.text)\n",
    "        # --- THE MAGIC TRICK ---\n",
    "        # This forces Jupyter to create a new cell below and paste the AI response inside it!\n",
    "        #get_ipython().set_next_input(response.text)\n",
    "    else:\n",
    "        raise e\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41a6f33b-8ed3-41b8-ae91-c5b69b5e536f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "# Homework 4: Ubuntu Network Configuration and `ipcalc`\n",
    "\n",
    "**Due Date:** Tuesday, September 29, 2026\n",
    "\n",
    "1. Log into your Virtual Desktop (VD).\n",
    "2. Create a working directory named `HW_4`:\n",
    "   ```bash\n",
    "   mkdir HW_4\n",
    "   cd HW_4\n",
    "   ```\n",
    "3. Inside the `HW_4` directory, create a file named `README`.\n",
    "4. Document all commands used to complete the following exercises inside your `README` file.\n",
    "\n",
    "\n",
    "## Ubuntu Network Configuration\n",
    "\n",
    "- Modify the network settings of the `netplan` Virtual Machine (VM) to change the static IP address to `192.168.122.192`. Record the updated YAML configuration file in your `README`.\n",
    "- Modify the network settings of the `netplan` VM to configure them using Dynamic Host Configuration Protocol (DHCP). Record the updated YAML configuration file in your `README`.\n",
    "\n",
    "\n",
    "## Using `ipcalc` for Network Calculations\n",
    "\n",
    "- Complete Exercise 4.17 to ensure that `ipcalc` is installed on your Virtual Desktop.\n",
    "- Given the following set of IP addresses and netmasks:\n",
    "  - `170.15.25.34/27`\n",
    "  - `170.15.25.192/27`\n",
    "  - `170.15.25.100/27`\n",
    "  - `138.10.10.1/25`\n",
    "  - `138.10.10.15/25`\n",
    "  - `138.10.10.250/25`\n",
    "\n",
    "  **Questions:**\n",
    "  - A) How many unique subnets are represented in this list?\n",
    "  - B) Which of the listed IP addresses belong to the same subnet?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f3c959d-6ea2-41fc-8047-17e0bf9a6fc9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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.13.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
