MIMIC Python API Guide

  1. Table of Contents
  2. Requirements
  3. The MIMIC Python API is an optional Update Wizard package, and requires the use of the Python 2.7 or later. Python is already installed on Linux platforms, but will have to be installed on the other platforms. MIMIC has been tested with Python 2.7.3 and upto 3.8 on Linux, and Python 2.7 and 3.5.1 on Windows.

  4. Class Reference
  5. The Python API is modelled after the Java API. All classes and methods have the same name as in Java, except that Python programming constructs are used. Click here for the generated Java reference documentation.

  6. Tcl to Python Mapping
  7. This section maps each Tcl mimicsh command to the equivalent call in the native Python API — the object model (modelled after the Java API). You create a Client, open a Session, get an Agent from the session and a Valuespace from the agent, then call methods on those objects:

        session = client.open_session(host, port)   # a Session
        agent   = session.get_agent(1)              # an Agent
        vspace  = agent.get_valuespace()            # a Valuespace
    
    Unlike the Tcl shell there is no "assigned/current" session or agent — you hold each object in a variable and call methods on it. (The dotted mimic.agent.... syntax comes from a separate Tcl-compatibility shim, which can be disabled; the tables below give the native calls.)

    4.1 Session Commands

    Sessions are managed by a Client; open_session returns the Session object you keep and use.
    TclPython (native)
    mimic session open host portsession = client.open_session(host, port)
    mimic session close sessionclient.close_session(session)
    mimic session listclient.list_sessions()
    mimic session propertiessession.get_host(), session.get_port(), session.get_user()
    mimic session assign sessionno native equivalent (Tcl shim only)
    mimic session assignedno native equivalent (Tcl shim only)

    4.2 Global Get

    TclPython (native)
    mimic get cfgfilesession.get_cfgfile()
    mimic get maxsession.get_max()
    mimic get lastsession.get_last()
    mimic get versionsession.get_version()
    mimic get clientssession.get_clients()
    mimic get configured_listsession.get_configured_list()
    mimic get configured_rangessession.get_configured_ranges()
    mimic get active_listsession.get_active_list()
    mimic get active_rangessession.get_active_ranges()
    mimic get active_data_listsession.get_active_data_list()
    mimic get changed_config_listsession.get_changed_config_list()
    mimic get changed_state_listsession.get_changed_state_list()
    mimic get logsession.get_log()
    mimic get interfacessession.get_interfaces()
    mimic get cfgfile_changedsession.get_cfg_file_changed()
    mimic get privdirsession.get_sim_privdir()
    mimic get protocolssession.get_protocols()
    mimic get productsession.get_product()
    mimic get netaddrsession.get_netaddr()
    mimic get netdevsession.get_netdev()
    mimic get licensingsession.get_licensing()
    mimic get threadssession.get_threads()

    4.3 Global Set / Load / Diag

    TclPython (native)
    mimic set log logfilesession.set_log(logfile)
    mimic load "agent.cfg"session.cfg_load("agent.cfg")
    mimic diag Nsession.diag(N)

    4.4 Configuration Commands

    TclPython (native)
    mimic cfg new [agents]session.cfg_new(agents="")
    mimic cfg load file [agents [start [merge]]]session.cfg_load(file, agents="", start="", merge="")
    mimic cfg save [agents]session.cfg_save(agents="")
    mimic cfg saveas file [agents]session.cfg_saveas(file, agents="")

    4.5 Agent Commands

    Get the Agent with session.get_agent(N) and call methods on it; there is no assign/assigned. The multi-agent mget/mset are Session methods (they take an agent list).
    TclPython (native)
    mimic agent assign Nagent = session.get_agent(N) (no "assign" — hold the Agent)
    mimic agent assignedno native equivalent; agent.get_agent_no() gives the number
    mimic agent startagent.start()
    mimic agent stopagent.stop()
    mimic agent pause [time]agent.pause_now() / agent.pause_at(time)
    mimic agent haltagent.halt()
    mimic agent resumeagent.resume()
    mimic agent add addr mibsagent.configure(addr, mibs)
    mimic agent removeagent.remove()
    mimic agent get paramagent.get_<param>() (see param map below)
    mimic agent set param valueagent.set_<param>(value) (see param map below)
    mimic agent mget param {N1 N2 N3}session.agent_mget(param, [N1,N2,N3])
    mimic agent mset param N1 V1 N2 V2 ...session.agent_mset(param, [N1,N2], [V1,V2])

    Agent get / set param name mapping (Tcl name to native Agent method):
    Tcl paramget methodset method
    "interface"get_interface()set_interface(val)
    "host"get_host()set_host(val)
    "mask"get_mask()set_mask(val)
    "port"get_port()set_port(val)
    "read"get_read_community()set_read_community(val)
    "write"get_write_community()set_write_community(val)
    "mibs"get_mibs()set_mibs(val)
    "sim"get_sim()(read-only)
    "scen"get_scen()(read-only)
    "pdusize"get_pdusize()set_pdusize(val)
    "protocol"get_protocols()set_protocols(val)
    "delay"get_delay()set_delay(val)
    "start"get_starttime()set_starttime(val)
    "state"get_state()(read-only)
    "statistics"get_statistics()(read-only)
    "drops"get_drops()set_drops(val)
    "changed"get_changed()(read-only)
    "config_changed"get_config_changed()(read-only)
    "state_changed"get_state_changed()(read-only)
    "trace"get_trace()set_trace(val)
    "validate"get_validate()set_validate(val)
    "owner"get_owner()(read-only)
    "privdir"get_privdir()(read-only)
    "oiddir"get_oiddir()set_oiddir(val)
    "inform_timeout"get_inform_timeout()set_inform_timeout(val)
    "inform_retries"get_inform_retries()set_inform_retries(val)
    "num_starts"get_num_starts()(read-only)

    4.6 Agent IP Alias Commands

    TclPython (native)
    mimic agent ipalias listagent.list_ipaliases()
    mimic agent ipalias add "addr,port" [mask [iface]]agent.add_ipalias(address, port=161, mask="", interface="")
    mimic agent ipalias delete "addr,port"agent.del_ipalias(address, port=161)
    mimic agent ipalias start "addr,port"agent.start_ipalias(address, port=161)
    mimic agent ipalias stop "addr,port"agent.stop_ipalias(address, port=161)

    Note: natively the address and port are separate arguments (not the combined "addr,port" string the Tcl form uses) — e.g. agent.add_ipalias("192.9.216.1", 161).

    4.7 Agent Store Commands

    TclPython (native)
    mimic agent store listagent.agent_store_list()
    mimic agent store exists varagent.agent_store_exists(var)
    mimic agent store persists varagent.agent_store_persists(var)
    mimic agent store get varagent.agent_store_get(var)
    mimic agent store set var val persistagent.agent_store_set(var, val, persist)
    mimic agent store append var val persistagent.agent_store_append(var, val, persist)
    mimic agent store unset varagent.agent_store_unset(var)
    mimic agent store copy other_agent_noagent.agent_store_copy(other)

    4.8 Agent Trap Commands

    TclPython (native)
    mimic agent trap listagent.trap_list()
    mimic agent trap config listagent.trap_config_list()
    mimic agent trap config add dest portagent.trap_config_add(dest, port)
    mimic agent trap config delete dest portagent.trap_config_del(dest, port)

    Note: agent.trap_config_delete(dest, port) is an accepted alias of trap_config_del() — both spellings work.

    4.9 Value Space Commands

    Value commands operate on a Valuespace obtained from the agent: vspace = agent.get_valuespace().
    TclPython (native)
    mimic value posvspace.get_pos()
    mimic value pos ifEntryvspace.set_pos("ifEntry")
    mimic value listvspace.get_objects()
    mimic value add ifEntry 510vspace.add("ifEntry", 510)
    mimic value remove ifEntry 510vspace.remove("ifEntry", 510)
    mimic value instances ifDescrvspace.get_instances("ifDescr")
    mimic value variables ifInOctets 1vspace.get_variables("ifInOctets", 1)
    mimic value get sysDescr 0 vvspace.get_value("sysDescr", 0, "v")
    mimic value set sysContact 0 v Sucharuvspace.set_value("sysContact", "0", "v", "Sucharu")
    mimic value mget obj inst var obj inst var ...vspace.mget_value(objects, instances, variables)
    mimic value mset obj inst var val obj inst var val ...vspace.mset_value(objects, instances, variables, values)
    mimic value unset obj inst varvspace.unset_value(obj, inst, var)
    mimic value munset obj inst var obj inst var ...vspace.munset_value(objects, instances, variables)
    mimic value eval obj instvspace.eval_value(obj, inst)
    mimic value meval obj inst obj inst ...vspace.meval_value(objects, instances)
    mimic value oid namevspace.get_oid(object)
    mimic value name oidvspace.get_name(Oid(oid))
    mimic value split oidvspace.split_oid(Oid(oid))
    mimic value mib objnamevspace.get_mib(objname)
    mimic value info objnamevspace.get_info(objname)
    mimic value minfo obj1 obj2 ...vspace.get_minfo([obj1, obj2, ...])
    mimic value state get objnamevspace.get_state(objname)
    mimic value state set objname valvspace.set_state(objname, val)

    Notes: the multi- calls (mget/mset/munset/meval) take parallel lists(objects, instances, variables[, values]) — not flat interleaved args; OID arguments/returns use the Oid class.

    4.10 Global Store Commands

    TclPython (native)
    mimic store listsession.store_list()
    mimic store exists varsession.store_exists(var)
    mimic store persists varsession.store_persists(var)
    mimic store get varsession.store_get(var)
    mimic store set var val persistsession.store_set(var, val, persist)
    mimic store append var val persistsession.store_append(var, val, persist)
    mimic store unset varsession.store_unset(var)
    mimic store mget {var1 var2}session.store_mget([var1, var2])
    mimic store lreplace var index valsession.store_lreplace(var, index, val)
    mimic store mlreplace var1 idx1 val1 var2 idx2 val2 ...session.store_mlreplace([vars], [idxs], [vals])

    4.11 Global Trap Commands

    TclPython (native)
    mimic trap config listsession.trap_config_list()
    mimic trap config add dest portsession.trap_config_add(dest, port)
    mimic trap config delete dest portsession.trap_config_delete(dest, port)

    Note: session.trap_config_del(dest, port) is an accepted alias of trap_config_delete() — both spellings work.

    4.12 Access Control Commands

    TclPython (native)
    mimic access get adminusersession.get_access_adminuser()
    mimic access get admindirsession.get_access_admindir()
    mimic access get acldbsession.get_access_acldb()
    mimic access get enabledsession.get_access_enabled()
    mimic access set enabled valsession.set_access_enabled(val)
    mimic access set adminuser usersession.set_access_adminuser(user)
    mimic access set admindir dirsession.set_access_admindir(dir)
    mimic access set acldb dbsession.set_access_acldb(db)
    mimic access listsession.list_access()
    mimic access add user agents masksession.add_access(user, agents, mask)
    mimic access del usersession.del_access(user)
    mimic access save filesession.save_access(file)
    mimic access load filesession.load_access(file)

    4.13 Protocol Message Commands

    Each protocol module accepts its own set of protocol msg sub-commands, but they all map to Python by one uniform rule:

    • Session level — mimic protocol msg protocol command maps to session.protocol_msg(protocol, command).
    • Agent level — mimic agent protocol msg protocol command maps to agent.protocol_msg(protocol, command).
    In both cases the Tcl protocol name becomes the first argument (verbatim, including its case — e.g. snmpv3 vs. TELNET) and the remaining Tcl words become the second (string) argument.

    The table below gives one representative command per module; it is not exhaustive. For the complete command set of a module, follow its reference link.
    ModuleRepresentative (Tcl)Python (native)Full command list
    snmpv3mimic protocol msg snmpv3 get argssession.protocol_msg("snmpv3", "get args")reference
    TELNETmimic protocol msg TELNET get argssession.protocol_msg("TELNET", "get args")reference
    SSHmimic protocol msg SSH get argssession.protocol_msg("SSH", "get args")reference
    NETFLOWmimic protocol msg NETFLOW get argssession.protocol_msg("NETFLOW", "get args")reference
    SFLOWmimic protocol msg SFLOW get argssession.protocol_msg("SFLOW", "get args")reference
    WEBmimic protocol msg WEB get argssession.protocol_msg("WEB", "get args")reference
    MQTTmimic protocol msg MQTT get argssession.protocol_msg("MQTT", "get args")reference
    IPMImimic protocol msg IPMI get argssession.protocol_msg("IPMI", "get args")reference
    DHCPmimic protocol msg DHCP get argssession.protocol_msg("DHCP", "get args")reference
    TFTPmimic protocol msg TFTP get argssession.protocol_msg("TFTP", "get args")reference

  8. Hello, World!
  9. The Hello, World! program for the Python MIMIC API is these commands typed into the mimicsh.py shell:

    % ./mimicsh.py
    MIMIC Python API shell
    Copyright (c) 2012-2013 Gambit Communications, Inc.
    Type "help;" for online help.
    
    mimicsh-python>>> session.cfg_new()
    mimicsh-python>>> session.cfg_load('agent.cfg')
    mimicsh-python>>> agent = session.get_agent(1)
    mimicsh-python>>> agent.start()
    mimicsh-python>>>
    
    

    While you are doing this, notice how your MIMICview reacts to each command - it synchronizes with the simulator to keep up with the latest state. When the first agent turns green, it has synchronized to your state. You have seen that you can control MIMIC from any number of clients using the MIMIC API.

    You can also do this non-interactively by saving these 4 commands into a file, and running mimicsh.py with the --script option:

    % cat > hello.py
    session.cfg_new()
    session.cfg_load('agent.cfg')
    agent = session.get_agent(1)
    agent.start()
    ^D
    
    % ./mimicsh.py --script hello.py
    
    

  10. Examples
  11. These example programs with source code show different usage of the Python API to control MIMIC:

  12. Jython
    • The Python API has been tested against Jython using version 2.2.1 on both Windows Vista and Linux (Fedora Core 8).

    • A performance test of the Python API in a Jython environment shows a significant loss in response time as compared to a native Python enviroment.