874f1a2f9f3a2c39bf48c14a2eb6e0035dcb5f51
[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 import base64
20 import socket
21
22 from optparse import OptionParser
23
24 STOP_MSG = "STOP"
25
26 def get_options():
27     usage = ("usage: %prog -N <vif-name> -S <socket-name>")
28     
29     parser = OptionParser(usage = usage)
30
31     parser.add_option("-N", "--vif-name", dest="vif_name",
32         help = "The name of the virtual interface, or a "
33                 "unique numeric identifier to name the interface "
34                 "if GRE mode is used.",
35         type="str")
36
37     parser.add_option("-S", "--socket-name", dest="socket_name",
38         help = "Name for the unix socket used to interact with this process", 
39         type="str")
40
41     (options, args) = parser.parse_args()
42    
43     return (options.socket_name, options.vif_name)
44
45 if __name__ == '__main__':
46
47     (socket_name, vif_name) = get_options()
48
49     # If a socket name is sent, send the STOP message and wait for a reply
50     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
51     try:
52         sock.connect(socket_name)
53         encoded = base64.b64encode(STOP_MSG)
54         sock.send("%s\n" % encoded)
55         reply = sock.recv(1024)
56         reply = base64.b64decode(reply)
57         print reply
58     except:
59         print "Did not properly shutdown device"
60