2020-04-18 08:11:12 -07:00
|
|
|
"""
|
|
|
|
This is a standalone script to run a small switch based scenario and will not
|
|
|
|
interact with the GUI.
|
|
|
|
"""
|
|
|
|
|
2019-10-04 12:36:15 -07:00
|
|
|
import logging
|
2018-04-20 17:00:47 -07:00
|
|
|
|
2018-05-01 10:40:25 -07:00
|
|
|
from core.emulator.coreemu import CoreEmu
|
|
|
|
from core.emulator.emudata import IpPrefixes
|
2019-09-10 14:20:51 -07:00
|
|
|
from core.emulator.enumerations import EventTypes, NodeTypes
|
2019-02-16 09:50:19 -08:00
|
|
|
|
2020-04-18 08:11:12 -07:00
|
|
|
NODES = 2
|
|
|
|
|
2018-04-20 17:00:47 -07:00
|
|
|
|
2020-04-18 08:11:12 -07:00
|
|
|
def main():
|
2018-04-20 17:00:47 -07:00
|
|
|
# ip generator for example
|
2018-04-24 16:24:54 -07:00
|
|
|
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
|
2018-04-20 17:00:47 -07:00
|
|
|
|
|
|
|
# create emulator instance for creating sessions and utility methods
|
|
|
|
coreemu = CoreEmu()
|
2018-04-24 16:24:54 -07:00
|
|
|
session = coreemu.create_session()
|
2018-04-20 17:00:47 -07:00
|
|
|
|
|
|
|
# must be in configuration state for nodes to start, when using "node_add" below
|
2018-04-25 16:33:58 -07:00
|
|
|
session.set_state(EventTypes.CONFIGURATION_STATE)
|
2018-04-20 17:00:47 -07:00
|
|
|
|
|
|
|
# create switch network node
|
2020-04-18 08:11:12 -07:00
|
|
|
switch = session.add_node(_type=NodeTypes.SWITCH, _id=100)
|
2018-04-20 17:00:47 -07:00
|
|
|
|
|
|
|
# create nodes
|
2020-04-18 08:11:12 -07:00
|
|
|
for _ in range(NODES):
|
2018-04-25 10:55:48 -07:00
|
|
|
node = session.add_node()
|
2018-04-24 16:24:54 -07:00
|
|
|
interface = prefixes.create_interface(node)
|
2019-04-26 22:07:51 -07:00
|
|
|
session.add_link(node.id, switch.id, interface_one=interface)
|
2018-04-20 17:00:47 -07:00
|
|
|
|
|
|
|
# instantiate session
|
|
|
|
session.instantiate()
|
|
|
|
|
|
|
|
# get nodes to run example
|
2020-04-18 08:11:12 -07:00
|
|
|
first_node = session.get_node(1)
|
|
|
|
last_node = session.get_node(NODES)
|
|
|
|
address = prefixes.ip4_address(first_node)
|
|
|
|
logging.info("node %s pinging %s", last_node.name, address)
|
|
|
|
output = last_node.cmd(f"ping -c 3 {address}")
|
2019-10-18 10:33:31 -07:00
|
|
|
logging.info(output)
|
2018-04-20 17:00:47 -07:00
|
|
|
|
|
|
|
# shutdown session
|
|
|
|
coreemu.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-04-18 08:11:12 -07:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
2018-04-20 17:00:47 -07:00
|
|
|
main()
|