{"id":12069,"date":"2020-03-10T06:20:03","date_gmt":"2020-03-10T10:20:03","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=12069"},"modified":"2020-03-10T06:20:03","modified_gmt":"2020-03-10T10:20:03","slug":"simulate-hardware-devices-and-switches-for-testing","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/simulate-hardware-devices-and-switches-for-testing\/","title":{"rendered":"Simulate hardware devices and switches for testing"},"content":{"rendered":"<h6><strong>Why and Who is this POST for?<\/strong><\/h6>\n<p>There can be situations\u00a0where QA or Developers need real hardware devices for suppose when developers need to test their code or when QA has to do end to end testing with it. I am one of those QA&#8217;s who was wondering how to simulate the hardware devices like linux machine, cisco routers and some switches with SNMP configuration for my end to end testing. I thought sharing this experience could help people like me out there.<\/p>\n<p><strong>SNMPSIM<\/strong><\/p>\n<p>I have used following python Modules to simulate the snmp devices \/ switches<\/p>\n<ul>\n<li><strong>snmpsim Installation<\/strong><\/li>\n<\/ul>\n<pre>$ virtualenv -p \/usr\/bin\/python3 devices\r\n$ <span class=\"nb\">source<\/span> devices\/bin\/activate\r\n$ pip install snmpsim<\/pre>\n<p>please note that snmpsim installation would get a sample\u00a0simulation data file under the data directory and\u00a0<span>it is stored in simple plaint-text files having OID|TYPE|VALUE format<\/span><\/p>\n<ul>\n<li><strong>Run snmp simulators<\/strong><\/li>\n<\/ul>\n<p>We can use snmpsim-command-responder daemon or snmpsimd.py tool to run simulators and this tool can run multiple individual daemons at a time<\/p>\n<pre>$ snmpsimd.py --data-dir=.\/data --agent-udpv4-endpoint=127.0.0.1:8089 &amp;<\/pre>\n<pre>$ snmpsim-command-responder\u00a0--data-dir=.\/data --agent-udpv4-endpoint=127.0.0.1:8089 &amp;<\/pre>\n<p>If snmpsim commands are not working,\u00a0 then try to pip install<\/p>\n<p>https:\/\/github.com\/etingof\/snmpsim\/archive\/master.zip<\/p>\n<ul>\n<li><strong>check if simulated devices\/agents are up and running<\/strong><\/li>\n<\/ul>\n<p>snmpwalk is the application that can be used to query the snmp simulators ,\u00a0you will see no response if the device is not up else it would return snmp data in the response<\/p>\n<pre>$ snmpwalk -v1 -c public 127.0.0.1:8089<\/pre>\n<ul>\n<li><strong>Below is the python code snippet to run simulated devices on linux system<\/strong><\/li>\n<\/ul>\n<pre lang=\"python\">\"\"\"Run n number of snmp simulated devices on linux\"\"\"\r\n\r\nimport os\r\nimport argparse\r\nfrom faker import Faker\r\nimport socket\r\n\r\n\r\ndef parse_args():\r\n    #set detault balues for the optional arguments\r\n    hostname = socket.gethostname()\r\n    localhost_ip = socket.gethostbyname(hostname)\r\n\r\n    parser = argparse.ArgumentParser(description=\"simulate SNMP devices\")\r\n    parser.add_argument('-d',\"--no_of_devices\",type=int,help=\"number of snmp devices\")\r\n    args = parser.parse_args()\r\n    return args\r\n\r\ndef create_snmp(**kwargs):\r\n    num_devices = kwargs.get('no_of_devices')\r\n    faker = Faker()\r\n    port_list = []\r\n    for i in range(num_devices):\r\n        try:\r\n            port = get_open_port(host=\"127.0.0.1\")\r\n            port_list.append(port)\r\n            os.system(\"snmpsimd.py --data-dir=.\/data --agent-udpv4-endpoint=127.0.0.1:%s &amp;\"%(port))\r\n        except:\r\n            raise ValueError(\"error in running the snmp device 'snmpsimd.py' command\")\r\n\r\n        #ip_list = [faker.ipv4() for port in port_list]\r\n        #self.update_iptables(port_list,ip_list)\r\n\r\n    return port_list\r\n\r\ndef get_open_port(host):\r\n    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r\n    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\r\n    s.bind((host,0))\r\n    s.listen(1)\r\n    port = s.getsockname()[1]\r\n    s.close()\r\n    \r\n    return port\r\n\r\ndef update_iptables(self,port_list,ip_list):\r\n    for port,ip in zip(port_list,ip_list):\r\n        try:\r\n            os.system(\"sudo iptables -t nat -A OUTPUT -p udp -d %s --dport 1:65535 -j DNAT --to-destination 127.0.0.1:%s  &amp;\"%(ip,port))\r\n            os.system(\"sudo iptables -t nat -A OUTPUT -p icmp -d %s  --icmp-type echo-request -j DNAT --to-destination 127.0.0.1:%s  &amp;\"%(ip,port))\r\n            os.system(\"sudo iptables -t nat -A OUTPUT -p tcp -d %s  --dport 22 -j DNAT --to-destination 127.0.0.1:%s  &amp;\"%(ip,port))\r\n        except OSError:\r\n            raise ValueError('error in running the iptable update commands')\r\n\r\n\r\nif __name__==\"__main__\":\r\n    args = parse_args()\r\n    print(\"Creating SNMP simulated devices................\")\r\n    port_list = create_snmp(no_of_devices=args.no_of_devices)\r\n    print(\"snmp devices are being run on these ports:\",port_list)\r\n<\/pre>\n<p><strong>FAKE SWITCHES<\/strong><\/p>\n<ul>\n<li><strong>fake-switches Installation<\/strong><\/li>\n<\/ul>\n<pre>$pip install fake-switches<\/pre>\n<ul>\n<li><strong>Run fake-switches<\/strong><\/li>\n<\/ul>\n<pre>$fake-switches --model cisco_2960_24TT_L --hostname 127.0.0.1 --username root --password root --listen-port 8087 &amp;<\/pre>\n<p>You can connect to switch &amp; check further details\u00a0<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/02\/fake-switches-e1581493795399.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-12375 size-full\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/02\/fake-switches-e1581493953771.png\" alt=\"\" width=\"833\" height=\"170\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/02\/fake-switches-e1581493953771.png 833w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/02\/fake-switches-e1581493953771-300x61.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/02\/fake-switches-e1581493953771-768x157.png 768w\" sizes=\"auto, (max-width: 833px) 100vw, 833px\" \/><\/a><\/p>\n<p>Please note these fake switches are not SNMP agent installed by default<\/p>\n<p><strong>Following are some references for more details on snmp-simulator &amp; fake-switches modules<\/strong><\/p>\n<p>https:\/\/github.com\/internap\/fake-switches<\/p>\n<p>https:\/\/github.com\/etingof\/snmpsim<\/p>\n<p>If you would like to assign a fake IP address for the simulated devices with in the local host , then refer this blog<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"GHepPcXyXH\"><p><a href=\"https:\/\/qxf2.com\/blog\/rerouting-outbound-traffic-to-a-simulated-device-on-localhost\/\">Rerouting outbound traffic to a simulated device on localhost<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"https:\/\/qxf2.com\/blog\/rerouting-outbound-traffic-to-a-simulated-device-on-localhost\/embed\/#?secret=GHepPcXyXH\" data-secret=\"GHepPcXyXH\" width=\"600\" height=\"338\" title=\"&#8220;Rerouting outbound traffic to a simulated device on localhost&#8221; &#8212; Qxf2 BLOG\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why and Who is this POST for? There can be situations\u00a0where QA or Developers need real hardware devices for suppose when developers need to test their code or when QA has to do end to end testing with it. I am one of those QA&#8217;s who was wondering how to simulate the hardware devices like linux machine, cisco routers and [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[224,223,61],"tags":[],"class_list":["post-12069","post","type-post","status-publish","format-standard","hentry","category-hardware-devices","category-simulate-devices","category-testing"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/12069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=12069"}],"version-history":[{"count":103,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/12069\/revisions"}],"predecessor-version":[{"id":12556,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/12069\/revisions\/12556"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=12069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=12069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=12069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}