def configure_network_interfaces(): """Configure network interfaces This function configure the following network interfaces stxbr[1-4] """ for interface in range(1, 5): current_interface = 'stxbr{}'.format(interface) # adding the network interface try: brctl.addbr(current_interface) except IOError: LOG.warning('[Errno 17] File exists %s', current_interface) networks = [ 'stxbr1 10.10.10.1/24', 'stxbr2 192.168.204.1/24', 'stxbr3', 'stxbr4' ] for net in networks: eval_cmd = bash('sudo ifconfig {} up'.format(net)) if 'ERROR'.encode('utf-8') in eval_cmd.stderr: LOG.error(eval_cmd.stderr) raise EnvironmentError(eval_cmd.stderr) # setting the ip tables iptables = ('sudo iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -j ' 'MASQUERADE') eval_cmd = bash(iptables) if 'ERROR'.encode('utf-8') in eval_cmd.stderr: LOG.error(eval_cmd.stderr) raise EnvironmentError(eval_cmd.stderr)
def test_addbr_delete(): br = brctl.addbr(b'br_test') try: check_output(b'brctl show', substr=[br.name]) finally: br.delete() check_output(b'brctl show', not_substr=[br.name])
def __init__(self, name): """ :param name: Name of the bridge """ self.name = name self.bridge = brctl.addbr(self.name) subprocess.call(['ip', 'link', 'set', 'dev', self.name, 'up']) subprocess.call( ['tc', 'qdisc', 'add', 'dev', self.name, 'root', 'netem'])
def delete_network_interfaces(): """Delete network interfaces This function performs a clean up for the following network interfaces virbr[1-4] """ # elevate module re-launches the current process with root/admin privileges # using one of the following mechanisms : sudo (Linux, macOS) # becoming in root elevate(graphical=False) ifdata = Ifcfg(subprocess.check_output(['ifconfig', '-a'])) for interface in range(1, 5): current_interface = 'virbr{}'.format(interface) if current_interface in ifdata.interfaces: # the network interface exists net_object = ifdata.get_interface(current_interface) net_up = net_object.get_values().get('UP') net_running = net_object.get_values().get('RUNNING') if net_up or net_running: # the network interface is up or running try: # down and delete the network interface ifconfig.Interface(current_interface).down() brctl.Bridge(current_interface).delete() except IOError: LOG.warn('[Errno 19] No such device: {}'.format( current_interface)) # adding the network interface try: brctl.addbr(current_interface) except IOError: LOG.warn('[Errno 17] File exists {}'.format(current_interface))
def create_bridge_with_vid(name, vid): devlist = [dev for dev in ifconfig.iterifs(physical=False) if dev.name == "eth1.10"] if len(devlist) != 0: raise Exists("eth1.10") br0 = brctl.findbridge(name) if br0: raise Exists("br0") br0 = brctl.addbr(name) vconfig.add_vlan("eth1", vid) iface1 = ifconfig.Interface("eth1.%s" % vid) br0.addif(iface1) br_iface = ifconfig.Interface(name) iface1.up() br_iface.up() return
def bridge(request, name): br = brctl.addbr(name) def cleanup(): br.delete() request.addfinalizer(cleanup) return br