summaryrefslogtreecommitdiff
path: root/vyroute/basic_function/StaticRoute.py
blob: f9fa233593e41786f715eb7b4c9e90a9c7d139b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
    """
    static_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 router
        for i in data['config']:
            conn.execute(static_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}