2 # Vsys script to set a device in promisc mode
3 # The user needs to have the device allocated to him
4 # via the vsys_vnet tag. Set vsys_vnet to the IP address of
8 # cat > /vsys/promisc.in
12 # To disable promisc mode:
14 # cat > /vsys/promisc.in
19 # (Notice the - on a newline)
31 vsys_config_dir = "/etc/planetlab/vsys-attributes"
33 def get_ip_address(ifname):
34 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
35 return socket.inet_ntoa(fcntl.ioctl(
38 struct.pack('256s', ifname[:15])
41 if len(sys.argv) != 2: sys.exit(1)
44 netblock_config=os.path.join(vsys_config_dir,slicename,"vsys_vnet")
46 # Read netblock allocation file
49 for netblock in open(netblock_config,'r'):
50 base, mask = netblock.split('/')
53 print >>sys.stderr, "Could not find entry for slice %s in netblock config file %s" % (slicename, netblock_config)
56 ### Read args from stdin
57 arglines = map(string.strip, sys.stdin.readlines())
60 print >>sys.stderr, "Insufficient argument lines."
63 vif = arglines[0] # interface name
65 if (len(arglines)==2 and arglines[1]=='-'):
70 vip = get_ip_address(vif)
73 # Convert network base addr to int format by unpacking as 32bit net-ordered long (!L)
74 base_int = struct.unpack('!L',socket.inet_aton(base))[0]
78 # Validate interface name
80 print >>sys.stderr, "Interface name %s invalid"%(vif)
84 vip_int = struct.unpack('!L',socket.inet_aton(vip))[0]
86 print >>sys.stderr, "Invalid IP: %s" % vip
89 # Check IP is in netblock
90 if (vip_int>>(32-mask)) != (base_int>>(32-mask)):
91 print >>sys.stderr, "Requested IP %s not in netblock %s/%d" % (vip,base,mask)
94 # OK. All checks passed. We can now act on the device.
97 os.system('ifconfig %s promisc'%vif)
99 os.system('ifconfig %s -promisc'%vif)