1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

Add windows systeminfo command parser

This commit is contained in:
Jon Smith
2021-03-24 14:13:22 -05:00
parent a2e8b3c7b6
commit ee8f06cbdb
6 changed files with 688 additions and 0 deletions

209
jc/parsers/systeminfo.py Normal file
View File

@ -0,0 +1,209 @@
"""jc - JSON CLI output utility `systeminfo` command output parser
Parses Windows "systeminfo" command. Multiline values such as
hotfixes or network cards are unparsed.
Usage (cli):
$ systeminfo | jc --systeminfo
Usage (module):
import jc.parsers.systeminfo
result = jc.parsers.systeminfo.parse(systeminfo_command_output)
Compatibility:
'win32'
Examples:
$ systeminfo | jc --systeminfo -p
{
"host_name": "DESKTOP-WIN01",
"os_name": "Microsoft Windows 10 Enterprise",
"os_version": "10.0.19042 N/A Build 19042",
"os_manufacturer": "Microsoft Corporation",
"os_configuration": "Member Workstation",
"os_build_type": "Multiprocessor Free",
"registered_owner": "User",
"registered_organization": "",
"product_id": "00111-12345-00001-AA111",
"original_install_date": "2/16/2021, 11:20:27 AM",
"system_boot_time": "3/19/2021, 9:25:03 AM",
"system_manufacturer": "VMware, Inc.",
"system_model": "VMware7,1",
"system_type": "x64-based PC",
"processors": "1 Processor(s) Installed.\n [01]: ...",
"bios_version": "VMware, Inc. VMW71.00V.11111111.B64.2008100111, 8/10/2020",
"windows_directory": "C:\\Windows",
"system_directory": "C:\\Windows\\system32",
"boot_device": "\\Device\\HarddiskVolume1",
"system_locale": "en-us;English (United States)",
"input_locale": "en-us;English (United States)",
"time_zone": "(UTC-08:00) Pacific Time (US & Canada)",
"total_physical_memory": "2,047 MB",
"available_physical_memory": "1,417 MB",
"virtual_memory_max_size": "2,687 MB",
"virtual_memory_available": "1,482 MB",
"virtual_memory_in_use": "1,205 MB",
"page_file_locations": "C:\\pagefile.sys",
"domain": "TEST.local",
"logon_server": "\\\\WIN-AA1A1A11AAA",
"hotfixs": "6 Hotfix(s) Installed.\n [01]: KB4578...",
"network_cards": "1 NIC(s) Installed.\n [01]: Int...",
"hyperv_requirements": "A hypervisor has been detected. Features required fo..."
}
"""
import jc.utils
class info:
version = "1.0"
description = "Windows systeminfo command parser"
author = "Jon Smith"
author_email = "jon@rebelliondefense.com"
# details = 'enter any other details here'
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
compatible = ["win32"]
magic_commands = ["systeminfo"]
__version__ = info.version
def process(proc_data):
"""
Final processing to conform to the schema.
Parameters:
proc_data: (List of Dictionaries) raw structured data to process
Returns:
List of Dictionaries. Some keys are optional. Example: a non-virtualized server will not have
the "hyperv_requirements" key. Structured data with the following schema:
[
{
"host_name": "string",
"os_name": "string",
"os_version": "string",
"os_manufacturer": "string",
"os_configuration": "string",
"os_build_type": "string",
"registered_owner": "string",
"registered_organization": "string",
"product_id": "string",
"original_install_date": "string",
"system_boot_time": "string",
"system_manufacturer": "string",
"system_model": "string",
"system_type": "string",
"processors": "string",
"bios_version": "string",
"windows_directory": "string",
"system_directory": "string",
"boot_device": "string",
"system_locale": "string",
"input_locale": "string",
"time_zone": "string",
"total_physical_memory": "string",
"available_physical_memory": "string",
"virtual_memory_max_size": "string",
"virtual_memory_available": "string",
"virtual_memory_in_use": "string",
"page_file_locations": "string",
"domain": "string",
"logon_server": "string",
"hotfixs": "string",
"network_cards": "string",
"hyperv_requirements": "string"
}
]
"""
# rebuild output for added semantic information
return proc_data
def parse(data, raw=False, quiet=False):
"""
Main text parsing function
Parameters:
data: (string) text data to parse
raw: (boolean) output preprocessed JSON if True
quiet: (boolean) suppress warning messages if True
Returns:
List of Dictionaries. Raw or processed structured data.
"""
if not quiet:
jc.utils.compatibility(__name__, info.compatible)
delim = ":" # k/v delimiter
raw_data = {} # intermediary output
if jc.utils.has_data(data):
# keepends = True, so that multiline data retains return chars
lines = [line for line in data.splitlines(keepends=True) if line.strip() != ""]
# find the character position of the value in the k/v pair of the first line
# all subsequent lines of data use the same character position
start_value_pos = get_value_pos(lines[0], delim)
last_key = None
for line in lines:
key = line[0:start_value_pos]
value = line[start_value_pos:]
# possible multiline data
if last_key:
# the value data doesn't start where it should
# so this is multiline data
if delim not in key:
raw_data[last_key] += line
continue
raw_data[key] = value
last_key = key
# clean up keys; strip values
raw_output = {}
for k, v in raw_data.items():
# lowercase and replace spaces with underscores
k = k.strip().lower().replace(' ', '_')
# remove invalid key characters
for c in ';:!@#$%^&*()-':
k = k.replace(c, '')
# since we split on start_value_pos, the delimiter
# is still in the key field. Remove it.
k = k.rstrip(delim)
raw_output[k] = v.strip()
if raw:
return raw_output
else:
return process(raw_output)
def get_value_pos(line, delim):
"""
Finds the first non-whitespace character after the delimiter
Parameters:
line: (string) Input string
delim: (string) The data delimiter
"""
fields = line.split(delim, 1)
if not len(fields) == 2:
raise Exception(f"Expected a '{delim}' delimited field. Actual: {line}")
return len(line) - len(fields[1].lstrip())

