Commit 5b6838d4 by Czémán Arnold

More dns support

Solves issue: #19
parent 26858401
......@@ -102,7 +102,8 @@ class Context(BaseContext):
@staticmethod
def change_ip(interfaces, dns):
change_ip_freebsd(interfaces, dns)
nameservers = dns.replace(' ', '').split(',')
change_ip_freebsd(interfaces, nameservers)
@staticmethod
def set_time(time):
......
......@@ -33,7 +33,7 @@ def remove_interfaces_freebsd(devices):
"unable to remove interface configuration: " + if_file)
def change_ip_freebsd(interfaces, dns):
def change_ip_freebsd(interfaces, nameservers):
data = list(get_interfaces_freebsd(interfaces))
for ifname, conf in data:
subprocess.call(('/usr/sbin/service', 'netif', 'stop', ifname))
......@@ -69,7 +69,8 @@ def change_ip_freebsd(interfaces, dns):
f.write("defaultrouter=\""+str(conf['gw4'])+"\"\n")
with open("/etc/resolv.conf", "w") as f:
f.write("nameserver "+dns)
for ns in nameservers:
f.write("nameserver %s\n" % ns)
for ifname, conf in data:
subprocess.call(('/usr/sbin/service', 'netif', 'start', ifname))
......
......@@ -89,10 +89,11 @@ class Context(BaseContext):
@staticmethod
def change_ip(interfaces, dns):
nameservers = dns.replace(' ', '').split(',')
if distro == 'debian':
change_ip_ubuntu(interfaces, dns)
change_ip_ubuntu(interfaces, nameservers)
elif distro == 'rhel':
change_ip_rhel(interfaces, dns)
change_ip_rhel(interfaces, nameservers)
@staticmethod
def set_time(time):
......
......@@ -52,7 +52,7 @@ def remove_interfaces_ubuntu(devices):
print line
def change_ip_ubuntu(interfaces, dns):
def change_ip_ubuntu(interfaces, nameservers):
data = list(get_interfaces_linux(interfaces))
for ifname, conf in data:
......@@ -91,7 +91,7 @@ def change_ip_ubuntu(interfaces, dns):
'ip': ip,
'prefixlen': prefixlen,
'gw': conf['gw6' if ip.version == 6 else 'gw4'],
'dns': dns})
'dns': ' '.join(nameservers)})
for ifname, conf in data:
subprocess.call(('/sbin/ifup', ifname))
......@@ -106,19 +106,24 @@ def change_ip_ubuntu(interfaces, dns):
# '8.8.8.8')
def change_ip_rhel(interfaces, dns):
def change_ip_rhel(interfaces, nameservers):
for ifname, conf in get_interfaces_linux(interfaces):
subprocess.call(('/sbin/ifdown', ifname))
subprocess.call(('/sbin/ip', 'addr', 'flush', 'dev', ifname))
subprocess.call(('/sbin/ip', 'link', 'set', 'dev', ifname, 'down'))
with open(ifcfg_template % ifname,
'w') as f:
with open(ifcfg_template % ifname, 'w') as f:
f.write('DEVICE=%s\n'
'DNS1=%s\n'
'BOOTPROTO=none\n'
'NM_CONTROLLED=no\n'
'USERCTL=no\n'
'ONBOOT=yes\n' % (ifname, dns))
'ONBOOT=yes\n' % ifname)
if len(nameservers) == 0:
f.write('PEERDNS=no')
if len(nameservers) >= 1:
f.write('DNS1=%s\n' % nameservers[0])
if len(nameservers) >= 2:
f.write('DNS2=%s\n' % nameservers[1])
for i in conf['addresses']:
ip_with_prefix = IPNetwork(i)
ip = ip_with_prefix.ip
......
......@@ -39,7 +39,8 @@ class Context(BaseContext):
@staticmethod
def change_ip(interfaces, dns):
change_ip_windows(interfaces, dns)
nameservers = dns.replace(' ', '').split(',')
change_ip_windows(interfaces, nameservers)
@staticmethod
def set_time(time):
......
......@@ -43,7 +43,7 @@ def get_interfaces_windows(interfaces):
yield nic, conf
def change_ip_windows(interfaces, dns):
def change_ip_windows(interfaces, nameservers):
for nic, conf in get_interfaces_windows(interfaces):
link_local = IPNetwork('fe80::/16')
new_addrs = set([IPAddress2(ip) for ip in conf['addresses']])
......@@ -103,6 +103,9 @@ def change_ip_windows(interfaces, dns):
% (nic.InterfaceIndex, conf.get('gw6')), shell=True)
# DNS
check_output2('netsh interface ipv4 add dnsserver %s '
'address=%s index=1'
% (nic.InterfaceIndex, dns), shell=True)
index = 1
for ns in nameservers:
check_output2('netsh interface ipv4 add dnsserver %s '
'address=%s index=%i'
% (nic.InterfaceIndex, ns, index), shell=True)
index += 1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment