Exemplo n.º 1
0
    def test_manipulation(self):
        temp_dir = tempfile.mkdtemp()
        os.mkdir(join(temp_dir, 'interfaces.d'))

        f1 = InterfacesFile(self.interfaces_filename)
        new_filename = join(temp_dir, 'interfaces')
        h1 = hash(f1)
        f1.save(recursive=True, filename=new_filename)
        f2 = self.checkup_interfaces_file(new_filename)
        h2 = hash(f2)
        self.assertEqual(h1, h2)

        eth0 = f2.get_iface('eth0')

        eth0.address = '192.168.11.2'
        self.assertEqual(eth0.address, '192.168.11.2')

        eth0.script = '/etc/network/if-up.d/eth0-up'
        self.assertEqual(eth0.script, '/etc/network/if-up.d/eth0-up')

        del eth0.netmask
        f2.save(new_filename, validate=False)

        f3 = InterfacesFile(new_filename)
        eth0 = f3.get_iface('eth0')
        self.assertEqual(eth0.address, '192.168.11.2')
        self.assertEqual(eth0.script, '/etc/network/if-up.d/eth0-up')
        self.assertRaises(AttributeError, lambda: self.netmask)
Exemplo n.º 2
0
    def test_validate(self):
        f1 = InterfacesFile(self.interfaces_filename)
        eth0 = f1.get_iface('eth0')
        self.assertTrue(eth0.validate())

        del eth0.network
        self.assertRaises(ValidationError, eth0.validate)
        self.assertTrue(eth0.validate(allow_correction=True))

        # del eth0.netmask
        # self.assertRaises(ValidationError, eth0.validate)
        # self.assertTrue(eth0.validate(allow_correction=True))

        eth0.address = '192.168.90.3'
        self.assertRaises(ValidationError, eth0.validate)
Exemplo n.º 3
0
    def test_validate(self):
        f1 = InterfacesFile(self.interfaces_filename)
        eth0 = f1.get_iface('eth0')
        self.assertTrue(eth0.validate())

        del eth0.network
        self.assertRaises(ValidationError, eth0.validate)
        self.assertTrue(eth0.validate(allow_correction=True))

        # del eth0.netmask
        # self.assertRaises(ValidationError, eth0.validate)
        # self.assertTrue(eth0.validate(allow_correction=True))

        eth0.address = '192.168.90.3'
        self.assertRaises(ValidationError, eth0.validate)
Exemplo n.º 4
0
    def test_manipulation(self):
        temp_dir = tempfile.mkdtemp()
        os.mkdir(join(temp_dir, 'interfaces.d'))

        f1 = InterfacesFile(self.interfaces_filename)
        new_filename = join(temp_dir, 'interfaces')
        h1 = hash(f1)
        f1.save(recursive=True, filename=new_filename)
        f2 = self.checkup_interfaces_file(new_filename)
        h2 = hash(f2)
        self.assertEqual(h1, h2)

        eth0 = f2.get_iface('eth0')

        eth0.address = '192.168.11.2'
        self.assertEqual(eth0.address, '192.168.11.2')

        eth0.script = '/etc/network/if-up.d/eth0-up'
        self.assertEqual(eth0.script, '/etc/network/if-up.d/eth0-up')

        del eth0.netmask
        f2.save(new_filename, validate=False)

        f3 = InterfacesFile(new_filename)
        eth0 = f3.get_iface('eth0')
        self.assertEqual(eth0.address, '192.168.11.2')
        self.assertEqual(eth0.script, '/etc/network/if-up.d/eth0-up')
        self.assertRaises(AttributeError, lambda: self.netmask)
