From 0cbcddae5d51e1f6979ec700e9e102df74c5ef89 Mon Sep 17 00:00:00 2001 From: Maxwell Schmitt Date: Wed, 24 Oct 2018 02:40:42 -0400 Subject: [PATCH] Initial commit with all code. Signed-off-by: Maxwell Schmitt --- README.md | 24 +++++ devtplbuild.py | 105 +++++++++++++++++++++ samplein.txt | 6 ++ sampleout.xml | 242 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 377 insertions(+) create mode 100644 README.md create mode 100644 devtplbuild.py create mode 100644 samplein.txt create mode 100644 sampleout.xml diff --git a/README.md b/README.md new file mode 100644 index 0000000..db108b0 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +#DEVTPLBUILD +#Maxwell Schmitt (maxwschm@cisco.com) +## 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. \ No newline at end of file diff --git a/devtplbuild.py b/devtplbuild.py new file mode 100644 index 0000000..c980d61 --- /dev/null +++ b/devtplbuild.py @@ -0,0 +1,105 @@ +''' +DEVTPLBUILD +Maxwell Schmitt +(maxwschm@cisco.com) + +**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 = '\n' +tpl = ''' + + + \{name\} + +
\{addr\}
+ + \{port\} + + + + + + ssh-rsa + + \{keydata\} + + + + + + + + unlocked + + + + \{authgroup\} + + + + + + \{nedn\}-id:\{nedn\} + + + + + +
\n''' +encloseend = "
" + +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) diff --git a/samplein.txt b/samplein.txt new file mode 100644 index 0000000..adcff5c --- /dev/null +++ b/samplein.txt @@ -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 diff --git a/sampleout.xml b/sampleout.xml new file mode 100644 index 0000000..c80f0d7 --- /dev/null +++ b/sampleout.xml @@ -0,0 +1,242 @@ + + + + + r1 + +
127.0.0.1
+ + 10025 + + + + + + ssh-rsa + + AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf + + + + + + + + unlocked + + + + default + + + + + + cisco-ios-xr-id:cisco-ios-xr + + + + + +
+ + + + r2 + +
127.0.0.1
+ + 10026 + + + + + + ssh-rsa + + AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf + + + + + + + + unlocked + + + + default + + + + + + cisco-ios-xr-id:cisco-ios-xr + + + + + +
+ + + + r3 + +
127.0.0.1
+ + 10027 + + + + + + ssh-rsa + + AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf + + + + + + + + unlocked + + + + default + + + + + + cisco-ios-xr-id:cisco-ios-xr + + + + + +
+ + + + r4 + +
127.0.0.1
+ + 10028 + + + + + + ssh-rsa + + AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf + + + + + + + + unlocked + + + + default + + + + + + cisco-ios-xr-id:cisco-ios-xr + + + + + +
+ + + + r5 + +
127.0.0.1
+ + 10029 + + + + + + ssh-rsa + + AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf + + + + + + + + unlocked + + + + default + + + + + + cisco-ios-xr-id:cisco-ios-xr + + + + + +
+ + + + r6 + +
127.0.0.1
+ + 10030 + + + + + + ssh-rsa + + AAAAB3NzaC1yc2EAAAADAQABAAABAQCfwcpG2KIWUV0xyEimS59buyNoGJSDycB66tXd0cqNOxa6cHnKK7QAHRDrrPWu668yYou7HGiGeoKhN5lV1ztDvTNtpYYvU5aNYejBq+EoFABgX5pLjguyTW7JpTe6ZdlIcgrSqlldjNsMHEc8xmHq1lJDJDJeC4PzIgUMYxaNOiYa5rxTABvYBk8O2qrxFqDAT3ym9p50iYFIjTrkLVwBK3HPW+Qlto7gf/rUHDYUEhBb0s3HI63bd63VmD21CwxvYta693XIFOR5lgxQzTNvigvWoMfrbO7KcI1Jq0+tCtzMPVnVKqjH8KK+C4Qt7gIBVmYfEMZqLmeVUdAKRqNf + + + + + + + + unlocked + + + + default + + + + + + cisco-ios-xr-id:cisco-ios-xr + + + + + +
+
\ No newline at end of file