summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhochikong <1097225749@qq.com>2016-07-24 22:44:49 +0800
committerhochikong <1097225749@qq.com>2016-07-24 22:44:49 +0800
commitcf04bae19e80a39a62644d1b96f5f58302242280 (patch)
tree063533f5ec5152761bbdc856d79c7673dcda5b4b
parentb07b336d6a674ae18636274e4dd78df1c372cf78 (diff)
downloadpython-vyos-mgmt-cf04bae19e80a39a62644d1b96f5f58302242280.tar.gz
python-vyos-mgmt-cf04bae19e80a39a62644d1b96f5f58302242280.zip
T86 Python management library methods give timeouts
Use pxssh replace exscript Attention:When you execute the logout method,you can't use this object any more.You must create a new BasicRouter substance to login the former VyOS system.
-rw-r--r--vyroute/Router.py62
-rw-r--r--vyroute/basic_function/DeleteRoute.py36
-rw-r--r--vyroute/basic_function/Modifylo.py8
-rw-r--r--vyroute/basic_function/OSPFRoute.py91
-rw-r--r--vyroute/basic_function/RIPRoute.py15
-rw-r--r--vyroute/basic_function/StaticRoute.py12
6 files changed, 158 insertions, 66 deletions
diff --git a/vyroute/Router.py b/vyroute/Router.py
index 0b92002..86f3c78 100644
--- a/vyroute/Router.py
+++ b/vyroute/Router.py
@@ -1,7 +1,6 @@
# Copyright (c) 2016 Hochikong
-from Exscript.protocols import SSH2
-from Exscript import Account
+from pxssh import pxssh
from vyroute.basic_function import Modifylo
from vyroute.basic_function import StaticRoute
from vyroute.basic_function import RIPRoute
@@ -80,9 +79,8 @@ class BasicRouter(Router):
self.__divi = self.__cred.index(":")
self.__username = ''.join(self.__cred[:self.__divi])
self.__passwd = ''.join(self.__cred[self.__divi+1:])
- self.__account = Account(self.__username, self.__passwd)
- self.__conn = SSH2()
- self.__status = {"object": None, "commit": None, "save": None, "configure": None}
+ self.__conn = pxssh()
+ self.__status = {"status": None, "commit": None, "save": None, "configure": None}
def status(self):
"""Check the router inner status
@@ -97,9 +95,8 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__conn.connect(self.__address) is True:
- self.__conn.login(self.__account)
- self.__status["object"] = "login"
+ if self.__conn.login(self.__address, self.__username, self.__passwd) is True:
+ self.__status["status"] = "login"
return {"Result": "Login successfully."}
else:
return {"Error": "Connect Failed."}
@@ -108,12 +105,13 @@ class BasicRouter(Router):
def logout(self):
"""Logout the router
+ Attention! If you logout,you can not reuse this Router object.You should create a new BasicRouter
:return: a python dictionary
"""
try:
- self.__conn.close(force=True)
- self.__status["object"] = "logout"
+ self.__conn.close()
+ self.__status["status"] = "logout"
self.__status["configure"] = None
return {"Result": "Logout successfully."}
except Exception as e:
@@ -125,9 +123,11 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] is not "Yes":
- self.__conn.execute("configure")
+ self.__conn.sendline("configure")
+ self.__conn.prompt(0)
+ self.__conn.set_unique_prompt()
self.__status["configure"] = "Yes"
return {"Result": "Active configure mode successfully."}
else:
@@ -143,12 +143,13 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
if self.__status["commit"] is None:
return {"Error": "You don't need to commit."}
if self.__status["commit"] == "No":
- self.__conn.execute("commit")
+ self.__conn.sendline("commit")
+ self.__conn.prompt()
self.__status["commit"] = "Yes"
return {"Result": "Commit successfully."}
else:
@@ -166,13 +167,14 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
if self.__status["commit"] == "Yes":
if self.__status["save"] is None:
return {"Error": "You don't need to save."}
if self.__status["save"] == "No":
- self.__conn.execute("save")
+ self.__conn.sendline("save")
+ self.__conn.prompt()
self.__status["save"] = "Yes"
return {"Result": "Save successfully."}
else:
@@ -193,10 +195,11 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
if force is True:
- self.__conn.execute("exit discard")
+ self.__conn.sendline("exit discard")
+ self.__conn.prompt()
self.__status["configure"] = "No"
self.__status["save"] = None
self.__status["commit"] = None
@@ -204,7 +207,8 @@ class BasicRouter(Router):
if force is False:
if self.__status["commit"] == "Yes":
if self.__status["save"] == "Yes":
- self.__conn.execute("exit")
+ self.__conn.sendline("exit")
+ self.__conn.prompt()
self.__status["configure"] = "No"
self.__status["save"] = None
self.__status["commit"] = None
@@ -231,7 +235,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = Modifylo.modifylo(self.__conn, data)
if "Result" in res:
@@ -264,7 +268,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = DeleteRoute.deleteroute(self.__conn, data)
if "Result" in res:
@@ -297,7 +301,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = StaticRoute.staticroute(self.__conn, data)
if "Result" in res:
@@ -330,7 +334,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = RIPRoute.riproute(self.__conn, data)
if "Result" in res:
@@ -363,7 +367,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = OSPFRoute.ospfarea(self.__conn, data)
if "Result" in res:
@@ -396,7 +400,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = OSPFRoute.router_id(self.__conn, data)
if "Result" in res:
@@ -429,7 +433,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = OSPFRoute.ospf_redistribute(self.__conn, data)
if "Result" in res:
@@ -457,7 +461,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = OSPFRoute.ospf_adjacency(self.__conn)
if "Result" in res:
@@ -490,7 +494,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = OSPFRoute.ospf_default_route(self.__conn, data)
if "Result" in res:
@@ -523,7 +527,7 @@ class BasicRouter(Router):
:return: a python dictionary
"""
try:
- if self.__status["object"] == "login":
+ if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = OSPFRoute.ospf_route_map(self.__conn, data)
if "Result" in res:
diff --git a/vyroute/basic_function/DeleteRoute.py b/vyroute/basic_function/DeleteRoute.py
index da3f8e9..048406c 100644
--- a/vyroute/basic_function/DeleteRoute.py
+++ b/vyroute/basic_function/DeleteRoute.py
@@ -1,8 +1,4 @@
# Copyright (c) 2016 Hochikong
-from Exscript.protocols import SSH2
-from Exscript import Account
-
-
def deleteroute(obj, data):
"""This method provide a router configuration delete function
@@ -25,17 +21,33 @@ def deleteroute(obj, data):
try:
if data['config'] == "all":
- obj.execute(delete_all_protocols)
- return {"Result": "Delete successfully."}
+ obj.sendline(delete_all_protocols)
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ return obj.before
+ else:
+ return {"Result": "Delete successfully."}
elif data['config'] == 'rip':
- obj.execute(delete_basic_configuration % 'rip')
- return {"Result": "Delete successfully."}
+ obj.sendline(delete_basic_configuration % 'rip')
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ return obj.before
+ else:
+ return {"Result": "Delete successfully."}
elif data['config'] == 'static':
- obj.execute(delete_basic_configuration % 'static')
- return {"Result": "Delete successfully."}
+ obj.sendline(delete_basic_configuration % 'static')
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ return obj.before
+ else:
+ return {"Result": "Delete successfully."}
elif data['config'] == 'ospf':
- obj.execute(delete_basic_configuration % 'ospf')
- return {"Result": "Delete successfully."}
+ obj.sendline(delete_basic_configuration % 'ospf')
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ return obj.before
+ else:
+ return {"Result": "Delete successfully."}
else:
return {"Error": "Nonsupport protocols type."}
except Exception as e:
diff --git a/vyroute/basic_function/Modifylo.py b/vyroute/basic_function/Modifylo.py
index f835c3e..5dca08f 100644
--- a/vyroute/basic_function/Modifylo.py
+++ b/vyroute/basic_function/Modifylo.py
@@ -15,7 +15,11 @@ def modifylo(obj, data):
try:
# Configure loopback interface lo address
- obj.execute(lo_basic_configuration % data['config'])
- return {"Result": "Modify successfully."}
+ obj.sendline(lo_basic_configuration % data['config'])
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ return obj.before
+ else:
+ return {"Result": "Modify successfully."}
except Exception as e:
return {'Error': e}
diff --git a/vyroute/basic_function/OSPFRoute.py b/vyroute/basic_function/OSPFRoute.py
index 0c3367c..b7c1c40 100644
--- a/vyroute/basic_function/OSPFRoute.py
+++ b/vyroute/basic_function/OSPFRoute.py
@@ -14,8 +14,12 @@ def ospfarea(obj, data):
try:
# Configure ospf area
- obj.execute(ospf_basic_configuration % (data['config']['area'], data['config']['network']))
- return {"Result": "Configured successfully"}
+ obj.sendline(ospf_basic_configuration % (data['config']['area'], data['config']['network']))
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ return obj.before
+ else:
+ return {"Result": "Configured successfully"}
except Exception as e:
return {"Error": e}
@@ -34,8 +38,12 @@ def router_id(obj, data):
router_id_configuration = "set protocols ospf parameters router-id %s"
try:
# Configure router id
- obj.execute(router_id_configuration % data['config']['id'])
- return {"Result": "Configured successfully"}
+ obj.sendline(router_id_configuration % data['config']['id'])
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ return obj.before
+ else:
+ return {"Result": "Configured successfully"}
except Exception as e:
return {"Error": e}
@@ -55,29 +63,46 @@ def ospf_redistribute(obj, data):
"1": "set protocols ospf redistribute connected route-map CONNECT",
}
try:
- obj.execute(redistribute_configuration['0'] % data['config']['type'])
- obj.execute(redistribute_configuration['1'])
- return {"Result": "Configured successfully"}
+ reg = 0
+ error_message = []
+ obj.sendline(redistribute_configuration['0'] % data['config']['type'])
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ error_message.append(obj.before)
+ reg += 1
+ obj.sendline(redistribute_configuration['1'])
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ error_message.append(obj.before)
+ reg += 1
+ if reg > 0:
+ return error_message
+ else:
+ return {"Result": "Configured successfully"}
except Exception as e:
return {"Error": e}
def ospf_adjacency(obj):
- """This method execute : set protocols ospf log-adjacency-changes
+ """This method sendline : set protocols ospf log-adjacency-changes
:param obj: a connection object
:return: a python dictionary
"""
log_adjacency_changes_configuration = "set protocols ospf log-adjacency-changes"
try:
- obj.execute(log_adjacency_changes_configuration)
- return {"Result": "Configured successfully"}
+ obj.sendline(log_adjacency_changes_configuration)
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ return obj.before
+ else:
+ return {"Result": "Configured successfully"}
except Exception as e:
return {"Error": e}
def ospf_default_route(obj, data):
- """This method execute : set protocols ospf default-information originate always
+ """This method sendline : set protocols ospf default-information originate always
and other commands
Parameter data example:
@@ -93,10 +118,27 @@ def ospf_default_route(obj, data):
"2": "set protocols ospf default-information originate metric-type %s",
}
try:
- obj.execute(default_route_configuration['0'])
- obj.execute(default_route_configuration['1'] % data['config']['metric'])
- obj.execute(default_route_configuration['2'] % data['config']['metric-type'])
- return {"Result": "Configured successfully"}
+ reg = 0
+ error_messsage = []
+ obj.sendline(default_route_configuration['0'])
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ error_messsage.append(obj.before)
+ reg += 1
+ obj.sendline(default_route_configuration['1'] % data['config']['metric'])
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ error_messsage.append(obj.before)
+ reg += 1
+ obj.sendline(default_route_configuration['2'] % data['config']['metric-type'])
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ error_messsage.append(obj.before)
+ reg += 1
+ if reg > 0:
+ return error_messsage
+ else:
+ return {"Result": "Configured successfully"}
except Exception as e:
return {"Error": e}
@@ -116,8 +158,21 @@ def ospf_route_map(obj, data):
"1": "set policy route-map CONNECT rule %s match interface %s",
}
try:
- obj.execute(route_map_configuration['0'] % data['config']['rule'])
- obj.execute(route_map_configuration['1'] % (data['config']['rule'], data['config']['interface']))
- return {"Result": "Configured successfully"}
+ reg = 0
+ error_messsage = []
+ obj.sendline(route_map_configuration['0'] % data['config']['rule'])
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ error_messsage.append(obj.before)
+ reg += 1
+ obj.sendline(route_map_configuration['1'] % (data['config']['rule'], data['config']['interface']))
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ error_messsage.append(obj.before)
+ reg += 1
+ if reg > 0:
+ return error_messsage
+ else:
+ return {"Result": "Configured successfully"}
except Exception as e:
return {"Error": e}
diff --git a/vyroute/basic_function/RIPRoute.py b/vyroute/basic_function/RIPRoute.py
index a493eac..cb421e0 100644
--- a/vyroute/basic_function/RIPRoute.py
+++ b/vyroute/basic_function/RIPRoute.py
@@ -14,8 +14,21 @@ def riproute(obj, data):
redistribute_configuration = "set protocols rip redistribute connected"
try:
# Configure RIP router
+ reg = 0
+ error_messsage = []
obj.execute(rip_basic_configuration % data['config'])
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ error_messsage.append(obj.before)
+ reg += 1
obj.execute(redistribute_configuration)
- return {"Result": "Configured successfully"}
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ error_messsage.append(obj.before)
+ reg += 1
+ if reg > 0:
+ return error_messsage
+ else:
+ return {"Result": "Configured successfully"}
except Exception as e:
return {"Error": e}
diff --git a/vyroute/basic_function/StaticRoute.py b/vyroute/basic_function/StaticRoute.py
index b70ed19..9bdf378 100644
--- a/vyroute/basic_function/StaticRoute.py
+++ b/vyroute/basic_function/StaticRoute.py
@@ -14,9 +14,13 @@ def staticroute(obj, data):
try:
# Configure static router
- obj.execute(static_basic_configuration % (data['config']['target'],
- data['config']['next-hop'],
- data['config']['distance']))
- return {"Result": "Configured successfully"}
+ obj.sendline(static_basic_configuration % (data['config']['target'],
+ data['config']['next-hop'],
+ data['config']['distance']))
+ obj.prompt()
+ if len(obj.before) > obj.before.index('\r\n') + 2:
+ return obj.before
+ else:
+ return {"Result": "Configured successfully"}
except Exception as e:
return {'Error': e}