From 8e2f9aa509a5d90897844c196675836a10068237 Mon Sep 17 00:00:00 2001 From: Hochikong Date: Mon, 23 May 2016 19:38:16 +0800 Subject: add new file and modify some wrong --- vyroute/basic_function/Modifylo.py | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 vyroute/basic_function/Modifylo.py (limited to 'vyroute/basic_function/Modifylo.py') 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} -- cgit v1.2.3