-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace WMIC with powershell cmd #4034
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1654,9 +1654,15 @@ def set_guest_ip_addr(session, mac, ip_addr, netmask="255.255.255.0", os_type="l | |
session.cmd(cmd, timeout=360) | ||
elif os_type == "windows": | ||
info_cmd = "ipconfig /all" | ||
get_nic_cmd = ( | ||
'powershell -command "$Adapter=Get-CimInstance Win32_NetworkAdapterConfiguration | ' | ||
"Where-Object {$_.MACAddress -eq '%s'}; $Adapter.InterfaceIndex\"" | ||
) % mac | ||
nic_index = session.cmd_output(get_nic_cmd, timeout=120) | ||
prefix_length = subnet_mask_to_prefix_length(netmask) | ||
cmd = ( | ||
"wmic nicconfig where MACAddress='%s' call " | ||
"enablestatic '%s','%s'" % (mac, ip_addr, netmask) | ||
"powershell -command \"New-NetIPAddress -InterfaceIndex %s -IPAddress '%s' -PrefixLength %s\"" | ||
% (nic_index, ip_addr, prefix_length) | ||
) | ||
session.cmd(cmd, timeout=360) | ||
else: | ||
|
@@ -1667,6 +1673,24 @@ def set_guest_ip_addr(session, mac, ip_addr, netmask="255.255.255.0", os_type="l | |
raise IPAddrSetError(mac, ip_addr, err) | ||
|
||
|
||
def subnet_mask_to_prefix_length(subnet_mask): | ||
""" | ||
Convert subnet_mask from 255.*** to prefix length | ||
|
||
:param subnet_mask: nic subnet_mask | ||
|
||
:return: prefix length | ||
""" | ||
|
||
octets = subnet_mask.split(".") | ||
|
||
prefix_length = 0 | ||
for octet in octets: | ||
prefix_length += bin(int(octet)).count("1") | ||
|
||
return prefix_length | ||
|
||
|
||
def get_guest_nameserver(session): | ||
""" | ||
Get guest nameserver from serial session for linux guest | ||
|
@@ -3788,7 +3812,10 @@ def str2ipaddr(str_ip): | |
return None | ||
|
||
maps = {} | ||
cmd = "wmic nicconfig where IPEnabled=True get ipaddress, macaddress" | ||
cmd = ( | ||
'powershell -command "Get-CimInstance Win32_NetworkAdapterConfiguration | ' | ||
"Where-Object {$_.IPEnabled -eq 'True'} | Select-Object IPAddress, MACAddress\"" | ||
) | ||
out = session.cmd_output(cmd) | ||
regex = r".*\w{2}[:-]\w{2}[:-]\w{2}[:-]\w{2}[:-]\w{2}[:-]\w{2}\s*" | ||
lines = [l.strip() for l in out.splitlines() if l.strip()] | ||
|
@@ -3941,19 +3968,22 @@ def update_mac_ip_address(vm, timeout=240): | |
|
||
|
||
def get_windows_nic_attribute( | ||
session, key, value, target, timeout=240, global_switch="nic" | ||
session, key, value, target, timeout=240, global_switch="NetworkAdapter" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @xiagao |
||
): | ||
""" | ||
Get the windows nic attribute using wmic. All the support key you can | ||
using wmic to have a check. | ||
Get the windows nic attribute using powershell. All the support key you can | ||
using powershell to have a check. | ||
|
||
:param session: session to the virtual machine | ||
:param key: the key supported by wmic | ||
:param key: the key supported by Get-CimInstance | ||
:param value: the value of the key | ||
:param target: which nic attribute you want to get. | ||
|
||
""" | ||
cmd = 'wmic %s where %s="%s" get %s' % (global_switch, key, value, target) | ||
cmd = ( | ||
"powershell -command \"Get-CimInstance Win32_%s | Where-Object {$_.%s -eq '%s'} | Select-Object %s\"" | ||
% (global_switch, key, value, target) | ||
) | ||
status, out = session.cmd_status_output(cmd, timeout=timeout) | ||
if status != 0: | ||
err_msg = "Execute guest shell command('%s') " "failed with error: '%s'" % ( | ||
|
@@ -3993,7 +4023,7 @@ def restart_windows_guest_network(session, connection_id, timeout=240, mode="net | |
|
||
:param session: session to virtual machine | ||
:param connection_id: windows nic connectionid,it means connection name, | ||
you Can get connection id string via wmic | ||
you Can get connection id string via wmic or powershell | ||
""" | ||
if mode == "netsh": | ||
disable_windows_guest_network(session, connection_id, timeout=timeout) | ||
|
@@ -4011,7 +4041,7 @@ def restart_windows_guest_network_by_key( | |
using devcon mode must download devcon.exe and put it under c:\ | ||
|
||
:param session: session to virtual machine | ||
:param key: the key supported by wmic nic | ||
:param key: the key supported by Get-CimInstance nic | ||
:param value: the value of the key | ||
:param timeout: timeout | ||
:param mode: command mode netsh or devcon | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,18 +1,28 @@ | ||||||
""" | ||||||
Windows drive utilities | ||||||
""" | ||||||
|
||||||
from virttest import utils_misc | ||||||
|
||||||
from . import wmic | ||||||
import re | ||||||
|
||||||
|
||||||
def _logical_disks(session, cond=None, props=None): | ||||||
cmd = wmic.make_query("LogicalDisk", cond, props, get_swch=wmic.FMT_TYPE_LIST) | ||||||
out = utils_misc.wait_for( | ||||||
lambda: wmic.parse_list(session.cmd(cmd, timeout=120)), 240 | ||||||
) | ||||||
return out if out else [] | ||||||
c_name, c_value = cond.split("=") | ||||||
cmd = ( | ||||||
'powershell -command "Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object {$_.%s -like %s}' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The disk needs to be under single quotes, same you've done in utils_netperf.py and replace the existing ones by double quotes
Suggested change
|
||||||
' | Select-Object %s | Format-List *"' | ||||||
) % (c_name, c_value, ",".join(props)) | ||||||
out = session.cmd(cmd, timeout=120) | ||||||
results = [] | ||||||
for para in re.split("(?:\r?\n){2,}", out.strip()): | ||||||
keys, vals = [], [] | ||||||
for line in para.splitlines(): | ||||||
key, val = line.split(":", 1) | ||||||
keys.append(key.strip()) | ||||||
vals.append(val.strip()) | ||||||
if len(keys) == 1: | ||||||
results.append(vals[0]) | ||||||
else: | ||||||
results.append(dict(zip(keys, vals))) | ||||||
return results if results else [] | ||||||
|
||||||
|
||||||
def get_hard_drive_letter(session, label): | ||||||
|
@@ -24,7 +34,7 @@ def get_hard_drive_letter(session, label): | |||||
|
||||||
:return: Hard drive's letter if found, otherwise `None`. | ||||||
""" | ||||||
cond = "VolumeName like '%s'" % label | ||||||
cond = "VolumeName='%s'" % label | ||||||
try: | ||||||
return _logical_disks(session, cond=cond, props=["DeviceID"])[0] | ||||||
except IndexError: | ||||||
|
@@ -103,13 +113,21 @@ def get_disk_props_by_serial_number(session, serial_number, props): | |||||
:return: The mapping between properties and values. | ||||||
:rtype: dict | ||||||
""" | ||||||
cond = "SerialNumber like '%s'" % serial_number | ||||||
cmd = wmic.make_query("diskdrive", cond, props=props, get_swch=wmic.FMT_TYPE_LIST) | ||||||
out = wmic.parse_list(session.cmd(cmd, timeout=120)) | ||||||
|
||||||
if out: | ||||||
mapping = out[-1] | ||||||
if isinstance(mapping, str): | ||||||
return {props[0]: mapping} | ||||||
return mapping | ||||||
return {} | ||||||
cmd = ( | ||||||
'powershell -command "Get-CimInstance -ClassName Win32_Diskdrive | Where-Object {$_.SerialNumber -eq %s}' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above
Suggested change
|
||||||
' | Select-Object %s | Format-List *"' | ||||||
) % (serial_number, ",".join(props)) | ||||||
out = session.cmd(cmd, timeout=120) | ||||||
results = [] | ||||||
for para in re.split("(?:\r?\n){2,}", out.strip()): | ||||||
print(para) | ||||||
keys, vals = [], [] | ||||||
for line in para.splitlines(): | ||||||
key, val = line.split(":", 1) | ||||||
keys.append(key.strip()) | ||||||
vals.append(val.strip()) | ||||||
if len(keys) == 1: | ||||||
results.append(vals[0]) | ||||||
else: | ||||||
results.append(dict(zip(keys, vals))) | ||||||
return results if results else [] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @leidwang |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @PaulYuuu
Would you like to help to review it?
Thanks. CC @nickzhq
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please note that the netmask in
set_guest_ip_addr
can be CIDR AND dot-decimal notation, only when users give a dot-decimal notation, we have to convert it to a bitmask.However, the new function is a duplicate of
convert_netmask
inutils_net
module, andset_guest_ip_addr
also used it.avocado-vt/virttest/utils_net.py
Lines 1641 to 1647 in 731ecab
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, the current
convert_netmask
can only handle dot-decimal notation, as we already importipaddress
, an enhancement here is to use it to handle all types of netmask.And then you can use it for linux/windows