Skip to content

Commit

Permalink
Initial commit with all code.
Browse files Browse the repository at this point in the history
Signed-off-by: Maxwell Schmitt <[email protected]>
  • Loading branch information
maxwschm committed Oct 24, 2018
0 parents commit 0cbcdda
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#DEVTPLBUILD
#Maxwell Schmitt ([email protected])
## Provided as-is -- allows you to build an xml file to import multiple devices in NSO

### Using:
Edit the python file's infile and outfile variable to what you'd prefer

The infile's structure should look like:
r1,127.0.0.1,10025,default,cisco-ios-xr
r2,127.0.0.1,10026,default,cisco-ios-xr
r3,127.0.0.1,10027,default,cisco-ios-xr
r4,127.0.0.1,10028,default,cisco-ios-xr
r5,127.0.0.1,10029,default,cisco-ios-xr
r6,127.0.0.1,10030,default,cisco-ios-xr

name (displayed in nso), device address, ssh port, authgroup (should already be setup in NSO), ned id.

Then call like:
python3 devtplbuild.py
and import into NSO like:
ncs_load -lm outfile


Sample infile and outfile are included in the repo for you to analyze.
105 changes: 105 additions & 0 deletions devtplbuild.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'''
DEVTPLBUILD
Maxwell Schmitt
([email protected])
**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)
6 changes: 6 additions & 0 deletions samplein.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
r1,127.0.0.1,10025,default,cisco-ios-xr
r2,127.0.0.1,10026,default,cisco-ios-xr
r3,127.0.0.1,10027,default,cisco-ios-xr
r4,127.0.0.1,10028,default,cisco-ios-xr
r5,127.0.0.1,10029,default,cisco-ios-xr
r6,127.0.0.1,10030,default,cisco-ios-xr
242 changes: 242 additions & 0 deletions sampleout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
<devices xmlns="http://tail-f.com/ns/ncs">

<device>

<name>r1</name>

<address>127.0.0.1</address>

<port>10025</port>

<ssh>

<host-key>

<algorithm>ssh-rsa</algorithm>

<key-data>AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf</key-data>

</host-key>

</ssh>

<state>

<admin-state>unlocked</admin-state>

</state>

<authgroup>default</authgroup>

<device-type>

<cli>

<ned-id xmlns:cisco-ios-xr-id="http://tail-f.com/ned/cisco-ios-xr-id">cisco-ios-xr-id:cisco-ios-xr</ned-id>

</cli>

</device-type>

</device>

<device>

<name>r2</name>

<address>127.0.0.1</address>

<port>10026</port>

<ssh>

<host-key>

<algorithm>ssh-rsa</algorithm>

<key-data>AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf</key-data>

</host-key>

</ssh>

<state>

<admin-state>unlocked</admin-state>

</state>

<authgroup>default</authgroup>

<device-type>

<cli>

<ned-id xmlns:cisco-ios-xr-id="http://tail-f.com/ned/cisco-ios-xr-id">cisco-ios-xr-id:cisco-ios-xr</ned-id>

</cli>

</device-type>

</device>

<device>

<name>r3</name>

<address>127.0.0.1</address>

<port>10027</port>

<ssh>

<host-key>

<algorithm>ssh-rsa</algorithm>

<key-data>AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf</key-data>

</host-key>

</ssh>

<state>

<admin-state>unlocked</admin-state>

</state>

<authgroup>default</authgroup>

<device-type>

<cli>

<ned-id xmlns:cisco-ios-xr-id="http://tail-f.com/ned/cisco-ios-xr-id">cisco-ios-xr-id:cisco-ios-xr</ned-id>

</cli>

</device-type>

</device>

<device>

<name>r4</name>

<address>127.0.0.1</address>

<port>10028</port>

<ssh>

<host-key>

<algorithm>ssh-rsa</algorithm>

<key-data>AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf</key-data>

</host-key>

</ssh>

<state>

<admin-state>unlocked</admin-state>

</state>

<authgroup>default</authgroup>

<device-type>

<cli>

<ned-id xmlns:cisco-ios-xr-id="http://tail-f.com/ned/cisco-ios-xr-id">cisco-ios-xr-id:cisco-ios-xr</ned-id>

</cli>

</device-type>

</device>

<device>

<name>r5</name>

<address>127.0.0.1</address>

<port>10029</port>

<ssh>

<host-key>

<algorithm>ssh-rsa</algorithm>

<key-data>AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf</key-data>

</host-key>

</ssh>

<state>

<admin-state>unlocked</admin-state>

</state>

<authgroup>default</authgroup>

<device-type>

<cli>

<ned-id xmlns:cisco-ios-xr-id="http://tail-f.com/ned/cisco-ios-xr-id">cisco-ios-xr-id:cisco-ios-xr</ned-id>

</cli>

</device-type>

</device>

<device>

<name>r6</name>

<address>127.0.0.1</address>

<port>10030</port>

<ssh>

<host-key>

<algorithm>ssh-rsa</algorithm>

<key-data>AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf</key-data>

</host-key>

</ssh>

<state>

<admin-state>unlocked</admin-state>

</state>

<authgroup>default</authgroup>

<device-type>

<cli>

<ned-id xmlns:cisco-ios-xr-id="http://tail-f.com/ned/cisco-ios-xr-id">cisco-ios-xr-id:cisco-ios-xr</ned-id>

</cli>

</device-type>

</device>
</devices>

0 comments on commit 0cbcdda

Please sign in to comment.