tweaked ping.py for inclusion in r2lab's tutorials - essentially as-is, except for...
[nepi.git] / examples / linux / 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 # This very simple experiment will ssh in a linux box, and from there
21 # issue a ping command towards a landmark (default faraday.inria.fr)
22 #
23 # $ ping.py -u root mybox.domain.com
24
25 # would do the equivalent of
26 # ssh root@mybox.domain.com ping -c3 faraday.inria.fr
27 #
28 # If nepi is not installed in your system, and you only have a git clone
29 # you might wish to do instead
30 #  
31 # $ cd <path-to-nepi>/src
32 # $ python ../examples/linux/ping.py -u root mybox.domain.com
33
34 # let's be ready for python3
35 from __future__ import print_function
36
37 import os
38 from argparse import ArgumentParser
39
40 from nepi.execution.ec import ExperimentController 
41
42
43 default_landmark = "faraday.inria.fr"
44
45 parser = ArgumentParser()
46 parser.add_argument("-u", "--username", dest="username", 
47                     help="Username to SSH to remote host")
48 parser.add_argument("-i", "--ssh-key", dest="ssh_key", 
49                     help="Path to private SSH key to be used for connection")
50 parser.add_argument("-l", "--landmark", dest='landmark', default=default_landmark,
51                     help="Set pings destination, default={}".format(default_landmark))
52 # this is required
53 parser.add_argument("hostname", type=str)
54
55 args = parser.parse_args()
56
57 hostname = args.hostname
58 username = args.username
59 ssh_key = args.ssh_key
60 landmark = args.landmark
61
62 ec = ExperimentController(exp_id = "ping-exp")
63         
64 node = ec.register_resource("linux::Node")
65 ec.set(node, "hostname", hostname)
66 ec.set(node, "username", username)
67 ec.set(node, "identity", ssh_key)
68 ec.set(node, "cleanExperiment", True)
69 ec.set(node, "cleanProcesses", True)
70
71 app = ec.register_resource("linux::Application")
72 ec.set(app, "command", "ping -c3 {}".format(landmark))
73 ec.register_connection(app, node)
74
75 ec.deploy()
76
77 ec.wait_finished(app)
78
79 print(ec.trace(app, "stdout"))
80
81 ec.shutdown()
82