Commit 8416c48f by Your Name

fix change_ip_windows()

Closes #12
parent 1891008e
from netaddr import IPNetwork, IPAddress from netaddr import IPNetwork, IPAddress
import logging import logging
from subprocess import check_output, CalledProcessError from subprocess import check_output, PIPE, Popen
logger = logging.getLogger() logger = logging.getLogger()
...@@ -38,8 +38,8 @@ def change_ip_windows(interfaces, dns): ...@@ -38,8 +38,8 @@ def change_ip_windows(interfaces, dns):
changed = ( changed = (
new_addrs_str != old_addrs_str or new_addrs_str != old_addrs_str or
set(nic.DefaultIPGateway) != set([conf['gw4'], conf['gw6']])) set(nic.DefaultIPGateway) != set([conf.get('gw4'), conf('gw6')]))
if changed or 1: if changed or 1: # TODO
logger.info('new config for <%s(%s)>: %s', nic.Description, logger.info('new config for <%s(%s)>: %s', nic.Description,
nic.MACAddress, ', '.join(new_addrs_str)) nic.MACAddress, ', '.join(new_addrs_str))
# IPv4 # IPv4
...@@ -53,36 +53,56 @@ def change_ip_windows(interfaces, dns): ...@@ -53,36 +53,56 @@ def change_ip_windows(interfaces, dns):
IPAddress=ipv4_addrs, SubnetMask=ipv4_masks) IPAddress=ipv4_addrs, SubnetMask=ipv4_masks)
assert retval == (0, ) assert retval == (0, )
nic.SetGateways(DefaultIPGateway=[conf['gw4']]) nic.SetGateways(DefaultIPGateway=[conf.get('gw4')])
assert retval == (0, ) assert retval == (0, )
# IPv6 # IPv6
for ip in new_addrs: for ip in new_addrs:
if ip.version == 6 and str(ip) not in old_addrs_str: if ip.version == 6 and str(ip) not in old_addrs_str:
logger.debug('add %s (%s)', ip, nic.Description) logger.info('add %s (%s)', ip, nic.Description)
check_output( try:
p = Popen((
'netsh interface ipv6 add address ' 'netsh interface ipv6 add address '
'interface=%s address=%s' 'interface=%s address=%s')
% (nic.InterfaceIndex, ip), shell=True) % (nic.InterfaceIndex, ip), shell=True,
stderr=PIPE, stdout=PIPE, stdin=PIPE)
logger.info('netsh_add(): %s', p.communicate())
except:
logger.exception(
'Unhandled exception in netsh_add(): ')
for ip in old_addrs: for ip in old_addrs:
if ip.version == 6 and str(ip) not in new_addrs_str: if ip.version == 6 and str(ip) not in new_addrs_str:
logger.debug('delete %s (%s)', ip, nic.Description) logger.info('del %s (%s)', ip, nic.Description)
check_output( try:
p = Popen((
'netsh interface ipv6 delete address ' 'netsh interface ipv6 delete address '
'interface=%s address=%s' 'interface=%s address=%s')
% (nic.InterfaceIndex, ip.ip), shell=True) % (nic.InterfaceIndex, ip), shell=True,
stderr=PIPE, stdout=PIPE, stdin=PIPE)
logger.info('netsh_add(): %s', p.communicate())
except:
logger.exception(
'Unhandled exception in netsh_del(): ')
# default gw6
try: try:
check_output('netsh interface ipv6 del route ::/0 interface=%s' check_output('netsh interface ipv6 del route ::/0 interface=%s'
% nic.InterfaceIndex, shell=True) % nic.InterfaceIndex, shell=True)
except CalledProcessError: except:
pass logger.exception('Unhandled exception:')
check_output('netsh interface ipv6 add route ::/0 interface=%s %s'
% (nic.InterfaceIndex, conf['gw6']), shell=True) try:
check_output(
'netsh interface ipv6 add route ::/0 interface=%s %s'
% (nic.InterfaceIndex, conf.get('gw6')), shell=True)
except:
logger.exception('Unhandled exception:')
# DNS
try: try:
check_output('netsh interface ipv4 add dnsserver %s ' check_output('netsh interface ipv4 add dnsserver %s '
'address=%s index=1' 'address=%s index=1'
% (nic.InterfaceIndex, dns), shell=True) % (nic.InterfaceIndex, dns), shell=True)
except CalledProcessError: except:
pass logger.exception('Unhandled exception:')
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