summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--vyroute/Routing.py31
-rw-r--r--vyroute/StaticRoute.py79
2 files changed, 109 insertions, 1 deletions
diff --git a/vyroute/Routing.py b/vyroute/Routing.py
index f82ec49..7d952b6 100644
--- a/vyroute/Routing.py
+++ b/vyroute/Routing.py
@@ -1 +1,30 @@
-# author=hochikong \ No newline at end of file
+# author=hochikong
+import StaticRoute
+
+
+class BasicRouting(object):
+ def static_routing(self):
+ pass
+
+ def rip_routing(self):
+ pass
+
+ def ospf_routing(self):
+ 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)
+
+
+
+
+
diff --git a/vyroute/StaticRoute.py b/vyroute/StaticRoute.py
new file mode 100644
index 0000000..1920b72
--- /dev/null
+++ b/vyroute/StaticRoute.py
@@ -0,0 +1,79 @@
+# author=hochikong
+from Exscript.protocols import SSH2
+from Exscript import Account
+
+
+def staticroute(data):
+ """This method provide a basic static router configuration function
+
+ Parameter data example:
+ {'router':'vyos@172.16.77.188','passwd':'vyos',
+ 'config':[{'target':'10.20.10.0/24','next-hop':'10.20.10.1','distance':'1'},
+ {'target':"192.168.20.0/24','next-hop':'192.168.20.1','distance':'1'}
+ ]
+ }
+
+ :param data: a python dictionary
+ :return:a python dictionary
+ """
+ basic_configuration = "set protocols static route %s next-hop %s distance %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 static route
+ for i in data['config']:
+ conn.execute(basic_configuration % (i['target'],
+ i['next-hop'],
+ i['distance']))
+
+# 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}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+