Exemplo n.º 5
0
def set_network(iface_name, iface_info, dns_info):
    """
    Set network info.
    """
    if is_changed_network(iface_name, iface_info, dns_info) == False:
        print "Network is not changed."
        return 0
    # 1. Update /etc/network/interfaces
    cfg_file = InterfacesFile(CONFIG_FILE_NETWORK_INTERFACE)
    iface = cfg_file.get_iface(iface_name)
    if iface_info["dhcp"] == "1":
        iface.method = "dhcp"
        delattr(iface, "address")
        delattr(iface, "netmask")
        delattr(iface, "gateway")
    else:
        iface.method = "static"
        if iface_info["ip"]:
            iface.address = iface_info["ip"]
        if iface_info["netmask"]:
            iface.netmask = iface_info["netmask"]
        if iface_info["gateway"]:
            iface.gateway = iface_info["gateway"]

    cfg_file.save()
    # 2. Update /etc/resolv.conf
    if dns_info["dns"]:
        lines = "nameserver " + dns_info["dns"] + "\n"
        file = open(CONFIG_FILE_NETWORK_DNS, "w")
        try:
            file.writelines(lines)
        finally:
            file.close()

    #3. Iface down wlan0 and iface up wlan0
    execute_cmd("/bin/bash", [
        "-c", COMMAND_SUDO + " " + COMMAND_NETWORK_IFACE_DOWN + " " +
        iface_name + ";" + COMMAND_SUDO + " " + COMMAND_NETWORK_IFACE_UP +
        " " + iface_name + ";"
    ])
    return 0
Exemplo n.º 6
0
    def checkup_interfaces_file(self, filename):
        f = InterfacesFile(filename)

        self.assertRaises(KeyError, f.get_iface, 'non-existance-iface')

        lo = f.get_iface('lo')
        self.assertEquals(lo.startup.mode, 'auto')
        self.assertIsInstance(lo.startup, Auto)
        self.assertEquals(lo.name, 'lo')
        self.assertEquals(lo.address_family, 'inet')
        self.assertEquals(lo.method, 'loopback')

        eth0 = f.get_iface('eth0')
        self.assertEquals(eth0.startup.mode, 'auto')
        self.assertIsInstance(eth0.startup, Auto)
        self.assertEquals(eth0.name, 'eth0')
        self.assertEquals(eth0.address_family, 'inet')
        self.assertEquals(eth0.method, 'static')
        self.assertEquals(eth0.netmask, '255.255.255.240')
        self.assertEquals(eth0.broadcast, '192.168.11.15')
        self.assertEquals(eth0.network, '192.168.11.0')
        self.assertEquals(eth0.gateway, '192.168.11.1')
        self.assertEquals(eth0.dns_nameservers, '8.8.8.8 8.8.4.4')

        self.assertTrue(hasattr(eth0, 'netmask'))
        self.assertTrue('netmask' in eth0)
        self.assertFalse('lorem' in eth0)

        eth1 = f.get_iface('eth1')
        self.assertEquals(eth1.startup.mode, 'auto')
        self.assertIsInstance(eth1.startup, Auto)
        self.assertEquals(eth1.name, 'eth1')
        self.assertEquals(eth1.address_family, 'inet')
        self.assertEquals(eth1.method, 'dhcp')
        self.assertRaises(AttributeError, lambda: eth1.address)

        wlan0 = f.get_iface('wlan0')
        self.assertEquals(wlan0.startup.mode, 'allow-hotplug')
        self.assertIsInstance(wlan0.startup, Allow)
        self.assertEquals(wlan0.name, 'wlan0')
        self.assertEquals(wlan0.address_family, 'inet')
        self.assertEquals(wlan0.method, 'static')
        self.assertEquals(wlan0.address, '192.168.9.41')
        self.assertEquals(wlan0.netmask, '255.255.255.0')
        self.assertEquals(wlan0.broadcast, '192.168.9.255')
        self.assertEquals(wlan0.network, '192.168.9.0')
        self.assertEquals(wlan0.gateway, '192.168.9.1')
        self.assertEquals(wlan0.wpa_ssid, 'khayyam')
        self.assertEquals(wlan0.wpa_psk, '<-Wine&Is&Mine->')

        wlan1 = f.get_iface('wlan1')
        self.assertEquals(wlan1.startup.mode, 'auto')
        self.assertIsInstance(wlan1.startup, Auto)
        self.assertEquals(wlan1.name, 'wlan1')
        self.assertEquals(wlan1.address_family, 'inet')
        self.assertEquals(wlan1.method, 'dhcp')
        self.assertEquals(wlan1.wpa_ssid, 'Dorfak')
        self.assertEquals(wlan1.wpa_psk, 'ShoorPalangGoolakhTappeh')
        self.assertRaises(AttributeError, lambda: wlan1.address)

        eth2 = f.get_iface('eth2')
        self.assertEquals(eth2.startup.mode, 'auto')
        self.assertIsInstance(eth2.startup, Auto)
        self.assertEquals(eth2.name, 'eth2')
        self.assertEquals(eth2.script, '/usr/local/sbin/map-scheme')
        self.assertEquals(eth2.map_HOME, 'eth2-home')
        self.assertEquals(eth2.map_WORK, 'eth2-work')
        self.assertEquals(
            eth2.mappings,
            [['map', 'HOME', 'eth2-home'], ['map', 'WORK', 'eth2-work']])

        eth2_home = f.get_iface('eth2-home')
        self.assertIsNone(eth2_home.startup)
        self.assertEquals(eth2_home.name, 'eth2-home')
        self.assertEquals(eth2_home.address_family, 'inet')
        self.assertEquals(eth2_home.method, 'static')
        self.assertEquals(eth2_home.address, '192.168.1.1')
        self.assertEquals(eth2_home.netmask, '255.255.255.0')
        self.assertEquals(eth2_home.up, 'flush-mail')

        return f
 def test_validate(self):
     f1 = InterfacesFile(self.interfaces_filename)
     eth0 = f1.get_iface('eth0')
     self.assertTrue(eth0.validate())