View File

@ -0,0 +1,35 @@
{
"host_name": "DESKTOP-WIN01",
"os_name": "Microsoft Windows 10 Enterprise",
"os_version": "10.0.19042 N/A Build 19042",
"os_manufacturer": "Microsoft Corporation",
"os_configuration": "Member Workstation",
"os_build_type": "Multiprocessor Free",
"registered_owner": "User",
"registered_organization": "",
"product_id": "00111-12345-00001-AA111",
"original_install_date": "2/16/2021, 11:20:27 AM",
"system_boot_time": "3/19/2021, 9:25:03 AM",
"system_manufacturer": "VMware, Inc.",
"system_model": "VMware7,1",
"system_type": "x64-based PC",
"processors": "1 Processor(s) Installed.\n [01]: Intel64 Family 6 Model 158 Stepping 13 GenuineIntel ~2400 Mhz",
"bios_version": "VMware, Inc. VMW71.00V.11111111.B64.2008100111, 8/10/2020",
"windows_directory": "C:\\Windows",
"system_directory": "C:\\Windows\\system32",
"boot_device": "\\Device\\HarddiskVolume1",
"system_locale": "en-us;English (United States)",
"input_locale": "en-us;English (United States)",
"time_zone": "(UTC-08:00) Pacific Time (US & Canada)",
"total_physical_memory": "2,047 MB",
"available_physical_memory": "1,417 MB",
"virtual_memory_max_size": "2,687 MB",
"virtual_memory_available": "1,482 MB",
"virtual_memory_in_use": "1,205 MB",
"page_file_locations": "C:\\pagefile.sys",
"domain": "TEST.local",
"logon_server": "\\\\WIN-AA1A1A11AAA",
"hotfixs": "6 Hotfix(s) Installed.\n [01]: KB4578968\n [02]: KB4562830\n [03]: KB4570334\n [04]: KB4580325\n [05]: KB4586864\n [06]: KB4594440",
"network_cards": "1 NIC(s) Installed.\n [01]: Intel(R) 82574L Gigabit Network Connection\n Connection Name: Ethernet0\n DHCP Enabled: Yes\n DHCP Server: 192.168.133.250\n IP address(es)\n [01]: 192.168.133.3\n [02]: fe80::192:eb64:1fcf:86eb",
"hyperv_requirements": "A hypervisor has been detected. Features required for Hyper-V will not be displayed."
}

