use print() - import print_function - should be fine for both py2 and py3
[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 __future__ import print_function
28
29 from nepi.execution.ec import ExperimentController
30 from nepi.execution.resource import ResourceAction, ResourceState
31
32 from optparse import OptionParser, SUPPRESS_HELP
33 import os
34
35 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
36
37 parser = OptionParser(usage = usage)
38 parser.add_option("-a", "--hostname1", dest="hostname1", 
39         help="Remote host 1", type="str")
40 parser.add_option("-b", "--hostname2", dest="hostname2", 
41         help="Remote host 2", type="str")
42 parser.add_option("-u", "--username", dest="username", 
43         help="Username to SSH to remote host", type="str")
44 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
45         help="Path to private SSH key to be used for connection", 
46         type="str")
47 (options, args) = parser.parse_args()
48
49 hostname1 = options.hostname1
50 hostname2 = options.hostname2
51 username = options.username
52 ssh_key = options.ssh_key
53
54 ## Create the experiment controller
55 ec = ExperimentController(exp_id = "nc_file_transfer")
56
57 ## Register node 1
58 node1 = ec.register_resource("linux::Node")
59 # Set the hostname of the first node to use for the experiment
60 ec.set(node1, "hostname", hostname1)
61 # username should be your SSH user 
62 ec.set(node1, "username", username)
63 # Absolute path to the SSH private key
64 ec.set(node1, "identity", ssh_key)
65 # Clean all files, results, etc, from previous experiments wit the same exp_id
66 ec.set(node1, "cleanExperiment", True)
67 # Kill all running processes in the node before running the experiment
68 ec.set(node1, "cleanProcesses", True)
69
70 ## Register node 2 
71 node2 = ec.register_resource("linux::Node")
72 # Set the hostname of the first node to use for the experiment
73 ec.set(node2, "hostname", hostname2)
74 # username should be your SSH user 
75 ec.set(node2, "username", username)
76 # Absolute path to the SSH private key
77 ec.set(node2, "identity", ssh_key)
78 # Clean all files, results, etc, from previous experiments wit the same exp_id
79 ec.set(node2, "cleanExperiment", True)
80 # Kill all running processes in the node before running the experiment
81 ec.set(node2, "cleanProcesses", True)
82
83 # Register server
84 video = "big_buck_bunny_240p_mpeg4_lq.ts"
85 local_path_to_video = os.path.join(
86         os.path.dirname(os.path.realpath(__file__)),
87             "..", video)
88
89 command = "cat ${SHARE}/%s | pv -fbt 2> bw.txt | nc %s 1234" % ( 
90         video, hostname2 )
91
92 server = ec.register_resource("linux::Application")
93 ec.set(server, "depends", "pv nc tcpdump")
94 ec.set(server, "files", local_path_to_video)
95 ec.set(server, "command", command)
96 ec.register_connection(server, node1)
97
98 # Register client
99 # Note: is important to add the -d option in nc command to not attempt to read from the 
100 # stdin
101 # if not nc in the client side close the socket suddently if runned in background
102 command =  "nc -dl 1234 > %s" % video
103
104 client = ec.register_resource("linux::Application")
105 ec.set(client, "depends", "nc")
106 ec.set(client, "command", command)
107 ec.register_connection(client, node2)
108
109 # Register a tcpdump in the server node to monitor the file transfer 
110 command = "tcpdump -ni eth0 -w file_transfer.pcap -s0 port 1234 2>&1"
111
112 capture = ec.register_resource("linux::Application")
113 ec.set(capture, "depends", "tcpdump")
114 ec.set(capture, "command", command)
115 ec.set(capture, "sudo", True)
116 ec.register_connection(capture, node1)
117
118 # Register conditions 1. nodes ; 2. start tcpdump capture ; 3. client listen port 1234 ;
119 # 4. server start sending video
120 ec.register_condition(server, ResourceAction.START, client, ResourceState.STARTED) 
121 ec.register_condition(client, ResourceAction.START, capture, ResourceState.STARTED)
122
123 # Deploy
124 ec.deploy()
125
126 # Wait until the applications are finish to retrive the traces
127 ec.wait_finished([server, client])
128
129 # Retrieve traces from nc and tcpdump
130 bw = ec.trace(server, "bw.txt")
131 pcap = ec.trace(capture, "file_transfer.pcap")
132
133 # Choose a directory to store the traces, example f = open("/home/<user>/bw.txt", "w")
134 f = open("bw.txt", "w")
135 f.write(bw)
136 f.close()
137 f = open("video_transfer.pcap", "w")
138 f.write(pcap)
139 f.close()
140
141 ec.shutdown()
142
143 print("Total bytes transfered saved to bw.txt...")
144