Skip to content

Commit 48bc0bd

Browse files
authored
cisco ipsec ttp demo
1 parent d733b87 commit 48bc0bd

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<vars>
2+
acl = {
3+
"permit": [],
4+
"deny": [],
5+
}
6+
</vars>
7+
8+
<g name="interfaces.{{ vrf }}.{{ ip }}">
9+
interface BDI{{ id }}
10+
vrf forwarding {{ vrf }}
11+
ip address {{ ip }} {{ ignore }}
12+
</g>
13+
14+
<g name="ipsec.{{ map_name }}**">
15+
crypto map {{ ignore }} local-address {{ ignore }}
16+
<g name="{{ id }}**">
17+
crypto map {{ map_name }} {{ id }} ipsec-isakmp
18+
! Incomplete {{ is_complete | set(0) }}
19+
description {{ description | ORPHRASE }}
20+
set peer {{ peer_ip }}
21+
set security-association lifetime seconds {{ lifetime }}
22+
set transform-set {{ tf_set }}
23+
set isakmp-profile {{ isa_profile }}
24+
match address {{ acl }}
25+
</g>
26+
</g>
27+
28+
<g name="isakmp.keyring.{{ name }}">
29+
crypto keyring {{ name }}
30+
pre-shared-key address {{ remote_ip }} key {{ key }}
31+
</g>
32+
33+
<g name="isakmp.profile.{{ name }}">
34+
crypto isakmp profile {{ name }}
35+
vrf {{ vrf }}
36+
keyring {{ keyring }}
37+
match identity address {{ identity_addr }} {{ ignore }}
38+
</g>
39+
40+
<g name="acl.{{ name }}" default="acl">
41+
ip access-list extended {{ name }}
42+
permit {{ permit | ORPHRASE | joinmatches | split('\n') }}
43+
deny {{ deny | ORPHRASE | joinmatches | split('\n') }}
44+
</g>
45+
46+
<g name="route.{{ vrf }}">
47+
<g name="global">
48+
ip route vrf {{ vrf }} {{ dest }} {{ mask }} {{ next_hop }} global
49+
</g>
50+
</g>

demo/ttp_demo.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from ttp import ttp
2+
import json
3+
4+
def parse_running_config(data, template):
5+
parser = ttp(data=data, template=template)
6+
parser.parse()
7+
result = parser.result()[0][0]
8+
return result
9+
10+
def parse_vpn_config(vrf, config, map_name="cryto_map_common"):
11+
try:
12+
iface_id = config["interfaces"][vrf]["66.66.66.66"]["id"]
13+
except:
14+
iface_id = config["interfaces"][vrf]["66.66.66.65"]["id"]
15+
result = config["ipsec"][map_name][iface_id]
16+
result["acl"] = config["acl"]["acc_acl_" + vrf]
17+
result["isa_profile"] = config["isakmp"]["profile"]["isa_profile_" + vrf]
18+
result["isa_profile"]["keyring"] = config["isakmp"]["keyring"]["keyring_" + vrf]
19+
result["route"] = config["route"].get(vrf, [])
20+
return result
21+
22+
config = parse_running_config("./test.log", "./1.txt")
23+
with open("result.json", 'w') as f:
24+
f.write(json.dumps(config))
25+
26+
# for i in config["ipsec"]["cryto_map_common"].values():
27+
# print(i.get("isa_profile"),i.get("is_complete","1"))
28+
29+
30+
result = parse_vpn_config("9e3rszehv0qlxijzv03bd5rhz", config)
31+
from pprint import pprint
32+
pprint(result)
33+
34+
# import csv
35+
36+
37+
# def get_device_list(filename):
38+
# """csv to dict"""
39+
# with open(filename) as f:
40+
# reader = csv.DictReader(f)
41+
# return list(reader)
42+
43+
# from netmiko import ConnectHandler as ch
44+
45+
# CISCO_CMD = "show run"
46+
# H3C_CMD = "dis curr"
47+
48+

0 commit comments

Comments
 (0)