Simulate hardware devices and switches for testing

Why and Who is this POST for?

There can be situations where 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’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.

SNMPSIM

I have used following python Modules to simulate the snmp devices / switches

  • snmpsim Installation
$ virtualenv -p /usr/bin/python3 devices
$ source devices/bin/activate
$ pip install snmpsim

please note that snmpsim installation would get a sample simulation data file under the data directory and it is stored in simple plaint-text files having OID|TYPE|VALUE format

  • Run snmp simulators

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

$ snmpsimd.py --data-dir=./data --agent-udpv4-endpoint=127.0.0.1:8089 &
$ snmpsim-command-responder --data-dir=./data --agent-udpv4-endpoint=127.0.0.1:8089 &

If snmpsim commands are not working,  then try to pip install

https://github.com/etingof/snmpsim/archive/master.zip

  • check if simulated devices/agents are up and running

snmpwalk is the application that can be used to query the snmp simulators , you will see no response if the device is not up else it would return snmp data in the response

$ snmpwalk -v1 -c public 127.0.0.1:8089
  • Below is the python code snippet to run simulated devices on linux system
"""Run n number of snmp simulated devices on linux"""
 
import os
import argparse
from faker import Faker
import socket
 
 
def parse_args():
    #set detault balues for the optional arguments
    hostname = socket.gethostname()
    localhost_ip = socket.gethostbyname(hostname)
 
    parser = argparse.ArgumentParser(description="simulate SNMP devices")
    parser.add_argument('-d',"--no_of_devices",type=int,help="number of snmp devices")
    args = parser.parse_args()
    return args
 
def create_snmp(**kwargs):
    num_devices = kwargs.get('no_of_devices')
    faker = Faker()
    port_list = []
    for i in range(num_devices):
        try:
            port = get_open_port(host="127.0.0.1")
            port_list.append(port)
            os.system("snmpsimd.py --data-dir=./data --agent-udpv4-endpoint=127.0.0.1:%s &"%(port))
        except:
            raise ValueError("error in running the snmp device 'snmpsimd.py' command")
 
        #ip_list = [faker.ipv4() for port in port_list]
        #self.update_iptables(port_list,ip_list)
 
    return port_list
 
def get_open_port(host):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((host,0))
    s.listen(1)
    port = s.getsockname()[1]
    s.close()
 
    return port
 
def update_iptables(self,port_list,ip_list):
    for port,ip in zip(port_list,ip_list):
        try:
            os.system("sudo iptables -t nat -A OUTPUT -p udp -d %s --dport 1:65535 -j DNAT --to-destination 127.0.0.1:%s  &"%(ip,port))
            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  &"%(ip,port))
            os.system("sudo iptables -t nat -A OUTPUT -p tcp -d %s  --dport 22 -j DNAT --to-destination 127.0.0.1:%s  &"%(ip,port))
        except OSError:
            raise ValueError('error in running the iptable update commands')
 
 
if __name__=="__main__":
    args = parse_args()
    print("Creating SNMP simulated devices................")
    port_list = create_snmp(no_of_devices=args.no_of_devices)
    print("snmp devices are being run on these ports:",port_list)

FAKE SWITCHES

  • fake-switches Installation
$pip install fake-switches
  • Run fake-switches
$fake-switches --model cisco_2960_24TT_L --hostname 127.0.0.1 --username root --password root --listen-port 8087 &

You can connect to switch & check further details 

Please note these fake switches are not SNMP agent installed by default

Following are some references for more details on snmp-simulator & fake-switches modules

https://github.com/internap/fake-switches

https://github.com/etingof/snmpsim

If you would like to assign a fake IP address for the simulated devices with in the local host , then refer this blog

Rerouting outbound traffic to a simulated device on localhost

Leave a Reply

Your email address will not be published. Required fields are marked *