Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
agent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
7
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8416c48f
authored
Mar 12, 2015
by
Your Name
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix change_ip_windows()
Closes
#12
parent
1891008e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
20 deletions
+40
-20
windows/network.py
+40
-20
No files found.
windows/network.py
View file @
8416c48f
from
netaddr
import
IPNetwork
,
IPAddress
import
logging
from
subprocess
import
check_output
,
CalledProcessError
from
subprocess
import
check_output
,
PIPE
,
Popen
logger
=
logging
.
getLogger
()
...
...
@@ -38,8 +38,8 @@ def change_ip_windows(interfaces, dns):
changed
=
(
new_addrs_str
!=
old_addrs_str
or
set
(
nic
.
DefaultIPGateway
)
!=
set
([
conf
[
'gw4'
],
conf
[
'gw6'
]
]))
if
changed
or
1
:
set
(
nic
.
DefaultIPGateway
)
!=
set
([
conf
.
get
(
'gw4'
),
conf
(
'gw6'
)
]))
if
changed
or
1
:
# TODO
logger
.
info
(
'new config for <
%
s(
%
s)>:
%
s'
,
nic
.
Description
,
nic
.
MACAddress
,
', '
.
join
(
new_addrs_str
))
# IPv4
...
...
@@ -53,36 +53,56 @@ def change_ip_windows(interfaces, dns):
IPAddress
=
ipv4_addrs
,
SubnetMask
=
ipv4_masks
)
assert
retval
==
(
0
,
)
nic
.
SetGateways
(
DefaultIPGateway
=
[
conf
[
'gw4'
]
])
nic
.
SetGateways
(
DefaultIPGateway
=
[
conf
.
get
(
'gw4'
)
])
assert
retval
==
(
0
,
)
# IPv6
for
ip
in
new_addrs
:
if
ip
.
version
==
6
and
str
(
ip
)
not
in
old_addrs_str
:
logger
.
debug
(
'add
%
s (
%
s)'
,
ip
,
nic
.
Description
)
check_output
(
'netsh interface ipv6 add address '
'interface=
%
s address=
%
s'
%
(
nic
.
InterfaceIndex
,
ip
),
shell
=
True
)
logger
.
info
(
'add
%
s (
%
s)'
,
ip
,
nic
.
Description
)
try
:
p
=
Popen
((
'netsh interface ipv6 add address '
'interface=
%
s address=
%
s'
)
%
(
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
:
if
ip
.
version
==
6
and
str
(
ip
)
not
in
new_addrs_str
:
logger
.
debug
(
'delete
%
s (
%
s)'
,
ip
,
nic
.
Description
)
check_output
(
'netsh interface ipv6 delete address '
'interface=
%
s address=
%
s'
%
(
nic
.
InterfaceIndex
,
ip
.
ip
),
shell
=
True
)
logger
.
info
(
'del
%
s (
%
s)'
,
ip
,
nic
.
Description
)
try
:
p
=
Popen
((
'netsh interface ipv6 delete address '
'interface=
%
s address=
%
s'
)
%
(
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
:
check_output
(
'netsh interface ipv6 del route ::/0 interface=
%
s'
%
nic
.
InterfaceIndex
,
shell
=
True
)
except
CalledProcessError
:
pass
check_output
(
'netsh interface ipv6 add route ::/0 interface=
%
s
%
s'
%
(
nic
.
InterfaceIndex
,
conf
[
'gw6'
]),
shell
=
True
)
except
:
logger
.
exception
(
'Unhandled exception:'
)
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
:
check_output
(
'netsh interface ipv4 add dnsserver
%
s '
'address=
%
s index=1'
%
(
nic
.
InterfaceIndex
,
dns
),
shell
=
True
)
except
CalledProcessError
:
pass
except
:
logger
.
exception
(
'Unhandled exception:'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment