2 # VSYS script to ifdown per-slice virtual network interfaces from the root slice
3 # Thom Haddow - 06/10/09
5 # Gets slice name as argv[1]
6 # Takes remaining args on stdin:
7 # - Interface name (eg [tun|tap]<sliceid>-<n>)
15 if len(sys.argv) != 2: sys.exit(1)
17 # VSYS scripts get slicename as $1
19 sliceid = pwd.getpwnam(slicename).pw_uid
23 vif = sys.stdin.readline().strip() # interface name
26 # Validate interface name
28 print >>sys.stderr, "Interface name %s invalid"%(vif)
31 if re.match(r'(tun|tap)%d-\d+' % sliceid, vif ) is None:
32 print >>sys.stderr, "Interface name %s does not match slice id %d."%(vif, sliceid)
37 # Bring interface down
39 cmd_ifconfig = "/sbin/ifconfig %s down" % (vif)
40 os.system(cmd_ifconfig)
42 # Remove iptables rules
43 cmd_iptables_del_in = "/sbin/iptables -D INPUT -i %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
44 cmd_iptables_del_out = "/sbin/iptables -D OUTPUT -o %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
46 os.system(cmd_iptables_del_in)
47 os.system(cmd_iptables_del_out)