Exemplo n.º 8
0
def get_network_info(iface):
    """
    Get network info.
    """

    cfg_file = InterfacesFile(CONFIG_FILE_NETWORK_INTERFACE)
    iface = cfg_file.get_iface(iface)

    dhcp = None
    address = None
    netmask = None
    gateway = None

    if iface.method == 'dhcp':
        dhcp = 1
    else:
        dhcp = 0

    if iface.method == 'static':
        address = iface.address
        netmask = iface.netmask
        gateway = iface.gateway

    networkinfo = {}
    networkinfo["dhcp"] = dhcp

    networkinfo["ip"] = ""
    networkinfo["ip_sec1"] = ""
    networkinfo["ip_sec2"] = ""
    networkinfo["ip_sec3"] = ""
    networkinfo["ip_sec4"] = ""
    if address != None:
        networkinfo["ip"] = address
        networkinfo["ip_sec1"] = address.split(".")[0]
        networkinfo["ip_sec2"] = address.split(".")[1]
        networkinfo["ip_sec3"] = address.split(".")[2]
        networkinfo["ip_sec4"] = address.split(".")[3]

    networkinfo["mask"] = ""
    networkinfo["mask_sec1"] = ""
    networkinfo["mask_sec2"] = ""
    networkinfo["mask_sec3"] = ""
    networkinfo["mask_sec4"] = ""
    if netmask != None:
        networkinfo["mask"] = netmask
        networkinfo["mask_sec1"] = netmask.split(".")[0]
        networkinfo["mask_sec2"] = netmask.split(".")[1]
        networkinfo["mask_sec3"] = netmask.split(".")[2]
        networkinfo["mask_sec4"] = netmask.split(".")[3]

    networkinfo["gateway"] = ""
    networkinfo["gateway_sec1"] = ""
    networkinfo["gateway_sec2"] = ""
    networkinfo["gateway_sec3"] = ""
    networkinfo["gateway_sec4"] = ""
    if gateway != None:
        networkinfo["gateway"] = gateway
        networkinfo["gateway_sec1"] = gateway.split(".")[0]
        networkinfo["gateway_sec2"] = gateway.split(".")[1]
        networkinfo["gateway_sec3"] = gateway.split(".")[2]
        networkinfo["gateway_sec4"] = gateway.split(".")[3]

    return networkinfo
