{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5b9a5dbe-508e-4cca-bb7b-055e100b25f4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "remove-cell",
     "remove_cell"
    ]
   },
   "outputs": [],
   "source": [
    "my_notebook_text = \"\"\"\n",
    "# Homework 6: NFS client on rocky. Shell scripting exercises.\n",
    "\n",
    "Due Date: Tuesday, October 20, 2026\n",
    "\n",
    "## A user account on rocky10-1 VM.\n",
    "\n",
    "- Create user account edward with uid=gid=1666 and home directory in ```/NFS/home/edward``` on rocky10-1 VM.\n",
    "\n",
    "## Configure autofs on rocky10-1 VM.\n",
    "- Set automount for ```/NFS/home``` on rocky91 VM by creating files ```auto.master``` and ```auto.exports``` in directory ```/etc```.\n",
    "\n",
    "- ssh to rocky10-1 VM from the desktop as user edward. Check if /NFS/home gets automounted.\n",
    "\n",
    "## netcat server and client.\n",
    "\n",
    "- Below are two shell scripts.\n",
    "Script ```send.sh```\n",
    "\n",
    "```bash\n",
    "#!/bin/bash\n",
    "ipaddr='192.168.122.11'\n",
    "port='9092'\n",
    "echo \"provide input file name\"\n",
    "read fname\n",
    "/bin/cat \"$fname\" | /bin/netcat -N \"$ipaddr\" $port\n",
    "```\n",
    "\n",
    "- and script ```receive.sh```:\n",
    "```bash\n",
    "#!/bin/bash\n",
    "DIR='/home/hostadm'\n",
    "fname='fileget.out'\n",
    "port='9092'\n",
    "if [ -s \"$DIR/$fname\" ]\n",
    "then\n",
    "echo 'file exists'\n",
    "else\n",
    "/bin/netcat -N -l \"$port\" > \"$DIR/$fname\"\n",
    "fi\n",
    "```\n",
    "\n",
    "- A) What do the scripts do?\n",
    "- B) Create a new VM or use one of the exsting VMs on your system, for example ```master``` or ```n01```. Put script ```receive.sh``` on the VM.\n",
    "Put script ```send.sh``` on the desktop, and modify variable ipaddr in the script to match the IP address of the VM.\n",
    "Run both the scripts to send/receive a file between the desktop and the VM.\n",
    "\n",
    "\n",
    "## Shell script with errors.\n",
    "\n",
    "- Consider the following script:\n",
    "```bash\n",
    "#!/bin/bash\n",
    "cmmd= /usr/bin/md5sum\n",
    "for i in $(ls /etc); do\n",
    "if [ ! -d  /etc/$i ] then\n",
    "$cmmd /etc/$i\n",
    "fi\n",
    "done\n",
    "```\n",
    "\n",
    "It fails to run due to a few syntactic errors.\n",
    "\n",
    "- A) Find the errors in the script by using ```shellcheck```.\n",
    "- B) Explain what the script does.\n",
    "\n",
    "## Looking for files in a deb package by using command grep.\n",
    "\n",
    "- Install package ```libgpg-error-dev``` on your desktop.\n",
    "- What are the file names that in the package that have extensions ```.a```, ```.so```, and ```.h```?\n",
    "- Specify the commands you used to find the file names.\n",
    "\n",
    "\n",
    "## String replacement by using command sed and regular expressions.\n",
    "\n",
    "Download a tar ball archive from http://linuxcourse.rutgers.edu/summer2023/html/Final/linpack_bench.tgz\n",
    "\n",
    "Extract the archive.\n",
    "\n",
    "Create a shell script,\n",
    " ```replace.sh```,\n",
    "\n",
    "A) to replace value of ```N 1000``` by ```N 200``` in file ```linpack_bench.c```.\n",
    "\n",
    "<i>Hint: use command ```sed``` to replace text patern  ```define N``` followed by any integer number.</i>\n",
    "\n",
    "B) Modify the script to read the new value of N from the keyboard input.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f4a25a01-b033-46d1-977d-418c8c38eb52",
   "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 6: NFS Client on Rocky Linux and Shell Scripting Exercises\n",
      "\n",
      "**Due Date:** Tuesday, October 20, 2026\n",
      "\n",
      "## User Account on rocky10-1 Virtual Machine\n",
      "\n",
      "- Create a user account named `edward` with both UID and GID set to `1666`, and configure the home directory in `/NFS/home/edward` on the `rocky10-1` virtual machine.\n",
      "\n",
      "## Configure Autofs on rocky10-1 Virtual Machine\n",
      "\n",
      "- Set up automounting for `/NFS/home` on the `rocky10-1` virtual machine by creating the configuration files `auto.master` and `auto.exports` within the `/etc` directory.\n",
      "- Connect via SSH to the `rocky10-1` virtual machine from your desktop as the user `edward`, and verify whether `/NFS/home` mounts automatically upon access.\n",
      "\n",
      "## Netcat Server and Client Exercises\n",
      "\n",
      "Review the two shell scripts provided below:\n",
      "\n",
      "**Script `send.sh`:**\n",
      "```bash\n",
      "#!/bin/bash\n",
      "ipaddr='192.168.122.11'\n",
      "port='9092'\n",
      "echo \"provide input file name\"\n",
      "read fname\n",
      "/bin/cat \"$fname\" | /bin/netcat -N \"$ipaddr\" $port\n",
      "```\n",
      "\n",
      "**Script `receive.sh`:**\n",
      "```bash\n",
      "#!/bin/bash\n",
      "DIR='/home/hostadm'\n",
      "fname='fileget.out'\n",
      "port='9092'\n",
      "if [ -s \"$DIR/$fname\" ]\n",
      "then\n",
      "echo 'file exists'\n",
      "else\n",
      "/bin/netcat -N -l \"$port\" > \"$DIR/$fname\"\n",
      "fi\n",
      "```\n",
      "\n",
      "- **A)** Analyze both scripts and explain their overall functionality and interaction.\n",
      "- **B)** Provision a new virtual machine or utilize an existing one from your environment (such as `master` or `n01`). Place the `receive.sh` script onto the target virtual machine, place the `send.sh` script on your desktop, and update the `ipaddr` variable in `send.sh` to match the IP address of your target virtual machine. Execute both scripts to securely transmit a file from the desktop to the virtual machine.\n",
      "\n",
      "## Shell Script with Syntax Errors\n",
      "\n",
      "Examine the following flawed script:\n",
      "\n",
      "```bash\n",
      "#!/bin/bash\n",
      "cmmd= /usr/bin/md5sum\n",
      "for i in $(ls /etc); do\n",
      "if [ ! -d  /etc/$i ] then\n",
      "$cmmd /etc/$i\n",
      "fi\n",
      "done\n",
      "```\n",
      "\n",
      "This script fails to execute successfully due to several syntactic errors.\n",
      "\n",
      "- **A)** Identify and list the errors present in the script by running it through the `shellcheck` utility.\n",
      "- **B)** Explain the intended purpose and behavior of the script once the syntax errors are corrected.\n",
      "\n",
      "## Searching for Files in a Debian Package Using Grep\n",
      "\n",
      "- Install the development package `libgpg-error-dev` on your desktop environment.\n",
      "- Identify the names of the files included in this package that end with the extensions `.a`, `.so`, and `.h`.\n",
      "- Document the exact terminal commands used to locate and filter these file names.\n",
      "\n",
      "## String Replacement Using Sed and Regular Expressions\n",
      "\n",
      "Download the source tarball archive from the course repository at:\n",
      "`http://linuxcourse.rutgers.edu/summer2023/html/Final/linpack_bench.tgz`\n",
      "\n",
      "Extract the contents of the archive.\n",
      "\n",
      "Create a shell script named `replace.sh`:\n",
      "\n",
      "- **A)** Write instructions within the script to replace the configuration value `N 1000` with `N 200` inside the `linpack_bench.c` source file.  \n",
      "  *(Hint: Use the `sed` command to match the text pattern `define N` followed by any arbitrary integer).*\n",
      "- **B)** Modify the script so that it prompts the user to enter the new value for `N` interactively via keyboard input.\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": "14dbb953-bb6e-481d-a211-ed282daf279a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "# Homework 6: NFS Client on Rocky Linux and Shell Scripting Exercises\n",
    "\n",
    "**Due Date:** Tuesday, October 20, 2026\n",
    "\n",
    "## User Account on rocky10-1 Virtual Machine\n",
    "\n",
    "- Create a user account named `edward` with both UID and GID set to `1666`, and configure the home directory in `/NFS/home/edward` on the `rocky10-1` virtual machine.\n",
    "\n",
    "## Configure Autofs on rocky10-1 Virtual Machine\n",
    "\n",
    "- Set up automounting for `/NFS/home` on the `rocky10-1` virtual machine by creating the configuration files `auto.master` and `auto.exports` within the `/etc` directory.\n",
    "- Connect via SSH to the `rocky10-1` virtual machine from your desktop as the user `edward`, and verify whether `/NFS/home` mounts automatically upon access.\n",
    "\n",
    "## Netcat Server and Client Exercises\n",
    "\n",
    "Review the two shell scripts provided below:\n",
    "\n",
    "**Script `send.sh`:**\n",
    "```bash\n",
    "#!/bin/bash\n",
    "ipaddr='192.168.122.11'\n",
    "port='9092'\n",
    "echo \"provide input file name\"\n",
    "read fname\n",
    "/bin/cat \"$fname\" | /bin/netcat -N \"$ipaddr\" $port\n",
    "```\n",
    "\n",
    "**Script `receive.sh`:**\n",
    "```bash\n",
    "#!/bin/bash\n",
    "DIR='/home/hostadm'\n",
    "fname='fileget.out'\n",
    "port='9092'\n",
    "if [ -s \"$DIR/$fname\" ]\n",
    "then\n",
    "echo 'file exists'\n",
    "else\n",
    "/bin/netcat -N -l \"$port\" > \"$DIR/$fname\"\n",
    "fi\n",
    "```\n",
    "\n",
    "- **A)** Analyze both scripts and explain their overall functionality and interaction.\n",
    "- **B)** Provision a new virtual machine or utilize an existing one from your environment (such as `master` or `n01`). Place the `receive.sh` script onto the target virtual machine, place the `send.sh` script on your desktop, and update the `ipaddr` variable in `send.sh` to match the IP address of your target virtual machine. Execute both scripts to securely transmit a file from the desktop to the virtual machine.\n",
    "\n",
    "## Shell Script with Syntax Errors\n",
    "\n",
    "Examine the following flawed script:\n",
    "\n",
    "```bash\n",
    "#!/bin/bash\n",
    "cmmd= /usr/bin/md5sum\n",
    "for i in $(ls /etc); do\n",
    "if [ ! -d  /etc/$i ] then\n",
    "$cmmd /etc/$i\n",
    "fi\n",
    "done\n",
    "```\n",
    "\n",
    "This script fails to execute successfully due to several syntactic errors.\n",
    "\n",
    "- **A)** Identify and list the errors present in the script by running it through the `shellcheck` utility.\n",
    "- **B)** Explain the intended purpose and behavior of the script once the syntax errors are corrected.\n",
    "\n",
    "## Searching for Files in a Debian Package Using Grep\n",
    "\n",
    "- Install the development package `libgpg-error-dev` on your desktop environment.\n",
    "- Identify the names of the files included in this package that end with the extensions `.a`, `.so`, and `.h`.\n",
    "- Document the exact terminal commands used to locate and filter these file names.\n",
    "\n",
    "## String Replacement Using Sed and Regular Expressions\n",
    "\n",
    "Download the source tarball archive from the course repository at:\n",
    "`http://linuxcourse.rutgers.edu/summer2023/html/Final/linpack_bench.tgz`\n",
    "\n",
    "Extract the contents of the archive.\n",
    "\n",
    "Create a shell script named `replace.sh`:\n",
    "\n",
    "- **A)** Write instructions within the script to replace the configuration value `N 1000` with `N 200` inside the `linpack_bench.c` source file.  \n",
    "  *(Hint: Use the `sed` command to match the text pattern `define N` followed by any arbitrary integer).*\n",
    "- **B)** Modify the script so that it prompts the user to enter the new value for `N` interactively via keyboard input.\n"
   ]
  }
 ],
 "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
}
