3 # NEPI, a framework to manage network experiments
4 # Copyright (C) 2013 INRIA
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;
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.
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/>.
18 # Author: Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
19 # Alina Quereilhac <alina.quereilhac@inria.fr>
22 # Example of how to run this experiment (replace with your information):
25 # python examples/linux/netcat_file_transfer.py -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>
27 from nepi.execution.ec import ExperimentController
28 from nepi.execution.resource import ResourceAction, ResourceState
30 from optparse import OptionParser, SUPPRESS_HELP
33 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
35 parser = OptionParser(usage = usage)
36 parser.add_option("-a", "--hostname1", dest="hostname1",
37 help="Remote host 1", type="str")
38 parser.add_option("-b", "--hostname2", dest="hostname2",
39 help="Remote host 2", type="str")
40 parser.add_option("-u", "--username", dest="username",
41 help="Username to SSH to remote host", type="str")
42 parser.add_option("-i", "--ssh-key", dest="ssh_key",
43 help="Path to private SSH key to be used for connection",
45 (options, args) = parser.parse_args()
47 hostname1 = options.hostname1
48 hostname2 = options.hostname2
49 username = options.username
50 ssh_key = options.ssh_key
52 ## Create the experiment controller
53 ec = ExperimentController(exp_id = "nc_file_transfer")
56 node1 = ec.register_resource("linux::Node")
57 # Set the hostname of the first node to use for the experiment
58 ec.set(node1, "hostname", hostname1)
59 # username should be your SSH user
60 ec.set(node1, "username", username)
61 # Absolute path to the SSH private key
62 ec.set(node1, "identity", ssh_key)
63 # Clean all files, results, etc, from previous experiments wit the same exp_id
64 ec.set(node1, "cleanExperiment", True)
65 # Kill all running processes in the node before running the experiment
66 ec.set(node1, "cleanProcesses", True)
69 node2 = ec.register_resource("linux::Node")
70 # Set the hostname of the first node to use for the experiment
71 ec.set(node2, "hostname", hostname2)
72 # username should be your SSH user
73 ec.set(node2, "username", username)
74 # Absolute path to the SSH private key
75 ec.set(node2, "identity", ssh_key)
76 # Clean all files, results, etc, from previous experiments wit the same exp_id
77 ec.set(node2, "cleanExperiment", True)
78 # Kill all running processes in the node before running the experiment
79 ec.set(node2, "cleanProcesses", True)
82 video = "big_buck_bunny_240p_mpeg4_lq.ts"
83 local_path_to_video = os.path.join(
84 os.path.dirname(os.path.realpath(__file__)),
87 command = "cat ${SHARE}/%s | pv -fbt 2> bw.txt | nc %s 1234" % (
90 server = ec.register_resource("linux::Application")
91 ec.set(server, "depends", "pv nc tcpdump")
92 ec.set(server, "files", local_path_to_video)
93 ec.set(server, "command", command)
94 ec.register_connection(server, node1)
97 # Note: is important to add the -d option in nc command to not attempt to read from the
99 # if not nc in the client side close the socket suddently if runned in background
100 command = "nc -dl 1234 > %s" % video
102 client = ec.register_resource("linux::Application")
103 ec.set(client, "depends", "nc")
104 ec.set(client, "command", command)
105 ec.register_connection(client, node2)
107 # Register a tcpdump in the server node to monitor the file transfer
108 command = "tcpdump -ni eth0 -w file_transfer.pcap -s0 port 1234 2>&1"
110 capture = ec.register_resource("linux::Application")
111 ec.set(capture, "depends", "tcpdump")
112 ec.set(capture, "command", command)
113 ec.set(capture, "sudo", True)
114 ec.register_connection(capture, node1)
116 # Register conditions 1. nodes ; 2. start tcpdump capture ; 3. client listen port 1234 ;
117 # 4. server start sending video
118 ec.register_condition(server, ResourceAction.START, client, ResourceState.STARTED)
119 ec.register_condition(client, ResourceAction.START, capture, ResourceState.STARTED)
124 # Wait until the applications are finish to retrive the traces
125 ec.wait_finished([server, client])
127 # Retrieve traces from nc and tcpdump
128 bw = ec.trace(server, "bw.txt")
129 pcap = ec.trace(capture, "file_transfer.pcap")
131 # Choose a directory to store the traces, example f = open("/home/<user>/bw.txt", "w")
132 f = open("bw.txt", "w")
135 f = open("video_transfer.pcap", "w")
141 print "Total bytes transfered saved to bw.txt..."