use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / src / nepi / resources / linux / scripts / linux-tap-delete.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from __future__ import print_function
20
21 import base64
22 import socket
23
24 from optparse import OptionParser
25
26 STOP_MSG = "STOP"
27
28 def get_options():
29     usage = ("usage: %prog -N <vif-name> -S <socket-name>")
30     
31     parser = OptionParser(usage = usage)
32
33     parser.add_option("-N", "--vif-name", dest="vif_name",
34         help = "The name of the virtual interface, or a "
35                 "unique numeric identifier to name the interface "
36                 "if GRE mode is used.",
37         type="str")
38
39     parser.add_option("-S", "--socket-name", dest="socket_name",
40         help = "Name for the unix socket used to interact with this process", 
41         type="str")
42
43     (options, args) = parser.parse_args()
44    
45     return (options.socket_name, options.vif_name)
46
47 if __name__ == '__main__':
48
49     (socket_name, vif_name) = get_options()
50
51     # If a socket name is sent, send the STOP message and wait for a reply
52     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
53     try:
54         sock.connect(socket_name)
55         encoded = base64.b64encode(STOP_MSG)
56         sock.send("%s\n" % encoded)
57         reply = sock.recv(1024)
58         reply = base64.b64decode(reply)
59         print(reply)
60     except:
61         print("Did not properly shutdown device")
62