-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_c.py
150 lines (106 loc) · 3.04 KB
/
lab_c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from bs4 import BeautifulSoup
import webbrowser
from ciscoconfparse import CiscoConfParse
from ciscoconfparse.ccp_util import IPv4Obj
def parseIP(conf):
parse = CiscoConfParse(conf, factory=True)
ip_add = []
interfaces = parse.find_objects(r"^interf")
for e in interfaces:
IPv4_REGEX = r"ip\saddress\s(\S+\s+\S+)"
for cmd in e.re_search_children(IPv4_REGEX):
ipv4_addr = e.re_match_iter_typed(IPv4_REGEX, result_type=IPv4Obj)
ip_add.append(ipv4_addr.ip.exploded)
return(ip_add)
def parseIntf(conf):
parse = CiscoConfParse(conf)
intf_list = []
interfaces = parse.find_objects(r"^interface ")
for interface in interfaces:
intf_list.append(interface.text)
if interface in parse.find_objects_w_child('^interface', '^\s+no ip address'):
intf_list.remove(interface.text)
return(intf_list)
def parseConf(conf):
f = open(conf,'r')
for line in f:
if "Last configuration" in line:
last_change = line
conf_change = last_change[31:58]
return(conf_change)
def parseHost(conf):
host = []
parse = CiscoConfParse(conf)
hostnames = parse.find_objects(r"^hostname")
for hostname in hostnames:
hostname = hostname.replace(r'hostname\s', r'')
host.append(hostname)
return(host)
def parseVersion(version):
f = open(version,'r')
for line in f:
if "uptime" in line:
uptime = line
if "ROM:" in line:
rom = line
rom_ver = rom[23:]
uptime = uptime[15:]
return rom_ver,uptime
def generateHtml(conf, ver):
html_template = '''
<html>
<head>
<title>Infotabell</title>
<style>
table, th, td {border: solid 1px black;}
th, td {padding: 5px;}
table {border-collapse: collapse;}
</style>
</head>
<body>
<h1>Routerinformation</h1>
<table>
<tr>
<td>Namn & Last Change</td><td id="namn">xxx</td><td>LastChange</td>
</tr>
<tr>
<td>Interface & IP-nummer</td><td id="interface">interface</td> <td id ="ip">allaipnummer</td>
</tr>
</table>
<table>
<tr>
<td>Rom version:</td><td id="ROM">ROM_VER</td></tr>
<br>
<tr>
<td>Uptime:</td><td id="Uptime">UPTIME</td>
</tr>
</body>
</html>
'''
soup = BeautifulSoup(html_template, 'html.parser')
#soup.find(id='namn').string = parseHost(conf)
FILNAMN = 'Lab_3_Infotabell.html'
out = open(FILNAMN,'w')
out.write(soup.prettify())
out.close()
host = parseHost(conf)[0]
ips = '<br>'.join(parseIP(conf))
intf = '<br>'.join(parseIntf(conf))
lastChange = parseConf(conf)
rom_ver, uptime = parseVersion(ver)
with open(FILNAMN, 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('allaipnummer',ips)
filedata = filedata.replace('interface', intf)
filedata = filedata.replace('LastChange',lastChange)
filedata = filedata.replace('ROM_VER',rom_ver)
filedata = filedata.replace('UPTIME',uptime)
filedata = filedata.replace('xxx', host)
# Write the file out again
with open(FILNAMN, 'w') as file:
file.write(filedata)
webbrowser.open('file:Users/yaggen/Desktop/python/hv-pyscheme/Lab/'+FILNAMN)
conf = "router_configuration.txt"
version = "router_show_version.txt"
generateHtml(conf,version)