-
-
Save kraeml/3475c759eb916c6831c6a91c8413ebae to your computer and use it in GitHub Desktop.
rdf/itec/Net-Lab.ipynb
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": [ | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "# IAV-Net Lab" | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "## Aufgabenstellung\n\n* Darstellen der Bridges wie `lxbr0`.\n* Darstellen der virtuellen PCs wie `bsa-pc1` als LXC Container.\n\nErweiterung:\n\n* Konfiguration der Bridges und virtuellen PCs.\n\nTop:\n\n* Öffnen einer ssh Shell zu den PCs." | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "## Darstellung\n\nTabellen in plain HTML5 mit Hilfe von Jinja2 Template.\n\nDer Webserver soll mit `flask` (Python) oder mit `nodered` (JavaScript und Python) realisiert werden.\n\n" | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "### Bridges" | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "| Name | Status | Domain | IP/CDIR | Range | fixe IPs |\n|------|--------|--------|---------|-------|----------|\n| lxbr0 | up | lxc | 10.0.3.1/24 | 10.0.3.20 bis 10.0.3.254 | 10.0.3.2 |\n| ... | ... | ... | ... | ... | ... |" | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "### PCs" | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "| Name | Template | Status | IP |\n|------|----------|--------|----|\n| bsa-pc1 | ubuntu | run | 10.0.3.2 |\n| ... | ... | ... | ... |" | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "## Proof of concept\n\n* Mit Python `bridges` und `lxc`-Container verwalten.\n* Arbeiten mit Schleifen in Jinja2." | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "Auslesen von `lxc` und `bridges` wäre möglich.\n\nWebsites:\n\n* https://motoma.io/bridging-the-network-in-pure-python/\n* https://linuxcontainers.org/lxc/documentation/\n* https://github.com/AdaptiveScale/lxdui\n* https://github.com/udragon/pybrctl\n* https://github.com/cloud9ers/pylxc\n* https://github.com/rlisagor/pynetlinux\n\nWebsites Jinja2\n\n* https://stackoverflow.com/questions/12655155/jinja2-for-loop-with-conditions\n* https://stackoverflow.com/questions/9198334/how-to-build-up-a-html-table-with-a-simple-for-loop-in-jinja2\n* https://sgros-students.blogspot.de/2013/04/lxc-python-bindings.html\n\nAnsible mit Python.\n\n* Module ansible\n* http://docs.ansible.com/ansible/latest/dev_guide/developing_api.html\n\nShell\n\n* https://www.cyberciti.biz/faq/python-run-external-command-and-get-output/" | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "## Resüme\n\nVerwenden der `python-lxc` API.\n\n* Achtung läuft nur als `root`, da `lxc` als `root` ausgeführt werden muss.\n\nAufrufen von Shellkommandos via Python. " | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "markdown", | |
| "source": "## Externe Programme mit Python" | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "* https://docs.python.org/3/library/subprocess.html\n\n* https://www.python-kurs.eu/os_modul_shell.php" | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "import os\ndir = os.popen(\"ls -lisa\").readlines()\nfor i in dir:\n print(i)", | |
| "execution_count": 1, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "insgesamt 68\n\n2896658 4 drwxrwxr-x 6 vagrant vagrant 4096 Mär 7 10:45 .\n\n2896655 4 drwxrwxr-x 6 vagrant vagrant 4096 Feb 17 20:38 ..\n\n2899210 4 drwxr-xr-x 2 vagrant vagrant 4096 Mär 7 07:39 .ipynb_checkpoints\n\n2899069 16 -rw-r--r-- 1 vagrant vagrant 12342 Mär 7 10:45 Net-Lab.ipynb\n\n2897601 20 -rw-r--r-- 1 vagrant vagrant 17885 Mär 7 09:49 notebook.tex\n\n2883593 4 drwxrwxr-x 8 vagrant vagrant 4096 Feb 21 12:01 python-kopf-bis-fuss\n\n2899214 4 drwxr-xr-x 7 vagrant vagrant 4096 Feb 28 11:56 python-kopf-bis-fuss-feb18\n\n2896941 4 drwxrwxr-x 3 vagrant vagrant 4096 Feb 17 20:38 python_routine_aufgaben\n\n2899121 8 -rw-r--r-- 1 vagrant vagrant 5143 Feb 28 10:45 Untitled.ipynb\n\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "import os\ndir = os.popen(\"ls -lisa\").read()\nprint(dir)", | |
| "execution_count": 2, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "insgesamt 64\n2896658 4 drwxrwxr-x 6 vagrant vagrant 4096 Mär 7 10:47 .\n2896655 4 drwxrwxr-x 6 vagrant vagrant 4096 Feb 17 20:38 ..\n2899210 4 drwxr-xr-x 2 vagrant vagrant 4096 Mär 7 07:39 .ipynb_checkpoints\n2899069 12 -rw-r--r-- 1 vagrant vagrant 8526 Mär 7 10:47 Net-Lab.ipynb\n2897601 20 -rw-r--r-- 1 vagrant vagrant 17885 Mär 7 09:49 notebook.tex\n2883593 4 drwxrwxr-x 8 vagrant vagrant 4096 Feb 21 12:01 python-kopf-bis-fuss\n2899214 4 drwxr-xr-x 7 vagrant vagrant 4096 Feb 28 11:56 python-kopf-bis-fuss-feb18\n2896941 4 drwxrwxr-x 3 vagrant vagrant 4096 Feb 17 20:38 python_routine_aufgaben\n2899121 8 -rw-r--r-- 1 vagrant vagrant 5143 Feb 28 10:45 Untitled.ipynb\n\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "help(os.popen)", | |
| "execution_count": 3, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "Help on function popen in module os:\n\npopen(cmd, mode='r', buffering=-1)\n # Supply os.popen()\n\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "> Modul subprocess soll andere Module ersetzen:\n\n os.system\n os.spawn*\n os.popen*\n popen2.*\n commands.*\n\nhttps://www.python-kurs.eu/os_modul_shell.php" | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "import subprocess", | |
| "execution_count": 4, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "subprocess.run([\"ls\", \"-l\"])", | |
| "execution_count": 5, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "execution_count": 5, | |
| "data": { | |
| "text/plain": "CompletedProcess(args=['ls', '-l'], returncode=0)" | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "import shlex", | |
| "execution_count": 6, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "command_line = input()", | |
| "execution_count": 7, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": "ls -l\n" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "args = shlex.split(command_line)", | |
| "execution_count": 8, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "print(args)", | |
| "execution_count": 9, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "['ls', '-l']\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "p = subprocess.run(args, stdout=subprocess.PIPE, shell=True)", | |
| "execution_count": 10, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "help(p)", | |
| "execution_count": 11, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "Help on CompletedProcess in module subprocess object:\n\nclass CompletedProcess(builtins.object)\n | A process that has finished running.\n | \n | This is returned by run().\n | \n | Attributes:\n | args: The list or str args passed to run().\n | returncode: The exit code of the process, negative for signals.\n | stdout: The standard output (None if not captured).\n | stderr: The standard error (None if not captured).\n | \n | Methods defined here:\n | \n | __init__(self, args, returncode, stdout=None, stderr=None)\n | Initialize self. See help(type(self)) for accurate signature.\n | \n | __repr__(self)\n | Return repr(self).\n | \n | check_returncode(self)\n | Raise CalledProcessError if the exit code is non-zero.\n | \n | ----------------------------------------------------------------------\n | Data descriptors defined here:\n | \n | __dict__\n | dictionary for instance variables (if defined)\n | \n | __weakref__\n | list of weak references to the object (if defined)\n\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "p = subprocess.Popen(\"ls -l\", stdout=subprocess.PIPE, shell=True)", | |
| "execution_count": 13, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "(output, err) = p.communicate()", | |
| "execution_count": 14, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "print(output.decode(\"utf-8\"))", | |
| "execution_count": 15, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "insgesamt 52\n-rw-r--r-- 1 vagrant vagrant 10735 Mär 7 10:53 Net-Lab.ipynb\n-rw-r--r-- 1 vagrant vagrant 17885 Mär 7 09:49 notebook.tex\ndrwxrwxr-x 8 vagrant vagrant 4096 Feb 21 12:01 python-kopf-bis-fuss\ndrwxr-xr-x 7 vagrant vagrant 4096 Feb 28 11:56 python-kopf-bis-fuss-feb18\ndrwxrwxr-x 3 vagrant vagrant 4096 Feb 17 20:38 python_routine_aufgaben\n-rw-r--r-- 1 vagrant vagrant 5143 Feb 28 10:45 Untitled.ipynb\n\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "print(err)", | |
| "execution_count": 16, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "None\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "p = subprocess.Popen(\"date\", stdout=subprocess.PIPE, shell=True)", | |
| "execution_count": 17, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "(output, err) = p.communicate()", | |
| "execution_count": 18, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "markdown", | |
| "source": "Warten bis der Subprocess beendet ist." | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "p_status = p.wait()", | |
| "execution_count": 19, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "print(output.decode('utf-8'))", | |
| "execution_count": 20, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "Mi 7. Mär 10:58:35 UTC 2018\n\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "print(p_status)", | |
| "execution_count": 21, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "0\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "p = subprocess.Popen(\"ps aux | grep dnsmasq | grep lxcbr0\", stdout=subprocess.PIPE, shell=True)", | |
| "execution_count": 23, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "(output, err) = p.communicate()", | |
| "execution_count": 25, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "print(output.decode('utf-8'))", | |
| "execution_count": 27, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": "lxc-dns+ 2102 0.0 0.1 54768 2424 ? S 07:31 0:03 dnsmasq --conf-file=/etc/lxc/dnsmasq.conf -s lxc -S /lxc/ -u lxc-dnsmasq --strict-order --bind-interfaces --pid-file=/run/lxc/dnsmasq.pid --listen-address 10.0.3.1 --dhcp-range 10.0.3.20,10.0.3.254 --dhcp-lease-max=253 --dhcp-no-override --except-interface=lo --interface=lxcbr0 --dhcp-leasefile=/var/lib/misc/dnsmasq.lxcbr0.leases --dhcp-authoritative\nvagrant 25425 0.0 0.0 4504 752 ? SN 11:04 0:00 /bin/sh -c ps aux | grep dnsmasq | grep lxcbr0\n\n", | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "output", | |
| "execution_count": 28, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "execution_count": 28, | |
| "data": { | |
| "text/plain": "b'lxc-dns+ 2102 0.0 0.1 54768 2424 ? S 07:31 0:03 dnsmasq --conf-file=/etc/lxc/dnsmasq.conf -s lxc -S /lxc/ -u lxc-dnsmasq --strict-order --bind-interfaces --pid-file=/run/lxc/dnsmasq.pid --listen-address 10.0.3.1 --dhcp-range 10.0.3.20,10.0.3.254 --dhcp-lease-max=253 --dhcp-no-override --except-interface=lo --interface=lxcbr0 --dhcp-leasefile=/var/lib/misc/dnsmasq.lxcbr0.leases --dhcp-authoritative\\nvagrant 25425 0.0 0.0 4504 752 ? SN 11:04 0:00 /bin/sh -c ps aux | grep dnsmasq | grep lxcbr0\\n'" | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3", | |
| "language": "python" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "version": 3, | |
| "name": "ipython" | |
| }, | |
| "version": "3.5.2", | |
| "nbconvert_exporter": "python", | |
| "file_extension": ".py", | |
| "pygments_lexer": "ipython3", | |
| "name": "python", | |
| "mimetype": "text/x-python" | |
| }, | |
| "toc": { | |
| "nav_menu": {}, | |
| "number_sections": true, | |
| "sideBar": true, | |
| "skip_h1_title": false, | |
| "title_cell": "Table of Contents", | |
| "title_sidebar": "Contents", | |
| "toc_cell": false, | |
| "toc_position": {}, | |
| "toc_section_display": true, | |
| "toc_window_display": false | |
| }, | |
| "gist": { | |
| "id": "a274aadc3605b2d2c1ba449213fc4c74", | |
| "data": { | |
| "description": "rdf/itec/Net-Lab.ipynb", | |
| "public": true | |
| } | |
| }, | |
| "_draft": { | |
| "nbviewer_url": "https://gist.github.com/a274aadc3605b2d2c1ba449213fc4c74" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment