0560337027cc2edfe41c795c06efe7b08d8842a6
[nepi.git] / examples / linux / netcat_file_transfer.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/linux/netcat_file_transfer.py -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>
26
27 from nepi.execution.ec import ExperimentController
28 from nepi.execution.resource import ResourceAction, ResourceState
29
30 from optparse import OptionParser, SUPPRESS_HELP
31 import os
32
33 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
34
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", 
44         type="str")
45 (options, args) = parser.parse_args()
46
47 hostname1 = options.hostname1
48 hostname2 = options.hostname2
49 username = options.username
50 ssh_key = options.ssh_key
51
52 ## Create the experiment controller
53 ec = ExperimentController(exp_id = "nc_file_transfer")
54
55 ## Register node 1
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)
67
68 ## Register node 2 
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)
80
81 # Register server
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__)),
85             "..", video)
86
87 command = "cat ${SHARE}/%s | pv -fbt 2> bw.txt | nc %s 1234" % ( 
88         video, hostname2 )
89
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)
95
96 # Register client
97 # Note: is important to add the -d option in nc command to not attempt to read from the 
98 # stdin
99 # if not nc in the client side close the socket suddently if runned in background
100 command =  "nc -dl 1234 > %s" % video
101
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)
106
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"
109
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)
115
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)
120
121 # Deploy
122 ec.deploy()
123
124 # Wait until the applications are finish to retrive the traces
125 ec.wait_finished([server, client])
126
127 # Retrieve traces from nc and tcpdump
128 bw = ec.trace(server, "bw.txt")
129 pcap = ec.trace(capture, "file_transfer.pcap")
130
131 # Choose a directory to store the traces, example f = open("/home/<user>/bw.txt", "w")
132 f = open("bw.txt", "w")
133 f.write(bw)
134 f.close()
135 f = open("video_transfer.pcap", "w")
136 f.write(pcap)
137 f.close()
138
139 ec.shutdown()
140
141 print "Total bytes transfered saved to bw.txt..."
142