2020-09-12 07:49:26 +01:00
|
|
|
# Python API
|
2018-07-31 20:57:30 +01:00
|
|
|
|
|
|
|
* Table of Contents
|
2022-05-25 18:51:42 +01:00
|
|
|
{:toc}
|
2018-07-31 20:57:30 +01:00
|
|
|
|
|
|
|
## Overview
|
|
|
|
|
2020-04-09 22:34:52 +01:00
|
|
|
Writing your own Python scripts offers a rich programming environment with
|
2020-09-12 19:22:58 +01:00
|
|
|
complete control over all aspects of the emulation.
|
|
|
|
|
|
|
|
The scripts need to be ran with root privileges because they create new network
|
2020-04-09 22:34:52 +01:00
|
|
|
namespaces. In general, a CORE Python script does not connect to the CORE
|
|
|
|
daemon, in fact the *core-daemon* is just another Python script that uses
|
2020-09-12 19:22:58 +01:00
|
|
|
the CORE Python modules and exchanges messages with the GUI.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
### Node Models
|
|
|
|
|
|
|
|
When creating nodes of type `core.nodes.base.CoreNode` these are the default models
|
|
|
|
and the services they map to.
|
|
|
|
|
|
|
|
* mdr
|
2022-05-25 18:51:42 +01:00
|
|
|
* zebra, OSPFv3MDR, IPForward
|
2020-09-12 19:22:58 +01:00
|
|
|
* PC
|
2022-05-25 18:51:42 +01:00
|
|
|
* DefaultRoute
|
2020-09-12 19:22:58 +01:00
|
|
|
* router
|
2022-05-25 18:51:42 +01:00
|
|
|
* zebra, OSPFv2, OSPFv3, IPForward
|
2020-09-12 19:22:58 +01:00
|
|
|
* host
|
2022-05-25 18:51:42 +01:00
|
|
|
* DefaultRoute, SSH
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
### Interface Helper
|
|
|
|
|
|
|
|
There is an interface helper class that can be leveraged for convenience
|
|
|
|
when creating interface data for nodes. Alternatively one can manually create
|
|
|
|
a `core.emulator.data.InterfaceData` class instead with appropriate information.
|
|
|
|
|
|
|
|
Manually creating interface data:
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
```python
|
|
|
|
from core.emulator.data import InterfaceData
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# id is optional and will set to the next available id
|
|
|
|
# name is optional and will default to eth<id>
|
|
|
|
# mac is optional and will result in a randomly generated mac
|
|
|
|
iface_data = InterfaceData(
|
|
|
|
id=0,
|
|
|
|
name="eth0",
|
|
|
|
ip4="10.0.0.1",
|
|
|
|
ip4_mask=24,
|
|
|
|
ip6="2001::",
|
|
|
|
ip6_mask=64,
|
|
|
|
)
|
|
|
|
```
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
Leveraging the interface prefixes helper class:
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2018-07-31 20:57:30 +01:00
|
|
|
```python
|
2020-09-12 19:22:58 +01:00
|
|
|
from core.emulator.data import IpPrefixes
|
|
|
|
|
|
|
|
ip_prefixes = IpPrefixes(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
|
|
|
|
# node is used to get an ip4/ip6 address indexed from within the above prefixes
|
|
|
|
# name is optional and would default to eth<id>
|
|
|
|
# mac is optional and will result in a randomly generated mac
|
|
|
|
iface_data = ip_prefixes.create_iface(
|
|
|
|
node=node, name="eth0", mac="00:00:00:00:aa:00"
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
### Listening to Events
|
|
|
|
|
2020-09-13 17:26:48 +01:00
|
|
|
Various events that can occur within a session can be listened to.
|
|
|
|
|
|
|
|
Event types:
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-13 17:26:48 +01:00
|
|
|
* session - events for changes in session state and mobility start/stop/pause
|
|
|
|
* node - events for node movements and icon changes
|
|
|
|
* link - events for link configuration changes and wireless link add/delete
|
|
|
|
* config - configuration events when legacy gui joins a session
|
|
|
|
* exception - alert/error events
|
|
|
|
* file - file events when the legacy gui joins a session
|
|
|
|
|
|
|
|
```python
|
|
|
|
def event_listener(event):
|
|
|
|
print(event)
|
|
|
|
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-13 17:26:48 +01:00
|
|
|
# add an event listener to event type you want to listen to
|
|
|
|
# each handler will receive an object unique to that type
|
|
|
|
session.event_handlers.append(event_listener)
|
|
|
|
session.exception_handlers.append(event_listener)
|
|
|
|
session.node_handlers.append(event_listener)
|
|
|
|
session.link_handlers.append(event_listener)
|
|
|
|
session.file_handlers.append(event_listener)
|
|
|
|
session.config_handlers.append(event_listener)
|
|
|
|
```
|
2020-05-21 06:14:03 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
### Configuring Links
|
2020-05-21 06:14:03 +01:00
|
|
|
|
2020-09-13 17:16:30 +01:00
|
|
|
Links can be configured at the time of creation or during runtime.
|
|
|
|
|
2020-09-14 16:41:38 +01:00
|
|
|
Currently supported configuration options:
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-14 16:41:38 +01:00
|
|
|
* bandwidth (bps)
|
|
|
|
* delay (us)
|
|
|
|
* dup (%)
|
|
|
|
* jitter (us)
|
|
|
|
* loss (%)
|
|
|
|
|
2020-09-13 17:16:30 +01:00
|
|
|
```python
|
|
|
|
from core.emulator.data import LinkOptions
|
|
|
|
|
|
|
|
# configuring when creating a link
|
|
|
|
options = LinkOptions(
|
|
|
|
bandwidth=54_000_000,
|
|
|
|
delay=5000,
|
|
|
|
dup=5,
|
|
|
|
loss=5.5,
|
|
|
|
jitter=0,
|
|
|
|
)
|
|
|
|
session.add_link(n1_id, n2_id, iface1_data, iface2_data, options)
|
|
|
|
|
|
|
|
# configuring during runtime
|
|
|
|
session.update_link(n1_id, n2_id, iface1_id, iface2_id, options)
|
|
|
|
```
|
2020-09-12 19:22:58 +01:00
|
|
|
|
|
|
|
### Peer to Peer Example
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
```python
|
|
|
|
# required imports
|
2018-07-31 20:57:30 +01:00
|
|
|
from core.emulator.coreemu import CoreEmu
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.emulator.data import IpPrefixes
|
2019-06-11 20:05:04 +01:00
|
|
|
from core.emulator.enumerations import EventTypes
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.nodes.base import CoreNode, Position
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# ip nerator for example
|
|
|
|
ip_prefixes = IpPrefixes(ip4_prefix="10.0.0.0/24")
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# create emulator instance for creating sessions and utility methods
|
|
|
|
coreemu = CoreEmu()
|
|
|
|
session = coreemu.create_session()
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# must be in configuration state for nodes to start, when using "node_add" below
|
|
|
|
session.set_state(EventTypes.CONFIGURATION_STATE)
|
2020-05-21 06:14:03 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# create nodes
|
2022-05-25 18:51:42 +01:00
|
|
|
position = Position(x=100, y=100)
|
|
|
|
n1 = session.add_node(CoreNode, position=position)
|
|
|
|
position = Position(x=300, y=100)
|
|
|
|
n2 = session.add_node(CoreNode, position=position)
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# link nodes together
|
|
|
|
iface1 = ip_prefixes.create_iface(n1)
|
|
|
|
iface2 = ip_prefixes.create_iface(n2)
|
|
|
|
session.add_link(n1.id, n2.id, iface1, iface2)
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# start session
|
|
|
|
session.instantiate()
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# do whatever you like here
|
|
|
|
input("press enter to shutdown")
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# stop session
|
|
|
|
session.shutdown()
|
|
|
|
```
|
2020-05-21 06:14:03 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
### Switch/Hub Example
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
```python
|
|
|
|
# required imports
|
|
|
|
from core.emulator.coreemu import CoreEmu
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.emulator.data import IpPrefixes
|
2020-09-12 19:22:58 +01:00
|
|
|
from core.emulator.enumerations import EventTypes
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.nodes.base import CoreNode, Position
|
2020-09-12 19:22:58 +01:00
|
|
|
from core.nodes.network import SwitchNode
|
2020-05-21 06:14:03 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# ip nerator for example
|
|
|
|
ip_prefixes = IpPrefixes(ip4_prefix="10.0.0.0/24")
|
2020-05-21 06:14:03 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# create emulator instance for creating sessions and utility methods
|
|
|
|
coreemu = CoreEmu()
|
|
|
|
session = coreemu.create_session()
|
2020-05-21 06:14:03 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# must be in configuration state for nodes to start, when using "node_add" below
|
|
|
|
session.set_state(EventTypes.CONFIGURATION_STATE)
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# create switch
|
2022-05-25 18:51:42 +01:00
|
|
|
position = Position(x=200, y=200)
|
|
|
|
switch = session.add_node(SwitchNode, position=position)
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# create nodes
|
2022-05-25 18:51:42 +01:00
|
|
|
position = Position(x=100, y=100)
|
|
|
|
n1 = session.add_node(CoreNode, position=position)
|
|
|
|
position = Position(x=300, y=100)
|
|
|
|
n2 = session.add_node(CoreNode, position=position)
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# link nodes to switch
|
|
|
|
iface1 = ip_prefixes.create_iface(n1)
|
|
|
|
session.add_link(n1.id, switch.id, iface1)
|
|
|
|
iface1 = ip_prefixes.create_iface(n2)
|
|
|
|
session.add_link(n2.id, switch.id, iface1)
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# start session
|
|
|
|
session.instantiate()
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# do whatever you like here
|
|
|
|
input("press enter to shutdown")
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# stop session
|
|
|
|
session.shutdown()
|
2018-07-31 20:57:30 +01:00
|
|
|
```
|
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
### WLAN Example
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
```python
|
|
|
|
# required imports
|
|
|
|
from core.emulator.coreemu import CoreEmu
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.emulator.data import IpPrefixes
|
2020-09-12 19:22:58 +01:00
|
|
|
from core.emulator.enumerations import EventTypes
|
|
|
|
from core.location.mobility import BasicRangeModel
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.nodes.base import CoreNode, Position
|
2020-09-12 19:22:58 +01:00
|
|
|
from core.nodes.network import WlanNode
|
2020-04-09 22:34:52 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# ip nerator for example
|
|
|
|
ip_prefixes = IpPrefixes(ip4_prefix="10.0.0.0/24")
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# create emulator instance for creating sessions and utility methods
|
|
|
|
coreemu = CoreEmu()
|
2018-07-31 20:57:30 +01:00
|
|
|
session = coreemu.create_session()
|
2020-09-12 19:22:58 +01:00
|
|
|
|
|
|
|
# must be in configuration state for nodes to start, when using "node_add" below
|
|
|
|
session.set_state(EventTypes.CONFIGURATION_STATE)
|
|
|
|
|
|
|
|
# create wlan
|
2022-05-25 18:51:42 +01:00
|
|
|
position = Position(x=200, y=200)
|
|
|
|
wlan = session.add_node(WlanNode, position=position)
|
2020-09-12 19:22:58 +01:00
|
|
|
|
|
|
|
# create nodes
|
2022-05-25 18:51:42 +01:00
|
|
|
options = CoreNode.create_options()
|
|
|
|
options.model = "mdr"
|
|
|
|
position = Position(x=100, y=100)
|
|
|
|
n1 = session.add_node(CoreNode, position=position, options=options)
|
|
|
|
position = Position(x=300, y=100)
|
|
|
|
n2 = session.add_node(CoreNode, position=position, options=options)
|
2020-09-12 19:22:58 +01:00
|
|
|
|
|
|
|
# configuring wlan
|
|
|
|
session.mobility.set_model_config(wlan.id, BasicRangeModel.name, {
|
|
|
|
"range": "280",
|
|
|
|
"bandwidth": "55000000",
|
|
|
|
"delay": "6000",
|
|
|
|
"jitter": "5",
|
|
|
|
"error": "5",
|
|
|
|
})
|
|
|
|
|
|
|
|
# link nodes to wlan
|
|
|
|
iface1 = ip_prefixes.create_iface(n1)
|
|
|
|
session.add_link(n1.id, wlan.id, iface1)
|
|
|
|
iface1 = ip_prefixes.create_iface(n2)
|
|
|
|
session.add_link(n2.id, wlan.id, iface1)
|
|
|
|
|
|
|
|
# start session
|
|
|
|
session.instantiate()
|
|
|
|
|
|
|
|
# do whatever you like here
|
|
|
|
input("press enter to shutdown")
|
|
|
|
|
|
|
|
# stop session
|
|
|
|
session.shutdown()
|
2018-07-31 20:57:30 +01:00
|
|
|
```
|
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
### EMANE Example
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
For EMANE you can import and use one of the existing models and
|
|
|
|
use its name for configuration.
|
|
|
|
|
|
|
|
Current models:
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
* core.emane.ieee80211abg.EmaneIeee80211abgModel
|
|
|
|
* core.emane.rfpipe.EmaneRfPipeModel
|
|
|
|
* core.emane.tdma.EmaneTdmaModel
|
|
|
|
* core.emane.bypass.EmaneBypassModel
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
Their configurations options are driven dynamically from parsed EMANE manifest files
|
|
|
|
from the installed version of EMANE.
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
Options and their purpose can be found at the [EMANE Wiki](https://github.com/adjacentlink/emane/wiki).
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
If configuring EMANE global settings or model mac/phy specific settings, any value not provided
|
|
|
|
will use the defaults. When no configuration is used, the defaults are used.
|
2018-07-31 20:57:30 +01:00
|
|
|
|
|
|
|
```python
|
2020-09-12 19:22:58 +01:00
|
|
|
# required imports
|
2021-05-07 18:40:18 +01:00
|
|
|
from core.emane.models.ieee80211abg import EmaneIeee80211abgModel
|
2020-09-12 19:22:58 +01:00
|
|
|
from core.emane.nodes import EmaneNet
|
|
|
|
from core.emulator.coreemu import CoreEmu
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.emulator.data import IpPrefixes
|
2020-09-12 19:22:58 +01:00
|
|
|
from core.emulator.enumerations import EventTypes
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.nodes.base import CoreNode, Position
|
2020-09-12 19:22:58 +01:00
|
|
|
|
|
|
|
# ip nerator for example
|
|
|
|
ip_prefixes = IpPrefixes(ip4_prefix="10.0.0.0/24")
|
|
|
|
|
|
|
|
# create emulator instance for creating sessions and utility methods
|
2018-07-31 20:57:30 +01:00
|
|
|
coreemu = CoreEmu()
|
|
|
|
session = coreemu.create_session()
|
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
# location information is required to be set for emane
|
|
|
|
session.location.setrefgeo(47.57917, -122.13232, 2.0)
|
|
|
|
session.location.refscale = 150.0
|
|
|
|
|
|
|
|
# must be in configuration state for nodes to start, when using "node_add" below
|
|
|
|
session.set_state(EventTypes.CONFIGURATION_STATE)
|
|
|
|
|
|
|
|
# create emane
|
2022-05-25 18:51:42 +01:00
|
|
|
options = EmaneNet.create_options()
|
|
|
|
options.emane_model = EmaneIeee80211abgModel.name
|
|
|
|
position = Position(x=200, y=200)
|
|
|
|
emane = session.add_node(EmaneNet, position=position, options=options)
|
2020-09-12 19:22:58 +01:00
|
|
|
|
|
|
|
# create nodes
|
2022-05-25 18:51:42 +01:00
|
|
|
options = CoreNode.create_options()
|
|
|
|
options.model = "mdr"
|
|
|
|
position = Position(x=100, y=100)
|
|
|
|
n1 = session.add_node(CoreNode, position=position, options=options)
|
|
|
|
position = Position(x=300, y=100)
|
|
|
|
n2 = session.add_node(CoreNode, position=position, options=options)
|
2020-09-12 19:22:58 +01:00
|
|
|
|
|
|
|
# configure general emane settings
|
|
|
|
config = session.emane.get_configs()
|
|
|
|
config.update({
|
2022-05-25 18:51:42 +01:00
|
|
|
"eventservicettl": "2"
|
2020-09-12 19:22:58 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
# configure emane model settings
|
|
|
|
# using a dict mapping currently support values as strings
|
|
|
|
session.emane.set_model_config(emane.id, EmaneIeee80211abgModel.name, {
|
2022-05-25 18:51:42 +01:00
|
|
|
"unicastrate": "3",
|
2020-09-12 19:22:58 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
# link nodes to emane
|
|
|
|
iface1 = ip_prefixes.create_iface(n1)
|
|
|
|
session.add_link(n1.id, emane.id, iface1)
|
|
|
|
iface1 = ip_prefixes.create_iface(n2)
|
|
|
|
session.add_link(n2.id, emane.id, iface1)
|
|
|
|
|
|
|
|
# start session
|
|
|
|
session.instantiate()
|
|
|
|
|
|
|
|
# do whatever you like here
|
|
|
|
input("press enter to shutdown")
|
|
|
|
|
|
|
|
# stop session
|
|
|
|
session.shutdown()
|
|
|
|
```
|
|
|
|
|
|
|
|
EMANE Model Configuration:
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
```python
|
|
|
|
from core import utils
|
|
|
|
|
2022-03-11 19:04:01 +00:00
|
|
|
# standardized way to retrieve an appropriate config id
|
|
|
|
# iface id can be omitted, to allow a general configuration for a model, per node
|
2020-09-12 19:22:58 +01:00
|
|
|
config_id = utils.iface_config_id(node.id, iface_id)
|
2022-03-11 19:04:01 +00:00
|
|
|
# set emane configuration for the config id
|
|
|
|
session.emane.set_config(config_id, EmaneIeee80211abgModel.name, {
|
2020-09-12 19:22:58 +01:00
|
|
|
"unicastrate": "3",
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
## Configuring a Service
|
|
|
|
|
2020-09-13 20:08:29 +01:00
|
|
|
Services help generate and run bash scripts on nodes for a given purpose.
|
|
|
|
|
|
|
|
Configuring the files of a service results in a specific hard coded script being
|
|
|
|
generated, instead of the default scripts, that may leverage dynamic generation.
|
|
|
|
|
2020-09-14 16:41:38 +01:00
|
|
|
The following features can be configured for a service:
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-14 16:41:38 +01:00
|
|
|
* configs - files that will be generated
|
|
|
|
* dirs - directories that will be mounted unique to the node
|
|
|
|
* startup - commands to run start a service
|
|
|
|
* validate - commands to run to validate a service
|
|
|
|
* shutdown - commands to run to stop a service
|
|
|
|
|
2020-09-13 20:08:29 +01:00
|
|
|
Editing service properties:
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-13 20:08:29 +01:00
|
|
|
```python
|
|
|
|
# configure a service, for a node, for a given session
|
|
|
|
session.services.set_service(node_id, service_name)
|
|
|
|
service = session.services.get_service(node_id, service_name)
|
|
|
|
service.configs = ("file1.sh", "file2.sh")
|
|
|
|
service.dirs = ("/etc/node",)
|
|
|
|
service.startup = ("bash file1.sh",)
|
|
|
|
service.validate = ()
|
|
|
|
service.shutdown = ()
|
|
|
|
```
|
|
|
|
|
2020-09-14 16:41:38 +01:00
|
|
|
When editing a service file, it must be the name of `config`
|
|
|
|
file that the service will generate.
|
|
|
|
|
2020-09-13 20:08:29 +01:00
|
|
|
Editing a service file:
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-13 20:08:29 +01:00
|
|
|
```python
|
|
|
|
# to edit the contents of a generated file you can specify
|
|
|
|
# the service, the file name, and its contents
|
|
|
|
session.services.set_service_file(
|
|
|
|
node_id,
|
|
|
|
service_name,
|
|
|
|
file_name,
|
|
|
|
"echo hello",
|
|
|
|
)
|
|
|
|
```
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
## File Examples
|
|
|
|
|
2020-09-13 20:08:29 +01:00
|
|
|
File versions of the network examples can be found
|
2022-12-05 09:15:09 +00:00
|
|
|
[here](https://github.com/coreemu/core/tree/master/package/examples/python).
|
2020-09-12 19:22:58 +01:00
|
|
|
|
|
|
|
## Executing Scripts from GUI
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
To execute a python script from a GUI you need have the following.
|
|
|
|
|
|
|
|
The builtin name check here to know it is being executed from the GUI, this can
|
|
|
|
be avoided if your script does not use a name check.
|
2022-05-25 18:51:42 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
```python
|
|
|
|
if __name__ in ["__main__", "__builtin__"]:
|
|
|
|
main()
|
2018-07-31 20:57:30 +01:00
|
|
|
```
|
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
A script can add sessions to the core-daemon. A global *coreemu* variable is
|
|
|
|
exposed to the script pointing to the *CoreEmu* object.
|
2018-07-31 20:57:30 +01:00
|
|
|
|
2020-09-12 19:22:58 +01:00
|
|
|
The example below has a fallback to a new CoreEmu object, in the case you would
|
|
|
|
like to run the script standalone, outside of the core-daemon.
|
2018-07-31 20:57:30 +01:00
|
|
|
|
|
|
|
```python
|
2020-09-12 19:22:58 +01:00
|
|
|
coreemu = globals().get("coreemu", CoreEmu())
|
2018-07-31 20:57:30 +01:00
|
|
|
session = coreemu.create_session()
|
|
|
|
```
|