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
6be953f1
authored
Mar 18, 2015
by
Bach Dániel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix change_ip_windows() v2
parent
8416c48f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
71 additions
and
71 deletions
+71
-71
windows/network.py
+71
-71
No files found.
windows/network.py
View file @
6be953f1
from
netaddr
import
IPNetwork
,
IPAddress
from
netaddr
import
IPNetwork
,
IPAddress
import
logging
import
logging
from
subprocess
import
check_output
,
PIPE
,
Popen
from
subprocess
import
PIPE
,
Popen
logger
=
logging
.
getLogger
()
logger
=
logging
.
getLogger
()
...
@@ -17,6 +17,23 @@ ifcfg_template = '/etc/sysconfig/network-scripts/ifcfg-%s'
...
@@ -17,6 +17,23 @@ ifcfg_template = '/etc/sysconfig/network-scripts/ifcfg-%s'
# '8.8.8.8')
# '8.8.8.8')
class
IPAddress2
(
IPNetwork
):
def
key
(
self
):
return
self
.
_module
.
version
,
self
.
_value
,
self
.
_prefixlen
def
check_output2
(
cmd
,
shell
=
False
):
try
:
p
=
Popen
(
cmd
,
shell
=
shell
,
stderr
=
PIPE
,
stdout
=
PIPE
,
stdin
=
PIPE
)
stdout
,
stderr
=
p
.
communicate
()
logger
.
info
(
'
%
s:
%
s,
%
s'
,
cmd
,
stdout
,
stderr
)
return
stdout
except
:
logger
.
exception
(
'Unhandled exception in
%
s: '
,
cmd
)
def
get_interfaces_windows
(
interfaces
):
def
get_interfaces_windows
(
interfaces
):
import
wmi
import
wmi
nics
=
wmi
.
WMI
()
.
Win32_NetworkAdapterConfiguration
(
IPEnabled
=
True
)
nics
=
wmi
.
WMI
()
.
Win32_NetworkAdapterConfiguration
(
IPEnabled
=
True
)
...
@@ -29,80 +46,63 @@ def get_interfaces_windows(interfaces):
...
@@ -29,80 +46,63 @@ def get_interfaces_windows(interfaces):
def
change_ip_windows
(
interfaces
,
dns
):
def
change_ip_windows
(
interfaces
,
dns
):
for
nic
,
conf
in
get_interfaces_windows
(
interfaces
):
for
nic
,
conf
in
get_interfaces_windows
(
interfaces
):
link_local
=
IPNetwork
(
'fe80::/16'
)
link_local
=
IPNetwork
(
'fe80::/16'
)
new_addrs
=
[
IPNetwork
(
ip
)
for
ip
in
conf
[
'addresses'
]]
new_addrs
=
set
([
IPAddress2
(
ip
)
for
ip
in
conf
[
'addresses'
]])
new_addrs_str
=
set
(
str
(
ip
)
for
ip
in
new_addrs
)
old_addrs
=
set
([
IPAddress2
(
'
%
s/
%
s'
%
(
ip
,
nic
.
IPSubnet
[
i
]))
old_addrs
=
[
IPNetwork
(
'
%
s/
%
s'
%
(
ip
,
nic
.
IPSubnet
[
i
]))
for
i
,
ip
in
enumerate
(
nic
.
IPAddress
)
for
i
,
ip
in
enumerate
(
nic
.
IPAddress
)
if
IPAddress
(
ip
)
not
in
link_local
])
if
IPAddress
(
ip
)
not
in
link_local
]
old_addrs_str
=
set
(
str
(
ip
)
for
ip
in
old_addrs
)
addrs_add
=
new_addrs
-
old_addrs
addrs_del
=
old_addrs
-
new_addrs
changed
=
(
new_addrs_str
!=
old_addrs_str
or
changed
=
(
addrs_add
or
addrs_del
or
set
(
nic
.
DefaultIPGateway
)
!=
set
([
conf
.
get
(
'gw4'
),
conf
(
'gw6'
)]))
set
(
nic
.
DefaultIPGateway
)
!=
set
(
if
changed
or
1
:
# TODO
[
conf
.
get
(
'gw4'
),
conf
.
get
(
'gw6'
)]))
if
changed
:
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
(
conf
[
'addresses'
]))
# IPv4
ipv4_addrs
=
[
str
(
ip
.
ip
)
for
ip
in
new_addrs
for
ip
in
addrs_add
:
if
ip
.
version
==
4
]
logger
.
info
(
'add
%
s (
%
s)'
,
ip
,
nic
.
Description
)
ipv4_masks
=
[
str
(
ip
.
netmask
)
for
ip
in
new_addrs
if
ip
.
version
==
6
:
if
ip
.
version
==
4
]
cmd
=
(
logger
.
debug
(
'<
%
s>.EnableStatic(
%
s,
%
s) called'
,
nic
.
Description
,
'netsh interface ipv6 add address '
ipv4_addrs
,
ipv4_masks
)
'interface=
%
s address=
%
s'
retval
=
nic
.
EnableStatic
(
%
(
nic
.
InterfaceIndex
,
ip
))
IPAddress
=
ipv4_addrs
,
SubnetMask
=
ipv4_masks
)
else
:
assert
retval
==
(
0
,
)
cmd
=
(
'netsh interface ipv4 add address '
nic
.
SetGateways
(
DefaultIPGateway
=
[
conf
.
get
(
'gw4'
)])
'
%
s
%
s
%
s'
assert
retval
==
(
0
,
)
%
(
nic
.
InterfaceIndex
,
ip
.
ip
,
ip
.
netmask
))
# IPv6
check_output2
(
cmd
,
shell
=
True
)
for
ip
in
new_addrs
:
if
ip
.
version
==
6
and
str
(
ip
)
not
in
old_addrs_str
:
for
ip
in
addrs_del
:
logger
.
info
(
'add
%
s (
%
s)'
,
ip
,
nic
.
Description
)
proto
=
'ipv6'
if
ip
.
version
==
6
else
'ipv4'
try
:
logger
.
info
(
'del
%
s (
%
s)'
,
ip
,
nic
.
Description
)
p
=
Popen
((
check_output2
(
'netsh interface ipv6 add address '
'netsh interface
%
s delete address '
'interface=
%
s address=
%
s'
)
'
%
s
%
s'
%
(
nic
.
InterfaceIndex
,
ip
),
shell
=
True
,
%
(
proto
,
nic
.
InterfaceIndex
,
ip
.
ip
),
shell
=
True
)
stderr
=
PIPE
,
stdout
=
PIPE
,
stdin
=
PIPE
)
logger
.
info
(
'netsh_add():
%
s'
,
p
.
communicate
())
# default gw4
except
:
if
conf
.
get
(
'gw4'
):
logger
.
exception
(
check_output2
(
'Unhandled exception in netsh_add(): '
)
'netsh interface ip del route 0.0.0.0/0 interface=
%
s'
%
nic
.
InterfaceIndex
,
shell
=
True
)
for
ip
in
old_addrs
:
check_output2
(
if
ip
.
version
==
6
and
str
(
ip
)
not
in
new_addrs_str
:
'netsh interface ip add route 0.0.0.0/0 interface=
%
s
%
s'
logger
.
info
(
'del
%
s (
%
s)'
,
ip
,
nic
.
Description
)
%
(
nic
.
InterfaceIndex
,
conf
.
get
(
'gw4'
)),
shell
=
True
)
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
# default gw6
try
:
if
conf
.
get
(
'gw6'
):
check_output
(
'netsh interface ipv6 del route ::/0 interface=
%
s'
check_output2
(
%
nic
.
InterfaceIndex
,
shell
=
True
)
'netsh interface ipv6 del route ::/0 interface=
%
s'
except
:
%
nic
.
InterfaceIndex
,
shell
=
True
)
logger
.
exception
(
'Unhandled exception:'
)
check_output2
(
try
:
check_output
(
'netsh interface ipv6 add route ::/0 interface=
%
s
%
s'
'netsh interface ipv6 add route ::/0 interface=
%
s
%
s'
%
(
nic
.
InterfaceIndex
,
conf
.
get
(
'gw6'
)),
shell
=
True
)
%
(
nic
.
InterfaceIndex
,
conf
.
get
(
'gw6'
)),
shell
=
True
)
except
:
logger
.
exception
(
'Unhandled exception:'
)
# DNS
# DNS
try
:
check_output2
(
'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
:
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