use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / netns / local_switch_ping.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License version 2 as
8 #    published by the Free Software Foundation;
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 #
20 #
21 # This example must be executed as root: 
22 # $ sudo PYTHONPATH=$PYTHONPATH:src python examples/netns/local_switch_ping.py
23
24 from __future__ import print_function
25
26 from nepi.execution.ec import ExperimentController 
27
28 ec = ExperimentController(exp_id = "netns-local-p2p-ping")
29
30 # Simulation will executed in the local machine
31 node = ec.register_resource("linux::Node")
32 ec.set(node, "hostname", "localhost")
33
34 # Add an emulation resource
35 emu = ec.register_resource("linux::netns::Emulation")
36 ec.set(emu, "verbose", True)
37 ec.register_connection(emu, node)
38
39 # Add two emulated nodes
40 netnsnode1 = ec.register_resource("netns::Node")
41 ec.register_connection(netnsnode1, emu)
42
43 iface1 = ec.register_resource("netns::NodeInterface")
44 ec.register_connection(iface1, netnsnode1)
45
46 ip1 = ec.register_resource("netns::IPv4Address")
47 ec.set(ip1, "ip", "10.0.0.1")
48 ec.set(ip1, "prefix", "30")
49 ec.register_connection(ip1, iface1)
50
51 netnsnode2 = ec.register_resource("netns::Node")
52 ec.register_connection(netnsnode2, emu)
53
54 iface2 = ec.register_resource("netns::NodeInterface")
55 ec.register_connection(iface2, netnsnode2)
56
57 ip2 = ec.register_resource("netns::IPv4Address")
58 ec.set(ip2, "ip", "10.0.0.2")
59 ec.set(ip2, "prefix", "30")
60 ec.register_connection(ip2, iface2)
61
62 switch = ec.register_resource("netns::Switch")
63 ec.register_connection(iface1, switch)
64 ec.register_connection(iface2, switch)
65
66 ping = ec.register_resource("netns::Application")
67 ec.set(ping, "command", "ping -c20 10.0.0.2")
68 ec.register_connection(ping, netnsnode1)
69
70 ec.deploy()
71
72 ec.wait_finished([ping])
73
74 stdout = ec.trace(ping, "stdout") 
75
76 ec.shutdown()
77
78 print("PING OUTPUT", stdout)