-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevtplbuild.py
105 lines (75 loc) · 2.45 KB
/
devtplbuild.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
'''
DEVTPLBUILD
Maxwell Schmitt
**THIS SCRIPT IS PROVIDED AS-IS**
'''
import subprocess
#Get the Fingerprint
def sshscan(addr, port):
keydata = subprocess.getoutput(["ssh-keyscan -p " + port + " " + addr])
keydata = keydata.split("ssh-rsa ")[1]
return keydata
#Generate the individual device template
def tplgen(tpl, name, addr, port, keydata, authgroup, nedn):
tpl = tpl.replace("\{name\}", name)
tpl = tpl.replace("\{addr\}", addr)
tpl = tpl.replace("\{port\}", port)
tpl = tpl.replace("\{keydata\}", keydata)
tpl = tpl.replace("\{authgroup\}", authgroup)
tpl = tpl.replace("\{nedn\}", nedn.strip())
return tpl
#Generate and concatenate the device templates
def grandtplgen(tpl, dlist):
grandtpl = ''''''
for entry in dlist:
grandtpl += tplgen(tpl, entry["name"], entry["addr"], entry["port"], entry["keydata"], entry["authgroup"], entry["nedn"])
return grandtpl
#Read in from file and output a list of dictionaries with all info needed to generate templates
def getdlist(ftr):
infile = open(ftr, 'r')
dlist = []
for line in infile:
values = line.split(',')
idlist = {}
idlist["name"] = values[0]
idlist["addr"] = values[1]
idlist["port"] = values[2]
idlist["keydata"] = sshscan(values[1], values[2])
idlist["authgroup"] = values[3]
idlist["nedn"] = values[4]
dlist.append(idlist)
infile.close()
return dlist
#MAIN CODE BELOW
enclosest = '<devices xmlns="http://tail-f.com/ns/ncs">\n'
tpl = '''
<device>
<name>\{name\}</name>
<address>\{addr\}</address>
<port>\{port\}</port>
<ssh>
<host-key>
<algorithm>ssh-rsa</algorithm>
<key-data>\{keydata\}</key-data>
</host-key>
</ssh>
<state>
<admin-state>unlocked</admin-state>
</state>
<authgroup>\{authgroup\}</authgroup>
<device-type>
<cli>
<ned-id xmlns:\{nedn\}-id="http://tail-f.com/ned/\{nedn\}-id">\{nedn\}-id:\{nedn\}</ned-id>
</cli>
</device-type>
</device>\n'''
encloseend = "</devices>"
infile = "in.txt" #modify the string to change the infile destination
outfile = "out.xml" #modify the string to change the outfile destination
grandtpl = grandtplgen(tpl, getdlist(infile))
grandout = enclosest + grandtpl + encloseend
xmlout = open(outfile, 'w')
xmlout.write(grandout)
xmlout.close()
print("Done!\nJust type ncs_load -lm " + outfile)