View File

@ -0,0 +1,47 @@
Host Name: DESKTOP-WIN01
OS Name: Microsoft Windows 10 Enterprise
OS Version: 10.0.19042 N/A Build 19042
OS Manufacturer: Microsoft Corporation
OS Configuration: Member Workstation
OS Build Type: Multiprocessor Free
Registered Owner: User
Registered Organization:
Product ID: 00111-12345-00001-AA111
Original Install Date: 2/16/2021, 11:20:27 AM
System Boot Time: 3/19/2021, 9:25:03 AM
System Manufacturer: VMware, Inc.
System Model: VMware7,1
System Type: x64-based PC
Processor(s): 1 Processor(s) Installed.
[01]: Intel64 Family 6 Model 158 Stepping 13 GenuineIntel ~2400 Mhz
BIOS Version: VMware, Inc. VMW71.00V.11111111.B64.2008100111, 8/10/2020
Windows Directory: C:\Windows
System Directory: C:\Windows\system32
Boot Device: \Device\HarddiskVolume1
System Locale: en-us;English (United States)
Input Locale: en-us;English (United States)
Time Zone: (UTC-08:00) Pacific Time (US & Canada)
Total Physical Memory: 2,047 MB
Available Physical Memory: 1,417 MB
Virtual Memory: Max Size: 2,687 MB
Virtual Memory: Available: 1,482 MB
Virtual Memory: In Use: 1,205 MB
Page File Location(s): C:\pagefile.sys
Domain: TEST.local
Logon Server: \\WIN-AA1A1A11AAA
Hotfix(s): 6 Hotfix(s) Installed.
[01]: KB4578968
[02]: KB4562830
[03]: KB4570334
[04]: KB4580325
[05]: KB4586864
[06]: KB4594440
Network Card(s): 1 NIC(s) Installed.
[01]: Intel(R) 82574L Gigabit Network Connection
Connection Name: Ethernet0
DHCP Enabled: Yes
DHCP Server: 192.168.133.250
IP address(es)
[01]: 192.168.133.3
[02]: fe80::192:eb64:1fcf:86eb
Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed.

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,298 @@
Host Name: TEST
OS Name: Microsoft Windows 7 Professional
OS Version: 6.1.7601 Service Pack 1 Build 7601
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Workstation
OS Build Type: Multiprocessor Free
Registered Owner: jdoe
Registered Organization:
Product ID: 00000-111-1111111-11111
Original Install Date: 01/01/2015, 12:00:00 PM
System Boot Time: 3/22/2021, 1:15:51 PM
System Manufacturer: LENOVO
System Model: 11111AA
System Type: x64-based PC
Processor(s): 1 Processor(s) Installed.
[01]: Intel64 Family 6 Model 42 Stepping 7 GenuineIntel ~2501 Mhz
BIOS Version: LENOVO 83ET67WW (1.37 ), 11/28/2011
Windows Directory: C:\Windows
System Directory: C:\Windows\system32
Boot Device: \Device\HarddiskVolume1
System Locale: en-us;English (United States)
Input Locale: en-us;English (United States)
Time Zone: (UTC-06:00) Central Time (US & Canada)
Total Physical Memory: 8,075 MB
Available Physical Memory: 1,620 MB
Virtual Memory: Max Size: 16,149 MB
Virtual Memory: Available: 6,468 MB
Virtual Memory: In Use: 9,681 MB
Page File Location(s): C:\pagefile.sys
Domain: WORKGROUP
Logon Server: \\TEST
Hotfix(s): 302 Hotfix(s) Installed.
[01]: KB2849697
[02]: KB2849696
[03]: KB2841134
[04]: KB2670838
[05]: KB2830477
[06]: KB2592687
[07]: KB971033
[08]: KB2479943
[09]: KB2491683
[10]: KB2506014
[11]: KB2506212
[12]: KB2506928
[13]: KB2509553
[14]: KB2511455
[15]: KB2515325
[16]: KB2532531
[17]: KB2533552
[18]: KB2533623
[19]: KB2534366
[20]: KB2536275
[21]: KB2536276
[22]: KB2544893
[23]: KB2545698
[24]: KB2547666
[25]: KB2552343
[26]: KB2560656
[27]: KB2562937
[28]: KB2563227
[29]: KB2564958
[30]: KB2570947
[31]: KB2574819
[32]: KB2579686
[33]: KB2585542
[34]: KB2603229
[35]: KB2604115
[36]: KB2619339
[37]: KB2620704
[38]: KB2621440
[39]: KB2631813
[40]: KB2639308
[41]: KB2640148
[42]: KB2647753
[43]: KB2653956
[44]: KB2654428
[45]: KB2656356
[46]: KB2660075
[47]: KB2667402
[48]: KB2676562
[49]: KB2685811
[50]: KB2685813
[51]: KB2685939
[52]: KB2690533
[53]: KB2698365
[54]: KB2705219
[55]: KB2706045
[56]: KB2709630
[57]: KB2712808
[58]: KB2718704
[59]: KB2719857
[60]: KB2726535
[61]: KB2727528
[62]: KB2729094
[63]: KB2729452
[64]: KB2731771
[65]: KB2732059
[66]: KB2732487
[67]: KB2732500
[68]: KB2736422
[69]: KB2742599
[70]: KB2750841
[71]: KB2758857
[72]: KB2761217
[73]: KB2763523
[74]: KB2770660
[75]: KB2773072
[76]: KB2786081
[77]: KB2789645
[78]: KB2791765
[79]: KB2798162
[80]: KB2799926
[81]: KB2800095
[82]: KB2803821
[83]: KB2807986
[84]: KB2808679
[85]: KB2813347
[86]: KB2813430
[87]: KB2820331
[88]: KB2832414
[89]: KB2834140
[90]: KB2836942
[91]: KB2836943
[92]: KB2839894
[93]: KB2840149
[94]: KB2840631
[95]: KB2843630
[96]: KB2846960
[97]: KB2847077
[98]: KB2847311
[99]: KB2847927
[100]: KB2852386
[101]: KB2853952
[102]: KB2855844
[103]: KB2857650
[104]: KB2861191
[105]: KB2861698
[106]: KB2862152
[107]: KB2862330
[108]: KB2862335
[109]: KB2862966
[110]: KB2862973
[111]: KB2864058
[112]: KB2864202
[113]: KB2868038
[114]: KB2868116
[115]: KB2868626
[116]: KB2871997
[117]: KB2872339
[118]: KB2882822
[119]: KB2884256
[120]: KB2887069
[121]: KB2888049
[122]: KB2891804
[123]: KB2892074
[124]: KB2893294
[125]: KB2893519
[126]: KB2894844
[127]: KB2900986
[128]: KB2908783
[129]: KB2911501
[130]: KB2912390
[131]: KB2913152
[132]: KB2918077
[133]: KB2918614
[134]: KB2919469
[135]: KB2922229
[136]: KB2923545
[137]: KB2926765
[138]: KB2928562
[139]: KB2929733
[140]: KB2931356
[141]: KB2937610
[142]: KB2939576
[143]: KB2943357
[144]: KB2952664
[145]: KB2957189
[146]: KB2957503
[147]: KB2957509
[148]: KB2961072
[149]: KB2965788
[150]: KB2966583
[151]: KB2968294
[152]: KB2970228
[153]: KB2971850
[154]: KB2972100
[155]: KB2972211
[156]: KB2972280
[157]: KB2973112
[158]: KB2973201
[159]: KB2973351
[160]: KB2976627
[161]: KB2976897
[162]: KB2977292
[163]: KB2977728
[164]: KB2978092
[165]: KB2978120
[166]: KB2978668
[167]: KB2978742
[168]: KB2979570
[169]: KB2980245
[170]: KB2984972
[171]: KB2984976
[172]: KB2984981
[173]: KB2985461
[174]: KB2991963
[175]: KB2992611
[176]: KB2993651
[177]: KB2993958
[178]: KB2994023
[179]: KB2999226
[180]: KB3001554
[181]: KB3002885
[182]: KB3003057
[183]: KB3003743
[184]: KB3004361
[185]: KB3004375
[186]: KB3005607
[187]: KB3006121
[188]: KB3006226
[189]: KB3006625
[190]: KB3008627
[191]: KB3008923
[192]: KB3009736
[193]: KB3010788
[194]: KB3011780
[195]: KB3012176
[196]: KB3013126
[197]: KB3013410
[198]: KB3014406
[199]: KB3019215
[200]: KB3020369
[201]: KB3020388
[202]: KB3021674
[203]: KB3022777
[204]: KB3023215
[205]: KB3025390
[206]: KB3030377
[207]: KB3031432
[208]: KB3032655
[209]: KB3033889
[210]: KB3033890
[211]: KB3033929
[212]: KB3035126
[213]: KB3035132
[214]: KB3037574
[215]: KB3042058
[216]: KB3042553
[217]: KB3045685
[218]: KB3046017
[219]: KB3046269
[220]: KB3055642
[221]: KB3059317
[222]: KB3060716
[223]: KB3061518
[224]: KB3067903
[225]: KB3069114
[226]: KB3069392
[227]: KB3069762
[228]: KB3071756
[229]: KB3072305
[230]: KB3072630
[231]: KB3072633
[232]: KB3074543
[233]: KB3075226
[234]: KB3076895
[235]: KB3076949
[236]: KB3077715
[237]: KB3078601
[238]: KB3080446
[239]: KB3081320
[240]: KB3083710
[241]: KB3084135
[242]: KB3086255
[243]: KB3087039
[244]: KB3087918
[245]: KB3088195
[246]
Network Card(s): 5 NIC(s) Installed.
[01]: Bluetooth Device (Personal Area Network)
Connection Name: Bluetooth Network Connection
Status: Media disconnected
[02]: Intel(R) 82579LM Gigabit Network Connection
Connection Name: Local Area Connection
Status: Media disconnected
[03]: Intel(R) Centrino(R) Advanced-N 6205
Connection Name: Wireless Network Connection
DHCP Enabled: Yes
DHCP Server: 192.168.2.1
IP address(es)
[01]: 192.168.2.2
[04]: Microsoft Virtual WiFi Miniport Adapter
Connection Name: Wireless Network Connection 2
Status: Hardware not present
[05]: Microsoft Virtual WiFi Miniport Adapter
Connection Name: Wireless Network Connection 3
Status: Hardware not present

