mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-21 00:19:42 +02:00
iwconfig: add to parser list + move test fixture to generic folder
This commit is contained in:
@ -65,6 +65,7 @@ parsers: List[str] = [
|
|||||||
'iptables',
|
'iptables',
|
||||||
'iso-datetime',
|
'iso-datetime',
|
||||||
'iw-scan',
|
'iw-scan',
|
||||||
|
'iwconfig',
|
||||||
'jar-manifest',
|
'jar-manifest',
|
||||||
'jobs',
|
'jobs',
|
||||||
'jwt',
|
'jwt',
|
||||||
|
@ -18,6 +18,32 @@ Usage (module):
|
|||||||
Schema:
|
Schema:
|
||||||
|
|
||||||
[
|
[
|
||||||
|
{
|
||||||
|
"name": string,
|
||||||
|
"protocol": string,
|
||||||
|
"essid": string,
|
||||||
|
"mode": string,
|
||||||
|
"frequency": float,
|
||||||
|
"frequency_unit": string,
|
||||||
|
"access_point": string,
|
||||||
|
"bit_rate": float,
|
||||||
|
"bit_rate_unit": string,
|
||||||
|
"tx_power": integer,
|
||||||
|
"tx_power_unit": string,
|
||||||
|
"retry_short_limit": integer,
|
||||||
|
"rts_threshold": boolean,
|
||||||
|
"fragment_threshold": boolean,
|
||||||
|
"power_management": boolean,
|
||||||
|
"link_quality": string,
|
||||||
|
"signal_level": integer,
|
||||||
|
"signal_level_unit": string,
|
||||||
|
"rx_invalid_nwid": integer,
|
||||||
|
"rx_invalid_crypt": integer,
|
||||||
|
"rx_invalid_frag": integer,
|
||||||
|
"tx_excessive_retries": integer,
|
||||||
|
"invalid_misc": integer,
|
||||||
|
"missed_beacon": integer
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -90,36 +116,7 @@ def parse(
|
|||||||
jc.utils.input_type_check(data)
|
jc.utils.input_type_check(data)
|
||||||
|
|
||||||
raw_output: List[Dict] = []
|
raw_output: List[Dict] = []
|
||||||
|
wireless_extension_obj: Dict = {}
|
||||||
# for backwards compatibility, preset all fields to None
|
|
||||||
wireless_extension_obj: Dict = {
|
|
||||||
"name": None,
|
|
||||||
"protocol": None,
|
|
||||||
"essid": None,
|
|
||||||
"mode": None,
|
|
||||||
"frequency": None,
|
|
||||||
"frequency_unit": None,
|
|
||||||
"access_point": None,
|
|
||||||
"bit_rate": None,
|
|
||||||
"bit_rate_unit": None,
|
|
||||||
"tx_power": None,
|
|
||||||
"tx_power_unit": None,
|
|
||||||
"retry_short_limit": None,
|
|
||||||
"rts_threshold": None,
|
|
||||||
"fragment_threshold": None,
|
|
||||||
"power_management": None,
|
|
||||||
"link_quality": None,
|
|
||||||
"signal_level": None,
|
|
||||||
"signal_level_unit": None,
|
|
||||||
"rx_invalid_nwid": None,
|
|
||||||
"rx_invalid_crypt": None,
|
|
||||||
"rx_invalid_frag": None,
|
|
||||||
"tx_excessive_retries": None,
|
|
||||||
"invalid_misc": None,
|
|
||||||
"missed_beacon": None
|
|
||||||
}
|
|
||||||
|
|
||||||
interface_item: Dict = wireless_extension_obj.copy()
|
|
||||||
|
|
||||||
re_interface = re.compile(r'^(?P<name>[a-zA-Z0-9:._-]+)\s+(?P<protocol>([a-zA-Z0-9]+\s)*[a-zA-Z0-9.]+)\s+ESSID:\"(?P<essid>[a-zA-Z0-9:._\s]+)\"')
|
re_interface = re.compile(r'^(?P<name>[a-zA-Z0-9:._-]+)\s+(?P<protocol>([a-zA-Z0-9]+\s)*[a-zA-Z0-9.]+)\s+ESSID:\"(?P<essid>[a-zA-Z0-9:._\s]+)\"')
|
||||||
re_mode = re.compile(r'Mode:(?P<mode>\w+)')
|
re_mode = re.compile(r'Mode:(?P<mode>\w+)')
|
||||||
@ -147,21 +144,22 @@ def parse(
|
|||||||
re_rx_invalid_frag, re_tx_excessive_retries, re_invalid_misc, re_missed_beacon
|
re_rx_invalid_frag, re_tx_excessive_retries, re_invalid_misc, re_missed_beacon
|
||||||
]
|
]
|
||||||
|
|
||||||
|
interface_item = None
|
||||||
if jc.utils.has_data(data):
|
if jc.utils.has_data(data):
|
||||||
for line in filter(None, data.splitlines()):
|
for line in filter(None, data.splitlines()):
|
||||||
|
|
||||||
# Find new interface lines
|
# Find new interface lines
|
||||||
interface_match = re.search(re_interface, line)
|
interface_match = re.search(re_interface, line)
|
||||||
if interface_match:
|
if interface_match:
|
||||||
if interface_item['name'] is not None:
|
if interface_item is not None:
|
||||||
raw_output.append(interface_item)
|
raw_output.append(interface_item)
|
||||||
interface_item = wireless_extension_obj.copy()
|
|
||||||
|
|
||||||
|
interface_item = dict()
|
||||||
interface_item.update(interface_match.groupdict())
|
interface_item.update(interface_match.groupdict())
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# we do not have any interface yet continue to search for it --> next line
|
# we do not have any interface yet continue to search for it --> next line
|
||||||
if interface_item['name'] is None:
|
if interface_item is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Filling interface with whatever we can find
|
# Filling interface with whatever we can find
|
||||||
@ -170,7 +168,7 @@ def parse(
|
|||||||
if match:
|
if match:
|
||||||
interface_item.update(match.groupdict())
|
interface_item.update(match.groupdict())
|
||||||
|
|
||||||
if interface_item['name'] is not None:
|
if interface_item is not None:
|
||||||
raw_output.append(interface_item)
|
raw_output.append(interface_item)
|
||||||
|
|
||||||
return raw_output if raw else _process(raw_output)
|
return raw_output if raw else _process(raw_output)
|
||||||
|
@ -9,15 +9,15 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|||||||
class iwconfigTests(unittest.TestCase):
|
class iwconfigTests(unittest.TestCase):
|
||||||
|
|
||||||
# input
|
# input
|
||||||
with open(os.path.join(THIS_DIR, 'fixtures/ubuntu-20.10/iwconfig.out'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, 'fixtures/generic/iwconfig.out'), 'r', encoding='utf-8') as f:
|
||||||
ubuntu_20_10_iwconfig= f.read()
|
iwconfig_output = f.read()
|
||||||
|
|
||||||
# output
|
# output
|
||||||
with open(os.path.join(THIS_DIR, 'fixtures/ubuntu-20.10/iwconfig.json'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, 'fixtures/generic/iwconfig.json'), 'r', encoding='utf-8') as f:
|
||||||
ubuntu_20_10_iwconfig_json= json.loads(f.read())
|
iwconfig_json = json.loads(f.read())
|
||||||
|
|
||||||
with open(os.path.join(THIS_DIR, 'fixtures/ubuntu-20.10/iwconfig-raw.json'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, 'fixtures/generic/iwconfig-raw.json'), 'r', encoding='utf-8') as f:
|
||||||
ubuntu_20_10_iwconfig_raw_json= json.loads(f.read())
|
iwconfig_raw_json = json.loads(f.read())
|
||||||
|
|
||||||
|
|
||||||
def test_iwconfig_nodata(self):
|
def test_iwconfig_nodata(self):
|
||||||
@ -26,17 +26,17 @@ class iwconfigTests(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
self.assertEqual(jc.parsers.iwconfig.parse('', quiet=True), [])
|
self.assertEqual(jc.parsers.iwconfig.parse('', quiet=True), [])
|
||||||
|
|
||||||
def test_iwconfig_ubuntu_20_04(self):
|
def test_iwconfig_raw(self):
|
||||||
"""
|
"""
|
||||||
Test 'iwconfig' raw on Ubuntu 20.10
|
Test 'iwconfig' raw
|
||||||
"""
|
"""
|
||||||
self.assertEqual(jc.parsers.iwconfig.parse(self.ubuntu_20_10_iwconfig, quiet=True, raw=True), self.ubuntu_20_10_iwconfig_raw_json)
|
self.assertEqual(jc.parsers.iwconfig.parse(self.iwconfig_output, quiet=True, raw=True), self.iwconfig_raw_json)
|
||||||
|
|
||||||
def test_iwconfig_ubuntu_20_04(self):
|
def test_iwconfig(self):
|
||||||
"""
|
"""
|
||||||
Test 'iwconfig' on Ubuntu 20.10
|
Test 'iwconfig'
|
||||||
"""
|
"""
|
||||||
self.assertEqual(jc.parsers.iwconfig.parse(self.ubuntu_20_10_iwconfig, quiet=True), self.ubuntu_20_10_iwconfig_json)
|
self.assertEqual(jc.parsers.iwconfig.parse(self.iwconfig_output, quiet=True), self.iwconfig_json)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Reference in New Issue
Block a user