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
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
62 deletions
+62
-62
windows/network.py
+62
-62
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
=
(
changed
=
(
addrs_add
or
addrs_del
or
new_addrs_str
!=
old_addrs_str
or
set
(
nic
.
DefaultIPGateway
)
!=
set
(
set
(
nic
.
DefaultIPGateway
)
!=
set
([
conf
.
get
(
'gw4'
),
conf
(
'gw6'
)]))
[
conf
.
get
(
'gw4'
),
conf
.
get
(
'gw6'
)]))
if
changed
or
1
:
# TODO
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
]
ipv4_masks
=
[
str
(
ip
.
netmask
)
for
ip
in
new_addrs
if
ip
.
version
==
4
]
logger
.
debug
(
'<
%
s>.EnableStatic(
%
s,
%
s) called'
,
nic
.
Description
,
ipv4_addrs
,
ipv4_masks
)
retval
=
nic
.
EnableStatic
(
IPAddress
=
ipv4_addrs
,
SubnetMask
=
ipv4_masks
)
assert
retval
==
(
0
,
)
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
.
info
(
'add
%
s (
%
s)'
,
ip
,
nic
.
Description
)
logger
.
info
(
'add
%
s (
%
s)'
,
ip
,
nic
.
Description
)
try
:
if
ip
.
version
==
6
:
p
=
Popen
(
(
cmd
=
(
'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
))
stderr
=
PIPE
,
stdout
=
PIPE
,
stdin
=
PIPE
)
else
:
logger
.
info
(
'netsh_add():
%
s'
,
p
.
communicate
())
cmd
=
(
except
:
'netsh interface ipv4 add address '
logger
.
exception
(
'
%
s
%
s
%
s'
'Unhandled exception in netsh_add(): '
)
%
(
nic
.
InterfaceIndex
,
ip
.
ip
,
ip
.
netmask
)
)
for
ip
in
old_addrs
:
check_output2
(
cmd
,
shell
=
True
)
if
ip
.
version
==
6
and
str
(
ip
)
not
in
new_addrs_str
:
for
ip
in
addrs_del
:
proto
=
'ipv6'
if
ip
.
version
==
6
else
'ipv4'
logger
.
info
(
'del
%
s (
%
s)'
,
ip
,
nic
.
Description
)
logger
.
info
(
'del
%
s (
%
s)'
,
ip
,
nic
.
Description
)
try
:
check_output2
(
p
=
Popen
((
'netsh interface
%
s delete address '
'netsh interface ipv6 delete address '
'
%
s
%
s'
'interface=
%
s address=
%
s'
)
%
(
proto
,
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
# default gw4
try
:
if
conf
.
get
(
'gw4'
):
check_output
(
'netsh interface ipv6 del route ::/0 interface=
%
s'
check_output2
(
'netsh interface ip del route 0.0.0.0/0 interface=
%
s'
%
nic
.
InterfaceIndex
,
shell
=
True
)
%
nic
.
InterfaceIndex
,
shell
=
True
)
except
:
check_output2
(
logger
.
exception
(
'Unhandled exception:'
)
'netsh interface ip add route 0.0.0.0/0 interface=
%
s
%
s'
%
(
nic
.
InterfaceIndex
,
conf
.
get
(
'gw4'
)),
shell
=
True
)
try
:
# default gw6
check_output
(
if
conf
.
get
(
'gw6'
):
check_output2
(
'netsh interface ipv6 del route ::/0 interface=
%
s'
%
nic
.
InterfaceIndex
,
shell
=
True
)
check_output2
(
'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