use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / ns3 / remote_p2p_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 from __future__ import print_function
21
22 from nepi.execution.ec import ExperimentController 
23
24 from optparse import OptionParser
25
26 usage = ("usage: %prog -H <hostanme> -u <username> -i <ssh-key>")
27
28 parser = OptionParser(usage = usage)
29 parser.add_option("-H", "--hostname", dest="hostname", 
30         help="Remote host name or IP address", type="str")
31 parser.add_option("-u", "--username", dest="username", 
32         help="Username to SSH to remote host", type="str")
33 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
34         help="Path to private SSH key to be used for connection", 
35         type="str")
36 (options, args) = parser.parse_args()
37
38 hostname = options.hostname
39 username = options.username
40 identity = options.ssh_key
41
42 ec = ExperimentController(exp_id = "ns3-remote-p2p-ping")
43
44 # Simulation will run in a remote machine
45 node = ec.register_resource("linux::Node")
46 ec.set(node, "hostname", hostname)
47 ec.set(node, "username", username)
48 ec.set(node, "identity", identity)
49 ec.set(node, "cleanProcesses", True)
50 ec.set(node, "cleanExperiment", True)
51
52 # Add a simulation resource
53 simu = ec.register_resource("linux::ns3::Simulation")
54 ec.set(simu, "verbose", True)
55 ec.register_connection(simu, node)
56
57 ## Add a ns-3 node with its protocol stack
58 nsnode1 = ec.register_resource("ns3::Node")
59 ec.register_connection(nsnode1, simu)
60
61 ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
62 ec.register_connection(nsnode1, ipv4)
63 arp = ec.register_resource("ns3::ArpL3Protocol")
64 ec.register_connection(nsnode1, arp)
65 icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
66 ec.register_connection(nsnode1, icmp)
67
68 # Add a point to point net device to the node
69 dev1 = ec.register_resource("ns3::PointToPointNetDevice")
70 ec.set(dev1, "ip", "10.0.0.1")
71 ec.set(dev1, "prefix", "30")
72 ec.register_connection(nsnode1, dev1)
73 queue1 = ec.register_resource("ns3::DropTailQueue")
74 ec.register_connection(dev1, queue1)
75
76 ## Add another ns-3 node with its protocol stack
77 nsnode2 = ec.register_resource("ns3::Node")
78 ec.register_connection(nsnode2, simu)
79
80 ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
81 ec.register_connection(nsnode2, ipv4)
82 arp = ec.register_resource("ns3::ArpL3Protocol")
83 ec.register_connection(nsnode2, arp)
84 icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
85 ec.register_connection(nsnode2, icmp)
86
87 # Add a point to point net device to the node
88 dev2 = ec.register_resource("ns3::PointToPointNetDevice")
89 ec.set(dev2, "ip", "10.0.0.2")
90 ec.set(dev2, "prefix", "30")
91 ec.register_connection(nsnode2, dev2)
92 queue2 = ec.register_resource("ns3::DropTailQueue")
93 ec.register_connection(dev2, queue2)
94
95 # Add a point to point channel
96 chan = ec.register_resource("ns3::PointToPointChannel")
97 ec.set(chan, "Delay", "0s")
98 ec.register_connection(chan, dev1)
99 ec.register_connection(chan, dev2)
100
101 ### create pinger
102 ping = ec.register_resource("ns3::V4Ping")
103 ec.set (ping, "Remote", "10.0.0.2")
104 ec.set (ping, "Interval", "1s")
105 ec.set (ping, "Verbose", True)
106 ec.set (ping, "StartTime", "0s")
107 ec.set (ping, "StopTime", "20s")
108 ec.register_connection(ping, nsnode1)
109
110 ec.deploy()
111
112 ec.wait_finished([ping])
113
114 stdout = ec.trace(simu, "stdout") 
115
116 ec.shutdown()
117
118 print("PING OUTPUT", stdout)