{ "info": { "author": "Yongping Guo", "author_email": "guoyoooping@163.com", "bugtrack_url": null, "classifiers": [], "description": "networkdevice\n*************\n\nPython modules to execut command on remote network device.\n\n1. Introduction\n===============\n\nnetworkdevice is a python module that allow you run command on remote network\ndevices just like locally.\n\nFor example, define a network devices and show the interface on it, it's\nespecially useful for network test automation:\n\n.. code-block:: python\n\n pc = networkdevice.LinuxDevice(name = \"officer\",\n host = \"10.208.172.12\",\n username = \"dev\",\n password = \"1234\")\n print pc.cmd(\"ifconfig\")\n\nUse help command show the documents::\n\n import from networkdevice import cisco, junos, linux\n help(cisco)\n help(junos)\n help(linux)\n\n2. Feature\n==========\n\n 1) Python Based: Plenty of feature\n 2) Environmentally friendly: can run anywhere where there is python and connect to the devices.\n 3) Easy to Learn: Don't need to know anything about Python\n 4) Easy to write: One case only have several to dozens of lines.\n 5) Faster: run the testbed from local and is much faster.\n 6) object oriented: Flexible and Easy to extend\n\n3. Test architecture based on networkdevice\n===========================================\n::\n\n +---------------------------------+------------+----------------------+\n | | | case1 |\n | | +----------------------+\n | One case | Test Suite | ... |\n | | +----------------------+\n | | | caseN |\n +---------------------------------+------------+----------------------+\n | networkdevice |\n | |\n | PC1 DUT DUT |\n | +---------------+ +---------------+ +---------------+ |\n | | Linux devices | | Junos devices | | Cisco devices | ... |\n | +---------------+ +---------------+ +---------------+ |\n | | Linux devices | | Linux devices | ... |\n | +---------------+ +---------------+ |\n +---------------------------------------------------------------------+\n\n test Architecture based on networkdevice\n \n4. Object description\n=====================\n\n4.1 LinuxDevice\n---------------\n\nLinuxDevice is a common abstraction for linux like devices. There are two ways\nto define a LinuxDevice object:\n\n1) use a dictionary:\n\n.. code-block:: python\n\n ent_vm01 = {\n \"name\": \"ent-vm01\",\n \"host\": \"10.208.172.12\",\n \"username\": \"root\",\n \"password\": \"5678\"\n \"prompt\": \"root@ent-vm01 ~\",\n \"interface\": [\n {\n \"ip\": \"4.4.4.2/24\",\n \"ip6\": \"2004::2/64\",\n \"name\": \"eth1\"\n }\n ],\n }\n client = linux.LinuxDevice(ent_vm01)\n\n2) use the parameter list:\n\n.. code-block:: python\n\n client = linux.LinuxDevice(name = \"ent-vm01\",\n host = \"ent-vm01\",\n prompt = \"root@ent-vm01 ~\",\n username = \"root\",\n password = \"5678\")\n\nAfter the LinuxDevice is created, all the input parameters could be used as its\nattributes. For example::\n\n print client[\"name\"]\n print client[\"interface\"][0][\"ip\"]\n\nNow LinuxDevice support the following attributes, some are mantodary while the\nothers are optional. If some parameters are not given, the following default\nvalue will be used::\n\n device0 = {\n # mandtory, if not given, it will fail to construct a device\n \"name\": \"\", # A name of the devices, used for log and\n # shell prompt;\n \"host\": \"\", # A ip address or hostname that can connect;\n \"username\": \"\", # Usename to login;\n \"password\": \"\", # Password to login;\n \"root_password\": \"\", # Root password, optional for linux devices,\n # mandtory for Junos devices;\n\n # Optional, if not given, use the default\n \"prompt\": None, # A shell prompt, if not given, use\n # username@name, it's not correct, it's better\n # destinate;\n \"fd\": sys.stdout, # log files, default is the stdout;\n \"mode\": \"ssh\", # login method, default is ssh, support ssh\n # and telnet now;\n \"interface\": [], # A list interface the device will configure;\n \"preconfig\": [], # A list of cmd/configuration the device will\n # configure;\n \"noconfig\": False, # If ture, will not configure the interface\n # and preconfig before test;\n \"color\": \"blue\", # log color\n \"log_level\": LOG_INFO, # log level\n }\n\nLinuxDevice has the following variable:\n\ncolor support the following name::\n\n 'black', 'dark_gray', 'light_gray', 'blue', 'light_blue', 'green',\n 'light_green', 'cyan', 'light_cyan', 'red', 'light_red', 'purple',\n 'light_purple', 'brown', 'yellow', 'white', 'default_color', 'red_bold'\n\ntrace level support the following name::\n\n LOG_EMERG = 0\n LOG_ALERT = 1\n LOG_CRIT = 2\n LOG_ERR = 3\n LOG_WARNING = 4\n LOG_NOTICE = 5\n LOG_INFO = 6\n LOG_DEBUG = 7\n\nLinuxDevice support the folowing method:\n\n __init__(self, device = None, \\*\\*kwargs):\n\n Constructor, create a pexpect session, configure the preconfig and\n initialize some variables.\n\n __del__(self):\n\n Deconstructor, free allocated resources and restore some configuration.\n\n cmd(self, cmd, expect = None, \\*\\*kwargs):\n \n Execute the command @cmd and return the execution result. If the\n command is non-interactive, the result is returned directly, for\n example::\n\n print client.cmd('ifconfig')\n\n If the command @cmd is interactive, you may input some prompt and\n command before you get the final result, for example::\n\n client.cmd('ftp 1.1.1.2', expect = \"Name\")\n client.cmd('dev', expect = \"Password\")\n client.cmd('1234', expect = \"ftp\")\n print client.cmd('ls', expect = \"ftp\")\n client.cmd('bye')\n\n @ expect: If the remote command couldn't return the system prompt, you\n need designate the prompt, for example::\n\n client.cmd('ftp 1.1.1.2', expect = \"ftp>\")\n\n @ timeout: Wait how many seconds before the timeout, default value is 3\n seconds.\n\n log (self, message, level = LOG_INFO):\n\n record the log to file self[\"fd\"] with the color self[\"color\"].\n\n @ message: the log to be write.\n @ level: log level\n\n dumps(self):\n\n Dump all its attributes.\n\n get_file(self, filename, localname = '.'):\n\n Get file @filename from @localname to local. Useful to get log file\n from remote testbed.\n\n put_file(self, filename, remotedir):\n\n Put file @filename from local to @remotedir of this linux device.\n Useful to put the configuration file to remote testbed.\n\n __getitem__(self, name):\n\n Get certain attribute, for example::\n\n print self[\"name\"]\n\n __setitem__(self, name, value):\n\n Set certain attribute, for example::\n\n self[\"color\"] = \"red\"\n\n4.2 JunosDevice\n---------------\n\nJunosDevice is a common abstraction for Juniper network devices. It derives\nfrom LinuxDevice so it has every method of LinuxDevice, exception some of them\nare overrided. You use the similar way to define a JunosDevice, for example::\n\n tangshan = {\n \"name\": \"tangshan\",\n \"username\": \"dev\",\n \"password\": \"1234\"\n \"root_password\": \"5678\",\n \"host\": \"10.208.128.19\",\n \"interface\": [\n {\n \"ip\": \"1.1.1.2/24\",\n \"ip6\": \"2001::2/64\",\n \"name\": \"fe-0/0/2.0\",\n \"zone\": \"untrust\"\n },\n {\n \"ip\": \"4.4.4.1/24\",\n \"ip6\": \"2004::1/64\",\n \"name\": \"fe-0/0/6.0\",\n \"zone\": \"trust\"\n }\n ],\n \"preconfig\": [\n \"set routing-options static route 1.1.1.0/24 next-hop 2.2.2.1\"\n ],\n }\n dut = junos.JunosDevice(tangshan)\n dut = junos.JunosDevice(name = \"tangshan\",\n host = \"10.208.128.19\",\n username = \"root\",\n password = \"5678\",\n root_password = \"5678\",\n interface = [\n {\n \"ip\": \"1.1.1.2/24\",\n \"ip6\": \"2001::2/64\",\n \"name\": \"fe-0/0/2.0\",\n \"zone\": \"untrust\" },\n {\n \"ip\": \"4.4.4.1/24\",\n \"ip6\": \"2004::1/64\",\n \"name\": \"fe-0/0/6.0\",\n \"zone\": \"trust\" }],\n preconfig = [ \"set routing-options static route 1.1.1.0/24 next-hop 2.2.2.1\"])\n\nJunosDevice support the folowing method:\n\n __init__(self, device = None, \\*\\*kwargs):\n\n Constructor, create a pexpect session, configure the preconfig and initialize some variables.\n\n __del__(self):\n\n Deconstructor, free allocated resources and restore some configuration.\n\n def cmd(self, cmd, mode = \"shell\", \\*\\*kwargs):\n \n Similar LinuxDevice.cmd, but add some more mode:\n\n @ mode == \"shell\": equal LinuxDevice.cmd\n\n @ mode == \"cli\": execute cli command in Junos, equal cli()\n\n @ mode == \"configure\": execute configure command, equal configure()\n\n @ mode == \"vty\": execute vty command, equal vty()\n\n cli(self, cmd, \\*\\*kwargs):\n\n Junos cli command.\n\n @timeout: time to wait for the execute command return. default is 5\n seconds\n\n @display: Junos command options, Show additional kinds of information,\n possible completions are \"xml\" and \"json\". Please note that \"json\" is\n supported since X49.\n\n @format: Which kind object you'd like the cmd return. Options include\n \"text\", \"dict\" and \"json\". Default is \"text\" and it's return directly\n from the cmd result. if \"dict\" or \"json\" is selected, the result will\n be parsed to python dict or json object.\n\n @force_list: When we parse the dictionaly from xml output, to force\n lists to be created even when there is only a single child of a given\n level of hierarchy. The force_list argument is a tuple of keys. If the\n key for a given level of hierarchy is in the force_list argument, that\n level of hierarchy will have a list as a child (even if there is only\n one sub-element). The index_keys operation takes precendence over this.\n This is applied after any user-supplied postprocessor has already run.\n\n For example, given this input::\n\n \n \n host1\n Linux\n \n \n em0\n 10.0.0.1\n \n \n \n \n\n If called with::\n\n dut.cmd(\"balabala\", display = \"xml\", force_list=('interface',))\n \n it will produce this dictionary::\n\n {'servers':\n {'server':\n {'name': 'host1',\n 'os': 'Linux'},\n 'interfaces': {'interface': [ {'name': 'em0', 'ip_address': '10.0.0.1' } ] } } }\n\n For examples to get the session list on plaintext format::\n\n print dut.cli('show security flow session application ftp')\n\n For examples to get the session list on dictionaly format::\n\n displayed = dut.cli('show security flow session application ftp',\n format = \"dict\",\n force_list=('flow-session'))\n print \"Total %s session found\" %(displayed['flow-session-information']['displayed-session-count'])\n\n configure(self, cmd, \\*\\*kwargs):\n\n Execute a configure command and return the result. For example::\n\n dut.configure('set security flow traceoptions flag all')\n dut.configure('set security traceoptions file flow.log size 50m')\n dut.configure('set security traceoptions level verbose')\n dut.configure('set security traceoptions flag all')\n dut.configure('commit')\n\n vty(self, cmd, \\*\\*kwargs):\n\n Execute a vty command and return the result.\n\n @timeout: time to wait for the execute command return. default is 5\n seconds\n\n @tnp_addr: tnp address to login.\n\n For example::\n\n print dut.vty(\"show usp flow config\", tnp_addr = \"node0.fpc0.pic1\")\n\n install_image (self, image):\n \n To be implemented.\n\n5. An example\n=============\n\nIn this example, we login the client linux device and then ftp the server.\nCheck if there is session generated on the Juniper SRX firewall. Then tear down\nthe connection:\n\n.. code-block:: python\n\n #!/usr/bin/env python\n from networkdevice import cisco, junos, linux\n\n tangshan = {\n \"name\": \"tangshan\",\n \"host\": \"tangshan\",\n \"username\": \"dev\",\n \"password\": \"1234\"\n \"root_password\": \"5678\",\n \"interface\": [\n {\n \"ip\": \"1.1.1.2/24\",\n \"ip6\": \"2001::2/64\",\n \"name\": \"fe-0/0/2.0\",\n \"zone\": \"untrust\"\n },\n {\n \"ip\": \"4.4.4.1/24\",\n \"ip6\": \"2004::1/64\",\n \"name\": \"fe-0/0/6.0\",\n \"zone\": \"trust\"\n }\n ],\n \"preconfig\": [\n \"set routing-options static route 1.1.1.0/24 next-hop 2.2.2.1\"\n ],\n }\n\n ent_vm01 = {\n \"name\": \"ent-vm01\"\n \"host\": \"ent-vm01\",\n \"username\": \"root\",\n \"password\": \"5678\",\n \"prompt\": \"root@ent-vm01 ~\",\n }\n\n if __name__ == '__main__':\n '''\n Topology:\n\n +----------+\n | server |\n +----+-----+\n | int[0]\n |\n | int[0]\n +----+-----+\n | DUT |\n +----------+\n | int[1]\n |\n | int[0]\n +----+-----+\n | client |\n +----------+\n '''\n # Use device descriptor to create a linux and junos device\n dut = junos.JunosDevice(tangshan)\n client = linux.LinuxDevice(ent_vm01)\n # Use parameter list to create a linux device\n server = linux.LinuxDevice(name = \"ent-vm02\",\n host = \"ent-vm02\",\n prompt = \"root@ent-vm02 ~\",\n username = \"root\",\n password = \"5678\")\n\n # Dump all the dut's attributes\n dut.dumps()\n print dut.cli('show version')\n\n # execute a non-interactive command and return the result\n print client.cmd('date')\n\n # execute an interactive command\n client.cmd('ftp 1.1.1.2', expect = \"Name\")\n client.cmd('%s' %(server[\"username\"]), expect = \"Password\")\n client.cmd('%s' %(server[\"password\"]), expect = \"ftp\")\n print client.cmd('ls', expect = \"ftp\")\n\n # check session on the Juniper srx firewall\n displayed = dut.cli('show security flow session application ftp',\n format = \"dict\",\n force_list=('flow-session'))\n if int(displayed['flow-session-information']['displayed-session-count']) <= 0:\n print \"No session found\"\n\n # tear down the ftp session\n client.cmd('bye')", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/guoyoooping/networkdevice", "keywords": "", "license": "GPLv3", "maintainer": "", "maintainer_email": "", "name": "networkdevice", "package_url": "https://pypi.org/project/networkdevice/", "platform": "", "project_url": "https://pypi.org/project/networkdevice/", "project_urls": { "Homepage": "https://github.com/guoyoooping/networkdevice" }, "release_url": "https://pypi.org/project/networkdevice/0.4/", "requires_dist": null, "requires_python": "", "summary": "Python modules to execut command on remote network device based on pexpect.", "version": "0.4" }, "last_serial": 2860561, "releases": { "0.4": [ { "comment_text": "", "digests": { "md5": "6d8e561ef642026f9754dafea6f0b061", "sha256": "f351e07b4a6b5ecd12276401d361a9b73054880345683439c6e481b461336502" }, "downloads": -1, "filename": "networkdevice-0.4.tar.gz", "has_sig": false, "md5_digest": "6d8e561ef642026f9754dafea6f0b061", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14087, "upload_time": "2017-05-08T11:18:56", "url": "https://files.pythonhosted.org/packages/f4/b5/640a9929bd44322fc1eca1f5879686f22ddab5598eb545b4ed4ec009627f/networkdevice-0.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "6d8e561ef642026f9754dafea6f0b061", "sha256": "f351e07b4a6b5ecd12276401d361a9b73054880345683439c6e481b461336502" }, "downloads": -1, "filename": "networkdevice-0.4.tar.gz", "has_sig": false, "md5_digest": "6d8e561ef642026f9754dafea6f0b061", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14087, "upload_time": "2017-05-08T11:18:56", "url": "https://files.pythonhosted.org/packages/f4/b5/640a9929bd44322fc1eca1f5879686f22ddab5598eb545b4ed4ec009627f/networkdevice-0.4.tar.gz" } ] }