Exemplo n.º 9
0
    def checkup_interfaces_file(self, filename):
        f = InterfacesFile(filename)

        self.assertRaises(KeyError, f.get_iface, 'non-existance-iface')

        lo = f.get_iface('lo')
        self.assertEquals(lo.startup.mode, 'auto')
        self.assertIsInstance(lo.startup, Auto)
        self.assertEquals(lo.name, 'lo')
        self.assertEquals(lo.address_family, 'inet')
        self.assertEquals(lo.method, 'loopback')

        eth0 = f.get_iface('eth0')
        self.assertEquals(eth0.startup.mode, 'auto')
        self.assertIsInstance(eth0.startup, Auto)
        self.assertEquals(eth0.name, 'eth0')
        self.assertEquals(eth0.address_family, 'inet')
        self.assertEquals(eth0.method, 'static')
        self.assertEquals(eth0.netmask, '255.255.255.240')
        self.assertEquals(eth0.broadcast, '192.168.11.15')
        self.assertEquals(eth0.network, '192.168.11.0')
        self.assertEquals(eth0.gateway, '192.168.11.1')
        self.assertEquals(eth0.dns_nameservers, '8.8.8.8 8.8.4.4')

        self.assertTrue(hasattr(eth0, 'netmask'))
        self.assertTrue('netmask' in eth0)
        self.assertFalse('lorem' in eth0)

        eth1 = f.get_iface('eth1')
        self.assertEquals(eth1.startup.mode, 'auto')
        self.assertIsInstance(eth1.startup, Auto)
        self.assertEquals(eth1.name, 'eth1')
        self.assertEquals(eth1.address_family, 'inet')
        self.assertEquals(eth1.method, 'dhcp')
        self.assertRaises(AttributeError, lambda: eth1.address)

        wlan0 = f.get_iface('wlan0')
        self.assertEquals(wlan0.startup.mode, 'allow-hotplug')
        self.assertIsInstance(wlan0.startup, Allow)
        self.assertEquals(wlan0.name, 'wlan0')
        self.assertEquals(wlan0.address_family, 'inet')
        self.assertEquals(wlan0.method, 'static')
        self.assertEquals(wlan0.address, '192.168.9.41')
        self.assertEquals(wlan0.netmask, '255.255.255.0')
        self.assertEquals(wlan0.broadcast, '192.168.9.255')
        self.assertEquals(wlan0.network, '192.168.9.0')
        self.assertEquals(wlan0.gateway, '192.168.9.1')
        self.assertEquals(wlan0.wpa_ssid, 'khayyam')
        self.assertEquals(wlan0.wpa_psk, '<-Wine&Is&Mine->')

        wlan1 = f.get_iface('wlan1')
        self.assertEquals(wlan1.startup.mode, 'auto')
        self.assertIsInstance(wlan1.startup, Auto)
        self.assertEquals(wlan1.name, 'wlan1')
        self.assertEquals(wlan1.address_family, 'inet')
        self.assertEquals(wlan1.method, 'dhcp')
        self.assertEquals(wlan1.wpa_ssid, 'Dorfak')
        self.assertEquals(wlan1.wpa_psk, 'ShoorPalangGoolakhTappeh')
        self.assertRaises(AttributeError, lambda: wlan1.address)

        eth2 = f.get_iface('eth2')
        self.assertEquals(eth2.startup.mode, 'auto')
        self.assertIsInstance(eth2.startup, Auto)
        self.assertEquals(eth2.name, 'eth2')
        self.assertEquals(eth2.script, '/usr/local/sbin/map-scheme')
        self.assertEquals(eth2.map_HOME, 'eth2-home')
        self.assertEquals(eth2.map_WORK, 'eth2-work')
        self.assertEquals(eth2.mappings, [
            ['map', 'HOME', 'eth2-home'],
            ['map', 'WORK', 'eth2-work']])

        eth2_home = f.get_iface('eth2-home')
        self.assertIsNone(eth2_home.startup)
        self.assertEquals(eth2_home.name, 'eth2-home')
        self.assertEquals(eth2_home.address_family, 'inet')
        self.assertEquals(eth2_home.method, 'static')
        self.assertEquals(eth2_home.address, '192.168.1.1')
        self.assertEquals(eth2_home.netmask, '255.255.255.0')
        self.assertEquals(eth2_home.up, 'flush-mail')

        return f