use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / src / nepi / resources / planetlab / scripts / pl-vif-down.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 import vsys
24
25 from optparse import OptionParser
26
27 STOP_MSG = "STOP"
28
29 def get_options():
30     usage = ("usage: %prog -u <slicename> -N <vif-name> -t <vif-type> "
31             "-D <delete> -S <socket-name>")
32     
33     parser = OptionParser(usage = usage)
34
35     parser.add_option("-u", "--slicename", dest="slicename",
36         help = "The name of the PlanetLab slice ",
37         type="str")
38
39     parser.add_option("-N", "--vif-name", dest="vif_name",
40         help = "The name of the virtual interface, or a "
41                 "unique numeric identifier to name the interface "
42                 "if GRE mode is used.",
43         type="str")
44
45     parser.add_option("-t", "--vif-type", dest="vif_type",
46             help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
47             "Defaults to IFF_TAP. ", type="str")
48
49     parser.add_option("-D", "--delete", dest="delete", 
50             action="store_true", 
51             default = False,
52             help="Removes virtual interface if GRE mode was used")
53
54     parser.add_option("-S", "--socket-name", dest="socket_name",
55         help = "Name for the unix socket used to interact with this process", 
56         type="str")
57
58     (options, args) = parser.parse_args()
59    
60     vif_type = vsys.IFF_TAP
61     if options.vif_type and options.vif_type == "IFF_TUN":
62         vif_type = vsys.IFF_TUN
63
64     return (options.socket_name, options.vif_name, options.slicename, 
65             vif_type, options.delete)
66
67 if __name__ == '__main__':
68
69     (socket_name, vif_name, slicename, vif_type, delete) = get_options()
70
71     # If a socket name is sent, send the STOP message and wait for a reply
72     if socket_name:
73         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
74         try:
75             sock.connect(socket_name)
76             encoded = base64.b64encode(STOP_MSG)
77             sock.send("%s\n" % encoded)
78             reply = sock.recv(1024)
79             reply = base64.b64decode(reply)
80             print(reply)
81         except:
82             print("Did not properly shutdown device")
83     # If a slicename is provided, use it to remove a GRE device
84     elif slicename:
85         import pwd
86         import getpass
87
88         sliceid = pwd.getpwnam(slicename).pw_uid
89
90         if vif_type == vsys.IFF_TAP:
91             vif_prefix = "tap"
92         else:
93             vif_prefix = "tun"
94
95         # if_name should be a unique numeric vif id
96         vif_name = "%s%s-%s" % (vif_prefix, sliceid, vif_name) 
97
98         vsys.vif_down(vif_name, delete = True)
99
100     # Else, use the vsys interface to set the virtual interface down
101     else:
102         vsys.vif_down(vif_name)
103
104