use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / planetlab / 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: Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
19 #         Alina Quereilhac <alina.quereilhac@inria.fr>
20 #
21
22 # Example of how to run this experiment (replace with your information):
23 #
24 # $ cd <path-to-nepi>
25 # python examples/planetlab/ping.py -s <pl-slice> -u <pl-user> -p <pl-password> -k <pl-ssh-key>  
26
27 from __future__ import print_function
28
29 from nepi.execution.ec import ExperimentController
30
31 from optparse import OptionParser
32 import os
33
34 usage = ("usage: %prog -s <pl-slice> -u <pl-user> -p <pl-password> "
35      "-k <pl-ssh-key>")
36
37 parser = OptionParser(usage = usage)
38 parser.add_option("-s", "--pl-slice", dest="pl_slice",
39         help="PlanetLab slicename", type="str")
40 parser.add_option("-u", "--pl-user", dest="pl_user",
41         help="PlanetLab web username", type="str")
42 parser.add_option("-p", "--pl-password", dest="pl_password",
43         help="PlanetLab web password", type="str")
44 parser.add_option("-k", "--pl-ssh-key", dest="pl_ssh_key",
45         help="Path to private SSH key associated with the PL account",
46         type="str")
47
48 (options, args) = parser.parse_args()
49
50 pl_slice = options.pl_slice
51 pl_ssh_key = options.pl_ssh_key
52 pl_user = options.pl_user
53 pl_password = options.pl_password
54
55 ## Create the experiment controller
56 ec = ExperimentController(exp_id = "pl_ping")
57
58 # Register a Planetlab Node with no restrictions, it can be any node
59 node = ec.register_resource("planetlab::Node")
60
61 # The username in this case is the slice name, the one to use for login in 
62 # via ssh into PlanetLab nodes. Replace with your own slice name.
63 ec.set(node, "username", pl_slice)
64 ec.set(node, "identity", pl_ssh_key)
65
66 # The pluser and plpassword are the ones used to login in the PlanetLab web 
67 # site. Replace with your own user and password account information.
68 ec.set(node, "pluser", pl_user)
69 ec.set(node, "plpassword", pl_password)
70
71 # Remove previous results
72 ec.set(node, "cleanExperiment", True)
73 ec.set(node, "cleanProcesses", True)
74
75 # Define a ping application
76 app = ec.register_resource("linux::Application")
77 ec.set(app, "command", "ping -c3 nepi.inria.fr")
78
79 # Connect the application to the node
80 ec.register_connection(node, app)
81     
82 # Deploy the experiment:
83 ec.deploy()
84
85 # Wait until the application is finish to retrive the trace:
86 ec.wait_finished(app)
87
88 trace = ec.trace(app, "stdout")
89
90 print("PING outout ", trace)
91
92 # Do the experiment controller shutdown
93 ec.shutdown()
94
95 # END