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 as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Author: Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
20 # Alina Quereilhac <alina.quereilhac@inria.fr>
23 # Example of how to run this experiment (replace with your information):
26 # python examples/linux/netcat_file_transfer.py -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>
28 from nepi.execution.ec import ExperimentController
29 from nepi.execution.resource import ResourceAction, ResourceState
31 from optparse import OptionParser, SUPPRESS_HELP
34 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
36 parser = OptionParser(usage = usage)
37 parser.add_option("-a", "--hostname1", dest="hostname1",
38 help="Remote host 1", type="str")
39 parser.add_option("-b", "--hostname2", dest="hostname2",
40 help="Remote host 2", type="str")
41 parser.add_option("-u", "--username", dest="username",
42 help="Username to SSH to remote host", type="str")
43 parser.add_option("-i", "--ssh-key", dest="ssh_key",
44 help="Path to private SSH key to be used for connection",
46 (options, args) = parser.parse_args()
48 hostname1 = options.hostname1
49 hostname2 = options.hostname2
50 username = options.username
51 ssh_key = options.ssh_key
53 ## Create the experiment controller
54 ec = ExperimentController(exp_id = "nc_file_transfer")
57 node1 = ec.register_resource("LinuxNode")
58 # Set the hostname of the first node to use for the experiment
59 ec.set(node1, "hostname", hostname1)
60 # username should be your SSH user
61 ec.set(node1, "username", username)
62 # Absolute path to the SSH private key
63 ec.set(node1, "identity", ssh_key)
64 # Clean all files, results, etc, from previous experiments wit the same exp_id
65 ec.set(node1, "cleanExperiment", True)
66 # Kill all running processes in the node before running the experiment
67 ec.set(node1, "cleanProcesses", True)
70 node2 = ec.register_resource("LinuxNode")
71 # Set the hostname of the first node to use for the experiment
72 ec.set(node2, "hostname", hostname2)
73 # username should be your SSH user
74 ec.set(node2, "username", username)
75 # Absolute path to the SSH private key
76 ec.set(node2, "identity", ssh_key)
77 # Clean all files, results, etc, from previous experiments wit the same exp_id
78 ec.set(node2, "cleanExperiment", True)
79 # Kill all running processes in the node before running the experiment
80 ec.set(node2, "cleanProcesses", True)
83 video = "big_buck_bunny_240p_mpeg4_lq.ts"
84 local_path_to_video = os.path.join(
85 os.path.dirname(os.path.realpath(__file__)),
88 command = "cat ${SHARE}/%s | pv -fbt 2> bw.txt | nc %s 1234" % (
91 server = ec.register_resource("LinuxApplication")
92 ec.set(server, "depends", "pv nc tcpdump")
93 ec.set(server, "files", local_path_to_video)
94 ec.set(server, "command", command)
95 ec.register_connection(server, node1)
98 # Note: is important to add the -d option in nc command to not attempt to read from the
100 # if not nc in the client side close the socket suddently if runned in background
101 command = "nc -dl 1234 > %s" % video
103 client = ec.register_resource("LinuxApplication")
104 ec.set(client, "depends", "nc")
105 ec.set(client, "command", command)
106 ec.register_connection(client, node2)
108 # Register a tcpdump in the server node to monitor the file transfer
109 command = "tcpdump -ni eth0 -w file_transfer.pcap -s0 port 1234 2>&1"
111 capture = ec.register_resource("LinuxApplication")
112 ec.set(capture, "depends", "tcpdump")
113 ec.set(capture, "command", command)
114 ec.set(capture, "sudo", True)
115 ec.register_connection(capture, node1)
117 # Register conditions 1. nodes ; 2. start tcpdump capture ; 3. client listen port 1234 ;
118 # 4. server start sending video
119 ec.register_condition(server, ResourceAction.START, client, ResourceState.STARTED)
120 ec.register_condition(client, ResourceAction.START, capture, ResourceState.STARTED)
125 # Wait until the applications are finish to retrive the traces
126 ec.wait_finished([server, client])
128 # Retrieve traces from nc and tcpdump
129 bw = ec.trace(server, "bw.txt")
130 pcap = ec.trace(capture, "file_transfer.pcap")
132 # Choose a directory to store the traces, example f = open("/home/<user>/bw.txt", "w")
133 f = open("bw.txt", "w")
136 f = open("video_transfer.pcap", "w")
142 print "Total bytes transfered saved to bw.txt..."