65
tests/test_systeminfo.py Normal file
View File

@ -0,0 +1,65 @@
import json
import os
import unittest
import jc.parsers.systeminfo
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
class MyTests(unittest.TestCase):
def setUp(self):
# input
with open(
os.path.join(
THIS_DIR, os.pardir, "tests/fixtures/windows/windows-10/systeminfo.out"
),
"r",
encoding="utf-8",
) as f:
self.windows_10_systeminfo = f.read()
with open(
os.path.join(
THIS_DIR, os.pardir, "tests/fixtures/windows/windows-7/systeminfo.out"
),
"r",
encoding="utf-8",
) as f:
self.windows_7_systeminfo = f.read()
# output
with open(
os.path.join(
THIS_DIR, os.pardir, "tests/fixtures/windows/windows-10/systeminfo.json"
),
"r",
encoding="utf-8",
) as f:
self.windows_10_systeminfo_json = json.loads(f.read())
with open(
os.path.join(
THIS_DIR, os.pardir, "tests/fixtures/windows/windows-7/systeminfo.json"
),
"r",
encoding="utf-8",
) as f:
self.windows_7_systeminfo_json = json.loads(f.read())
def test_windows_systeminfo(self):
"""
Test a sample Windows "systeminfo" command output
"""
self.assertEqual(
jc.parsers.systeminfo.parse(self.windows_10_systeminfo, quiet=True),
self.windows_10_systeminfo_json,
)
self.assertEqual(
jc.parsers.systeminfo.parse(self.windows_7_systeminfo, quiet=True),
self.windows_7_systeminfo_json,
)
if __name__ == "__main__":
unittest.main()