I maybe doing something wrong but using the Netmiko ouput from a Cisco WLC as input for TTP, the final result is not a real dictionary.
Step 1: This is my script:
with ConnectHandler(**device) as net_connect:
wlc_hostname = net_connect.find_prompt()[:-1]
data_to_parse = net_connect.send_command('show run | sec ^wlan.+_([0-9])+_GuestNet')
print (data_to_parse)
ttp_template = """
<template name="GUEST_wlan_profile">
<group name="{{ wlan_profile }}">
wlan {{ wlan_profile }} {{ wlan_id }} {{ ssid }}
security wpa psk set-key ascii {{ psk-encrp-method }} {{ guest-psk | ORPHRASE }}
shutdown {{ state | set ("disabled") }}
</group>
</template>
"""
parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()
cisco_wlanTemplates = parser.result(structure="dictionary")
print(json.dumps(cisco_wlanTemplates, sort_keys=True, indent=4, separators=(',', ': ')))
for cisco_wlan in cisco_wlanTemplates:
print ('WLAN Profile: ',*cisco_wlan)
This is the Netmiko output:
wlan GUEST_local 49 GuestSSID
assisted-roaming prediction
client association limit radio 31
description "Guest with local forwarding"
no security ft adaptive
security wpa psk set-key ascii 8 TaIQFQeIOPX^ZCIEbbggNLMiFNGSAYVBecRF
no security wpa akm dot1x
security wpa akm psk
no shutdown
This is the parser output:
{
"GUEST_wlan_profile": [
{
"GUEST_local": {
"guest-psk": "TaIQFQeIOPX^ZCIEbbggNLMiFNGSAYVBecRF",
"psk-encrp-method": "8",
"ssid": "GuestNet",
"wlan_id": "49"
}
}
]
}
This is what I get from printing the dictionary items:
WLAN Profile: G U E S T _ w l a n _ p r o f i l e
And this is what I get if I try to get first element from supposed nested dictionary: Code:
for cisco_wlan in cisco_wlanTemplates:
print ('WLAN Profile: ',cisco_wlan[0])
Output:
WLAN Profile: G
Step 2: Now using this output I got a different result for print:
Code:
ttp_template = """
<template name="GUEST_wlan_profile">
<group name="{{ wlan_profile }}">
wlan {{ wlan_profile }} {{ wlan_id }} {{ ssid }}
<group>
security wpa psk set-key ascii {{ psk-encrp-method }} {{ guest-psk | ORPHRASE }}
shutdown {{ state | set ("disabled") }}
</group>
</group>
</template>
"""
# create parser object and parse data using template:
parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()
cisco_wlanTemplates = parser.result(format='json')[0]
print(cisco_wlanTemplates)
Output:
[
{
"GUEST_local": [
{
"ssid": "GuestNet",
"wlan_id": "49"
},
{
"guest-psk": "TaIQFQeIOPX^ZCIEbbggNLMiFNGSAYVBecRF",
"psk-encrp-method": "8"
}
]
}
]
For some reason it seems to me that TTP's ouput creates an initial dictionary with a list of dicts where I don't know how get access to keys and values.
What I want to accomplish with TTP is to access the "guest-psk" to modify it, and in a future modify additional features from the originalNetmiko output, so I need nested dictionaries to cover all diferent "WLAN profiles" setup in the Cisco device. I don't want to use another parser this time nor a regex to accomplish that.
I finally have found where the problem was, and it was the structure of the parser's output should be "flat_list":