Reputation: 3
I am currently lost in my quest to script something that parses a firewall configuration and outputs a html list.
For some reason that I just can't figure out myself why the <br />.join
part does not work.
Another part of my script creates the below mentioned dictionaries.
The part of the script displayed here checks, which value is assigned to a key from the dict "ipsec_encr_int". Said value gets assigned to key_interface as a string.
The script then checks if this key_interface exists als a key in phase1_int_ike and whether it has one or two values assigned.
After there are only ikev1 and ikev2. Depending on which one (or both) the key/Value pairing should be bisplayed, but not as a LIST! It should be displayed as several strings with <br />
between them, since the end result should be a html-table.
I tried running the ".join()" part outside of the function, but got the same result.
Data set:
phase1_int_ike = {'OUTSIDE': ['ikev2', 'ikev1'], 'P2P-Duckburg': ['ikev2'], 'P2P-Darkwing': ['ikev2']}
ipsec_encr_int = {'VPN-TO-Gearloose': ['OUTSIDE'], 'VPN-TO-Ducktales': ['OUTSIDE'], 'VPN-TO-BeagleBoys': ['OUTSIDE'], 'VPN-TO-Duckburg': ['P2P-Duckburg'], 'VPN-TO-Darkwing': ['P2P-Darkwing']}
ipsec = {'VPN-TO-Gearloose': ['OUTSIDE_MAP', 'Map Number: 10', 'PFS: Default DH Group', 'Peer: 123.123.123.126', 'ikev1', 'Phase 2: ESP-AES256-SHA1'], 'VPN-TO-Ducktales': ['OUTSIDE_MAP', 'Map Number: 20', 'PFS: DH group19', 'Peer: 123.123.123.13', 'ikev2', 'Phase 2: IKEV2-AES256-SHA256'], 'VPN-TO-BeagleBoys': ['OUTSIDE_MAP', 'Map Number: 30', 'PFS: DH group5', 'Peer: 123.123.123.250', 'ikev1', 'Phase 2: ESP-AES256-SHA1', 'lifetime 3600 seconds', 'lifetime 4608000 kilobytes'], 'VPN-TO-Duckburg': ['P2P-Duckburg', 'Map Number: 10', 'PFS: DH group19', 'Peer: 123.123.123.27', 'ikev2', 'Phase 2: IKEV2-AES256-SHA256'], 'VPN-TO-Darkwing': ['P2P-Darkwing', 'Map Number: 10', 'PFS: DH group19', 'Peer: 123.123.123.17', 'ikev2', 'Phase 2: IKEV2-AES256-SHA256']}
ikev2_pols = {'ikev2 policy 10': ['encryption: aes-256', 'integrity sha256', 'group 19', 'prf sha256', 'lifetime 86400']}
ikev1_pols = {'ikev1 policy 10': ['authentication: pre-share', 'encryption: aes-256', 'hash: sha', 'group 2', 'lifetime: 86400'], 'ikev1 policy 20': ['authentication: pre-share', 'encryption: aes-256', 'hash: sha', 'group 5', 'lifetime: 28800']}
crypto_int = {'OUTSIDE_MAP': ['OUTSIDE'], 'P2P-Duckburg': ['P2P-Duckburg'], 'P2P-Darkwing': ['P2P-Darkwing']}
Function currently:
def html_ipsec_tbl(key):
if key in ipsec_encr_int:
key_interface = ' '.join(ipsec_encr_int[key])
if key_interface in phase1_int_ike and len(phase1_int_ike[key_interface]) == 2:
for ikev1_key in ikev1_pols:
return '<td>' + '<th>' + ikev1_key + '</th>' + '<br />'.join([str(x) for x in ikev1_pols.values()]) + '</td>'
#print('<th>' + ikev1_key + '</th>')
#print(*ikev1_pol, sep = "\n")
for ikev2_key in ikev2_pols:
return '<td>' + '<th>' + ikev2_key + '</th>' + '<br />'.join([str(x) for x in ikev2_pols.values()]) + '</td>'
#print('<th>' + ikev2_key + '</th>')
#print(*ikev2_pol, sep = "\n")
elif key_interface in phase1_int_ike and len(phase1_int_ike[key_interface]) == 1:
if 'ikev1' in phase1_int_ike[key_interface]:
for ikev1_key in ikev1_pols:
return '<td>' + '<th>' + ikev1_key + '</th>' + '<br />'.join([str(x) for x in ikev1_pols.values()]) + '</td>'
#print('<th>' + ikev1_key + '</th>')
#print(*ikev1_pol, sep = "\n")
elif 'ikev2' in phase1_int_ike[key_interface]:
for ikev2_key in ikev2_pols:
return '<td>' + '<th>' + ikev2_key + '</th>' + '<br />'.join([str(x) for x in ikev2_pols.values()]) + '</td>'
#print('<th>' + ikev2_key + '</th>')
#print(*ikev2_pol, sep = "\n")
for value in ipsec[key]:
if 'Peer:' in value.split(' '):
peer = value.split(' ')[1]
print(html_ipsec_tbl('VPN-TO-Duckburg'))
Output currently:
<td><th>ikev2 policy 10</th>['encryption: aes-256', 'integrity sha256', 'group 19', 'prf sha256', 'lifetime 86400']</td>
My Goal:
<td><th>ikev2 policy 10</th> <br />encryption: aes-256<br />integrity sha256<br />group 19<br />prf sha256<br />lifetime 86400</td>
Now there is no question that it's possible to code this WAY cleaner. But I'm a total beginner and as long as the .join part works too, I'm fine with my code as it is.
Upvotes: 0
Views: 51
Reputation: 1719
The values in your dictionary are lists with strings in them. So dict.values()
returns a list of lists. When you use .join
, it is converting that list into a str
instead of the elements of that list:
In [38]: [str(x) for x in ikev2_pols.values()]
Out[38]: ["['encryption: aes-256', 'integrity sha256', 'group 19', 'prf sha256', 'lifetime 86400']"]
Instead, you need to flatten out that list:
In [39]: [str(elem) for sublist in ikev2_pols.values() for elem in sublist]
Out[39]:
['encryption: aes-256',
'integrity sha256',
'group 19',
'prf sha256',
'lifetime 86400']
Add that code inside your .join()
instead.
Upvotes: 1