diff options
author | Hochikong <michellehzg@gmail.com> | 2016-05-23 19:38:16 +0800 |
---|---|---|
committer | Hochikong <michellehzg@gmail.com> | 2016-05-23 19:38:16 +0800 |
commit | 8e2f9aa509a5d90897844c196675836a10068237 (patch) | |
tree | c5aaf9af194d730ecd1ce486e64b07cd2f39b96c | |
parent | 71db4ed81a299a8f4b1947272bccb7966360f98f (diff) | |
download | python-vyos-mgmt-8e2f9aa509a5d90897844c196675836a10068237.tar.gz python-vyos-mgmt-8e2f9aa509a5d90897844c196675836a10068237.zip |
add new file and modify some wrong
-rw-r--r-- | setup.py | 1 | ||||
-rw-r--r-- | vyroute/Routing.py | 21 | ||||
-rw-r--r-- | vyroute/basic_function/DeleteRoute.py | 57 | ||||
-rw-r--r-- | vyroute/basic_function/Modifylo.py | 50 | ||||
-rw-r--r-- | vyroute/basic_function/RIPRoute.py | 55 | ||||
-rw-r--r-- | vyroute/basic_function/StaticRoute.py (renamed from vyroute/StaticRoute.py) | 16 | ||||
-rw-r--r-- | vyroute/basic_function/__init__.py | 0 |
7 files changed, 181 insertions, 19 deletions
@@ -5,6 +5,7 @@ setup( name="vyroute", version="0.1", packages=find_packages(), + install_requires=['Exscript'], description="A library for VyOS routing setting", long_description="A library for VyOS routing setting", diff --git a/vyroute/Routing.py b/vyroute/Routing.py index 7d952b6..8c1ec11 100644 --- a/vyroute/Routing.py +++ b/vyroute/Routing.py @@ -1,29 +1,28 @@ # author=hochikong -import StaticRoute +from vyroute.basic_function import RIPRoute +from vyroute.basic_function import StaticRoute class BasicRouting(object): - def static_routing(self): + def static_routing(self,data): pass - def rip_routing(self): + def rip_routing(self,data): pass - def ospf_routing(self): + def ospf_routing(self,data): pass class Routing(BasicRouting): - def __init__(self, datafile): - """Initial member self.file. - - :param datafile: a string - """ - self.file = datafile - def static_routing(self, data): return StaticRoute.staticroute(data) + def rip_routing(self,data): + return RIPRoute.riproute(data) + + + diff --git a/vyroute/basic_function/DeleteRoute.py b/vyroute/basic_function/DeleteRoute.py new file mode 100644 index 0000000..87af263 --- /dev/null +++ b/vyroute/basic_function/DeleteRoute.py @@ -0,0 +1,57 @@ +# author=hochikong +from Exscript.protocols import SSH2 +from Exscript import Account + + +def deleteroute(data): + """This method provide a router configuration delete function + + Parameter data example: + {'router':'vyos@172.16.77.188','passwd':'vyos', + 'config':'rip' + } + + WARNING! + When you use this function,please don't forget this func will delete all same type + router configuration,when your 'config' in data is 'rip',it will delete all rip router setting. + If you do not want your setting disappear,you can delete router configuration manually or rewrite + this func. + + :param data: a python dictionary + :return: a python dictionary + """ + delete_basic_configuration = "delete protocols %s" + + try: + stringlist = list(data['router']) + divi = stringlist.index('@') + user = ''.join(stringlist[:divi]) + passwd = data['passwd'] + address = ''.join(stringlist[divi + 1:]) + account = Account(user, passwd) + conn = SSH2() + conn.connect(address) + conn.login(account) + + # configure mode + conn.execute("configure") + + # delete specific configuration + conn.execute(delete_basic_configuration % data['config']) + + # commit configuration + conn.execute("commit") + + # save configuration + conn.execute("save") + + # exit configure mode + conn.execute("exit") + + # close connection + conn.close(force=True) + + return {"Result": "Delete successfully"} + + except Exception, e: + return {"Error": e}
\ No newline at end of file diff --git a/vyroute/basic_function/Modifylo.py b/vyroute/basic_function/Modifylo.py new file mode 100644 index 0000000..6808c0d --- /dev/null +++ b/vyroute/basic_function/Modifylo.py @@ -0,0 +1,50 @@ +# author=hochikong +from Exscript.protocols import SSH2 +from Exscript import Account + + +def modifylo(data): + """This method provide a loopback address configuration function + + Parameter data example: + {'router':'vyos@172.16.77.188','passwd':'vyos','config':'1.1.1.1/32' + } + + :param data: a python dictionary + :return: a python dictionary + """ + lo_basic_configuration = "set interfaces loopback lo address %s" + + try: + stringlist = list(data['router']) + divi = stringlist.index('@') + user = ''.join(stringlist[:divi]) + passwd = data['passwd'] + address = ''.join(stringlist[divi+1:]) + account = Account(user, passwd) + conn = SSH2() + conn.connect(address) + conn.login(account) + +# configure mode + conn.execute("configure") + +# configure loopback interface lo address + conn.execute(lo_basic_configuration % data['config']) + + # commit configuration + conn.execute("commit") + + # save configuration + conn.execute("save") + + # exit configure mode + conn.execute("exit") + + # close connection + conn.close(force=True) + + return {"Result": "Configured successfully"} + + except Exception, e: + return {'Error': e} diff --git a/vyroute/basic_function/RIPRoute.py b/vyroute/basic_function/RIPRoute.py new file mode 100644 index 0000000..f559a17 --- /dev/null +++ b/vyroute/basic_function/RIPRoute.py @@ -0,0 +1,55 @@ +# author=hochikong +from Exscript.protocols import SSH2 +from Exscript import Account + + +def riproute(data): + """This method provide a RIP protocols router configuration function + + Parameter data example: + {'router':'vyos@172.16.77.188','passwd':'vyos', + 'config':['192.168.10.0/24','10.20.10.0/24'], + } + + :param data: a python dictionary + :return: a python dictionary + """ + rip_basic_configuration = "set protocols rip network %s" + redistribute_configuration = "set protocols rip redistribute connected" + try: + stringlist = list(data['router']) + divi = stringlist.index('@') + user = ''.join(stringlist[:divi]) + passwd = data['passwd'] + address = ''.join(stringlist[divi + 1:]) + account = Account(user, passwd) + conn = SSH2() + conn.connect(address) + conn.login(account) + + # configure mode + conn.execute("configure") + + # configure RIP router + for i in data['config']: + conn.execute(rip_basic_configuration % i) + + conn.execute(redistribute_configuration) + + # commit configuration + conn.execute("commit") + + # save configuration + conn.execute("save") + + # exit configure mode + conn.execute("exit") + + # close connection + conn.close(force=True) + + return {"Result": "Configured successfully"} + + except Exception, e: + return {"Error": e} + diff --git a/vyroute/StaticRoute.py b/vyroute/basic_function/StaticRoute.py index 1920b72..f9fa233 100644 --- a/vyroute/StaticRoute.py +++ b/vyroute/basic_function/StaticRoute.py @@ -16,7 +16,7 @@ def staticroute(data): :param data: a python dictionary :return:a python dictionary """ - basic_configuration = "set protocols static route %s next-hop %s distance %s" + static_basic_configuration = "set protocols static route %s next-hop %s distance %s" try: stringlist = list(data['router']) @@ -29,25 +29,25 @@ def staticroute(data): conn.connect(address) conn.login(account) -# configure mode + # configure mode conn.execute("configure") -# configure static route + # configure static router for i in data['config']: - conn.execute(basic_configuration % (i['target'], + conn.execute(static_basic_configuration % (i['target'], i['next-hop'], i['distance'])) -# commit configuration + # commit configuration conn.execute("commit") -# save configuration + # save configuration conn.execute("save") -# exit configure mode + # exit configure mode conn.execute("exit") -# close connection + # close connection conn.close(force=True) return {"Result": "Configured successfully"} diff --git a/vyroute/basic_function/__init__.py b/vyroute/basic_function/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/vyroute/basic_function/__init__.py |