route.py

 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
import os, subprocess, popen2, string
tekko = "xxx.xxx.xxx.xxx"
#dev_tekko = "eth3"
bs = "xxx.xxx.xxx.xxx"
#dev_bs = "eth2"
def set_route(req, prov):
s = """<html> Ok </html>"""
if not (prov):
return "A required parameter is missing, \
please go back and correct the error"
if prov == '1':
subprocess.call("sudo ip ro ch default via %s metric 1" % (bs), shell=True)
subprocess.call("sudo ip ro ch default via %s metric 2" % (tekko), shell=True)
# s = """ <html> %s </html> """ % prov
elif prov == '2':
subprocess.call("sudo ip ro ch default via %s metric 1" % (tekko), shell=True)
subprocess.call("sudo ip ro ch default via %s metric 2" % (bs), shell=True)
# s = """ <html> %s </html> """ % prov
else:
s = """<html> Error </html>"""
return s
def get_route(req):
z, z2 = os.popen2("sudo ip ro")
rd = z2.readlines()
z.close(); z2.close()
for line in rd:
k = string.split(line)
if k[0] == 'default':
if k[2] == bs:
pr = '1'
elif k[2] == tekko:
pr = '2'
else:
pr = '0'
break
return pr