Skip to content

Commit 269b765

Browse files
committed
added base_config example script
1 parent 9770fd5 commit 269b765

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

example/base_config.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/python
2+
3+
"""
4+
Push a default config to a switch
5+
6+
This is free and unencumbered software released into the public domain.
7+
8+
Anyone is free to copy, modify, publish, use, compile, sell, or
9+
distribute this software, either in source code form or as a compiled
10+
binary, for any purpose, commercial or non-commercial, and by any
11+
means.
12+
13+
In jurisdictions that recognize copyright laws, the author or authors
14+
of this software dedicate any and all copyright interest in the
15+
software to the public domain. We make this dedication for the benefit
16+
of the public at large and to the detriment of our heirs and
17+
successors. We intend this dedication to be an overt act of
18+
relinquishment in perpetuity of all present and future rights to this
19+
software under copyright law.
20+
21+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
OTHER DEALINGS IN THE SOFTWARE.
28+
29+
For more information, please refer to <http://unlicense.org/>
30+
31+
32+
Description:
33+
34+
This script can get the configuration from a device and push it to other devices as a default config.
35+
The script automatically changes the hostname to the 2 last octets of the IP. Furthermore it preserves
36+
all port labels that read "BAD"(My friends like to break ports).
37+
38+
get config:
39+
python base_config.py get 10.10.10.1 config-template.json
40+
41+
push config:
42+
python base_config.py push 10.10.10.1 config-template.json
43+
44+
45+
"""
46+
47+
import netonix_api
48+
import json
49+
import sys
50+
import getpass
51+
import traceback
52+
import urllib3
53+
import time
54+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
55+
56+
if __name__ == '__main__':
57+
filename=sys.argv[3]
58+
ip=sys.argv[2]
59+
cmd=sys.argv[1]
60+
pw= getpass.getpass()
61+
username="admin"
62+
n=netonix_api.Netonix()
63+
n.open(ip,username,pw)
64+
n.getConfig()
65+
if(cmd=="get"):
66+
with open(filename, 'w') as fp:
67+
json.dump(n.config, fp,indent=4)
68+
exit(1)
69+
elif(cmd=="push"):
70+
with open(filename) as json_data:
71+
default = json.load(json_data)
72+
default["IPv4_Address"]=ip
73+
a=ip.split(".")
74+
default["Switch_Name"]="%s.%s"%(a[2],a[3])
75+
default["Config_Version"]=n.config["Config_Version"]
76+
i=0
77+
for a in n.config["Ports"]:
78+
i+=1
79+
print("%d:\t%s"%(i,a["Name"]))
80+
if(a["Name"].lower()=="bad"):
81+
default["Ports"][i-1]["Name"]="BAD"
82+
default["Ports"][i-1]["PoE"]="Off"
83+
n.config=default
84+
n.putConfig()
85+
else:
86+
print("Command %s unkown"%cmd)

0 commit comments

Comments
 (0)