e2047d10047e07cde6a794965bb2d202a825ecd1
[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 as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
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.
15 #
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/>.
18 #
19 # Author: Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
20 #         Alina Quereilhac <alina.quereilhac@inria.fr>
21 #
22 #
23 # Example of how to run this experiment (replace with your information):
24 #
25 # $ cd <path-to-nepi>
26 # python examples/linux/netcat_file_transfer.py -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>
27
28 from nepi.execution.ec import ExperimentController
29 from nepi.execution.resource import ResourceAction, ResourceState
30
31 from optparse import OptionParser, SUPPRESS_HELP
32 import os
33
34 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
35
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", 
45         type="str")
46 (options, args) = parser.parse_args()
47
48 hostname1 = options.hostname1
49 hostname2 = options.hostname2
50 username = options.username
51 ssh_key = options.ssh_key
52
53 ## Create the experiment controller
54 ec = ExperimentController(exp_id = "nc_file_transfer")
55
56 ## Register node 1
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)
68
69 ## Register node 2 
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)
81
82 # Register server
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__)),
86             "..", video)
87
88 command = "cat ${SHARE}/%s | pv -fbt 2> bw.txt | nc %s 1234" % ( 
89         video, hostname2 )
90
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)
96
97 # Register client
98 # Note: is important to add the -d option in nc command to not attempt to read from the 
99 # stdin
100 # if not nc in the client side close the socket suddently if runned in background
101 command =  "nc -dl 1234 > %s" % video
102
103 client = ec.register_resource("LinuxApplication")
104 ec.set(client, "depends", "nc")
105 ec.set(client, "command", command)
106 ec.register_connection(client, node2)
107
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"
110
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)
116
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)
121
122 # Deploy
123 ec.deploy()
124
125 # Wait until the applications are finish to retrive the traces
126 ec.wait_finished([server, client])
127
128 # Retrieve traces from nc and tcpdump
129 bw = ec.trace(server, "bw.txt")
130 pcap = ec.trace(capture, "file_transfer.pcap")
131
132 # Choose a directory to store the traces, example f = open("/home/<user>/bw.txt", "w")
133 f = open("bw.txt", "w")
134 f.write(bw)
135 f.close()
136 f = open("video_transfer.pcap", "w")
137 f.write(pcap)
138 f.close()
139
140 ec.shutdown()
141
142 print "Total bytes transfered saved to bw.txt..."
143