mirror of
https://github.com/sileht/bird-lg.git
synced 2024-11-24 15:54:43 +01:00
Basic python reformatting
Transform all tabs to spaces and remove trailing spaces.
This commit is contained in:
parent
ca4550ae74
commit
a45ae45408
235
bird.py
235
bird.py
|
@ -25,60 +25,60 @@ import sys
|
||||||
BUFSIZE = 4096
|
BUFSIZE = 4096
|
||||||
|
|
||||||
SUCCESS_CODES = {
|
SUCCESS_CODES = {
|
||||||
"0000" : "OK",
|
"0000" : "OK",
|
||||||
"0001" : "Welcome",
|
"0001" : "Welcome",
|
||||||
"0002" : "Reading configuration",
|
"0002" : "Reading configuration",
|
||||||
"0003" : "Reconfigured",
|
"0003" : "Reconfigured",
|
||||||
"0004" : "Reconfiguration in progress",
|
"0004" : "Reconfiguration in progress",
|
||||||
"0005" : "Reconfiguration already in progress, queueing",
|
"0005" : "Reconfiguration already in progress, queueing",
|
||||||
"0006" : "Reconfiguration ignored, shutting down",
|
"0006" : "Reconfiguration ignored, shutting down",
|
||||||
"0007" : "Shutdown ordered",
|
"0007" : "Shutdown ordered",
|
||||||
"0008" : "Already disabled",
|
"0008" : "Already disabled",
|
||||||
"0009" : "Disabled",
|
"0009" : "Disabled",
|
||||||
"0010" : "Already enabled",
|
"0010" : "Already enabled",
|
||||||
"0011" : "Enabled",
|
"0011" : "Enabled",
|
||||||
"0012" : "Restarted",
|
"0012" : "Restarted",
|
||||||
"0013" : "Status report",
|
"0013" : "Status report",
|
||||||
"0014" : "Route count",
|
"0014" : "Route count",
|
||||||
"0015" : "Reloading",
|
"0015" : "Reloading",
|
||||||
"0016" : "Access restricted",
|
"0016" : "Access restricted",
|
||||||
}
|
}
|
||||||
|
|
||||||
TABLES_ENTRY_CODES = {
|
TABLES_ENTRY_CODES = {
|
||||||
"1000" : "BIRD version",
|
"1000" : "BIRD version",
|
||||||
"1001" : "Interface list",
|
"1001" : "Interface list",
|
||||||
"1002" : "Protocol list",
|
"1002" : "Protocol list",
|
||||||
"1003" : "Interface address",
|
"1003" : "Interface address",
|
||||||
"1004" : "Interface flags",
|
"1004" : "Interface flags",
|
||||||
"1005" : "Interface summary",
|
"1005" : "Interface summary",
|
||||||
"1006" : "Protocol details",
|
"1006" : "Protocol details",
|
||||||
"1007" : "Route list",
|
"1007" : "Route list",
|
||||||
"1008" : "Route details",
|
"1008" : "Route details",
|
||||||
"1009" : "Static route list",
|
"1009" : "Static route list",
|
||||||
"1010" : "Symbol list",
|
"1010" : "Symbol list",
|
||||||
"1011" : "Uptime",
|
"1011" : "Uptime",
|
||||||
"1012" : "Route extended attribute list",
|
"1012" : "Route extended attribute list",
|
||||||
"1013" : "Show ospf neighbors",
|
"1013" : "Show ospf neighbors",
|
||||||
"1014" : "Show ospf",
|
"1014" : "Show ospf",
|
||||||
"1015" : "Show ospf interface",
|
"1015" : "Show ospf interface",
|
||||||
"1016" : "Show ospf state/topology",
|
"1016" : "Show ospf state/topology",
|
||||||
"1017" : "Show ospf lsadb",
|
"1017" : "Show ospf lsadb",
|
||||||
"1018" : "Show memory",
|
"1018" : "Show memory",
|
||||||
}
|
}
|
||||||
|
|
||||||
ERROR_CODES = {
|
ERROR_CODES = {
|
||||||
"8000" : "Reply too long",
|
"8000" : "Reply too long",
|
||||||
"8001" : "Route not found",
|
"8001" : "Route not found",
|
||||||
"8002" : "Configuration file error",
|
"8002" : "Configuration file error",
|
||||||
"8003" : "No protocols match",
|
"8003" : "No protocols match",
|
||||||
"8004" : "Stopped due to reconfiguration",
|
"8004" : "Stopped due to reconfiguration",
|
||||||
"8005" : "Protocol is down => cannot dump",
|
"8005" : "Protocol is down => cannot dump",
|
||||||
"8006" : "Reload failed",
|
"8006" : "Reload failed",
|
||||||
"8007" : "Access denied",
|
"8007" : "Access denied",
|
||||||
|
|
||||||
"9000" : "Command too long",
|
"9000" : "Command too long",
|
||||||
"9001" : "Parse error",
|
"9001" : "Parse error",
|
||||||
"9002" : "Invalid symbol type",
|
"9002" : "Invalid symbol type",
|
||||||
}
|
}
|
||||||
|
|
||||||
END_CODES = ERROR_CODES.keys() + SUCCESS_CODES.keys()
|
END_CODES = ERROR_CODES.keys() + SUCCESS_CODES.keys()
|
||||||
|
@ -87,91 +87,88 @@ global bird_sockets
|
||||||
bird_sockets = {}
|
bird_sockets = {}
|
||||||
|
|
||||||
def BirdSocketSingleton(host, port):
|
def BirdSocketSingleton(host, port):
|
||||||
global bird_sockets
|
global bird_sockets
|
||||||
s = bird_sockets.get((host,port), None)
|
s = bird_sockets.get((host,port), None)
|
||||||
if not s:
|
if not s:
|
||||||
s = BirdSocket(host,port)
|
s = BirdSocket(host,port)
|
||||||
bird_sockets[(host,port)] = s
|
bird_sockets[(host,port)] = s
|
||||||
return s
|
return s
|
||||||
|
|
||||||
class BirdSocket:
|
class BirdSocket:
|
||||||
|
|
||||||
def __init__(self, host="", port="", file=""):
|
def __init__(self, host="", port="", file=""):
|
||||||
self.__file = file
|
self.__file = file
|
||||||
self.__host = host
|
self.__host = host
|
||||||
self.__port = port
|
self.__port = port
|
||||||
self.__sock = None
|
self.__sock = None
|
||||||
|
|
||||||
def __connect(self):
|
def __connect(self):
|
||||||
if self.__sock: return
|
if self.__sock: return
|
||||||
|
|
||||||
if not file:
|
if not file:
|
||||||
self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.__sock.settimeout(3.0)
|
self.__sock.settimeout(3.0)
|
||||||
self.__sock.connect((self.__host, self.__port))
|
self.__sock.connect((self.__host, self.__port))
|
||||||
else:
|
else:
|
||||||
self.__sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
self.__sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
self.__sock.settimeout(3.0)
|
self.__sock.settimeout(3.0)
|
||||||
self.__sock.connect(self.__file)
|
self.__sock.connect(self.__file)
|
||||||
|
|
||||||
# read welcome message
|
# read welcome message
|
||||||
self.__sock.recv(1024)
|
self.__sock.recv(1024)
|
||||||
self.cmd("restrict")
|
self.cmd("restrict")
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if self.__sock:
|
if self.__sock:
|
||||||
try: self.__sock.close()
|
try: self.__sock.close()
|
||||||
except: pass
|
except: pass
|
||||||
self.__sock = None
|
self.__sock = None
|
||||||
|
|
||||||
def cmd(self, cmd):
|
def cmd(self, cmd):
|
||||||
try:
|
try:
|
||||||
self.__connect()
|
self.__connect()
|
||||||
self.__sock.send(cmd + "\n")
|
self.__sock.send(cmd + "\n")
|
||||||
data = self.__read()
|
data = self.__read()
|
||||||
return data
|
return data
|
||||||
except socket.error:
|
except socket.error:
|
||||||
why = sys.exc_info()[1]
|
why = sys.exc_info()[1]
|
||||||
self.close()
|
self.close()
|
||||||
return False, "Bird connection problem: %s" % why
|
return False, "Bird connection problem: %s" % why
|
||||||
|
|
||||||
def __read(self):
|
def __read(self):
|
||||||
code = "7000" # Not used in bird
|
code = "7000" # Not used in bird
|
||||||
parsed_string = ""
|
parsed_string = ""
|
||||||
lastline = ""
|
lastline = ""
|
||||||
|
|
||||||
while code not in END_CODES:
|
while code not in END_CODES:
|
||||||
data = self.__sock.recv(BUFSIZE)
|
data = self.__sock.recv(BUFSIZE)
|
||||||
|
|
||||||
lines = (lastline + data).split("\n")
|
lines = (lastline + data).split("\n")
|
||||||
if len(data) == BUFSIZE:
|
if len(data) == BUFSIZE:
|
||||||
lastline = lines[-1]
|
lastline = lines[-1]
|
||||||
lines = lines[:-1]
|
lines = lines[:-1]
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
code = line[0:4]
|
code = line[0:4]
|
||||||
|
|
||||||
if not line.strip():
|
if not line.strip():
|
||||||
continue
|
continue
|
||||||
elif code == "0000":
|
elif code == "0000":
|
||||||
return True, parsed_string
|
return True, parsed_string
|
||||||
elif code in SUCCESS_CODES.keys():
|
elif code in SUCCESS_CODES.keys():
|
||||||
return True, SUCCESS_CODES.get(code)
|
return True, SUCCESS_CODES.get(code)
|
||||||
elif code in ERROR_CODES.keys():
|
elif code in ERROR_CODES.keys():
|
||||||
return False, ERROR_CODES.get(code)
|
return False, ERROR_CODES.get(code)
|
||||||
elif code[0] in [ "1", "2"] :
|
elif code[0] in [ "1", "2"] :
|
||||||
parsed_string += line[5:] + "\n"
|
parsed_string += line[5:] + "\n"
|
||||||
elif code[0] == " ":
|
elif code[0] == " ":
|
||||||
parsed_string += line[1:] + "\n"
|
parsed_string += line[1:] + "\n"
|
||||||
elif code[0] == "+":
|
elif code[0] == "+":
|
||||||
parsed_string += line[1:]
|
parsed_string += line[1:]
|
||||||
else:
|
else:
|
||||||
parsed_string += "<<<unparsable_string(%s)>>>\n"%line
|
parsed_string += "<<<unparsable_string(%s)>>>\n"%line
|
||||||
|
|
||||||
return True, parsed_string
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['BirdSocketSingleton' , 'BirdSocket' ]
|
|
||||||
|
|
||||||
|
return True, parsed_string
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ['BirdSocketSingleton', 'BirdSocket']
|
||||||
|
|
2
lg.py
2
lg.py
|
@ -461,7 +461,6 @@ def show_bgpmap():
|
||||||
if "%s*" % label_without_star not in labels:
|
if "%s*" % label_without_star not in labels:
|
||||||
labels = [ kwargs["label"] ] + [ l for l in labels if not l.startswith(label_without_star) ]
|
labels = [ kwargs["label"] ] + [ l for l in labels if not l.startswith(label_without_star) ]
|
||||||
labels = sorted(labels, cmp=lambda x,y: x.endswith("*") and -1 or 1)
|
labels = sorted(labels, cmp=lambda x,y: x.endswith("*") and -1 or 1)
|
||||||
|
|
||||||
label = escape("\r".join(labels))
|
label = escape("\r".join(labels))
|
||||||
e.set_label(label)
|
e.set_label(label)
|
||||||
return edges[edge_tuple]
|
return edges[edge_tuple]
|
||||||
|
@ -511,7 +510,6 @@ def show_bgpmap():
|
||||||
else:
|
else:
|
||||||
hop_label = ""
|
hop_label = ""
|
||||||
|
|
||||||
|
|
||||||
if _as == asmap[-1]:
|
if _as == asmap[-1]:
|
||||||
add_node(_as, fillcolor="#F5A9A9", shape="box", )
|
add_node(_as, fillcolor="#F5A9A9", shape="box", )
|
||||||
else:
|
else:
|
||||||
|
|
15
lgproxy.py
15
lgproxy.py
|
@ -77,14 +77,13 @@ def traceroute():
|
||||||
|
|
||||||
src = []
|
src = []
|
||||||
if request.path == '/traceroute6':
|
if request.path == '/traceroute6':
|
||||||
traceroute = traceroute6
|
traceroute = traceroute6
|
||||||
if app.config.get("IPV6_SOURCE",""):
|
if app.config.get("IPV6_SOURCE", ""):
|
||||||
src = [ "-s", app.config.get("IPV6_SOURCE") ]
|
src = [ "-s", app.config.get("IPV6_SOURCE") ]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
traceroute = traceroute4
|
traceroute = traceroute4
|
||||||
if app.config.get("IPV4_SOURCE",""):
|
if app.config.get("IPV4_SOURCE",""):
|
||||||
src = [ "-s", app.config.get("IPV4_SOURCE") ]
|
src = [ "-s", app.config.get("IPV4_SOURCE") ]
|
||||||
|
|
||||||
query = request.args.get("q","")
|
query = request.args.get("q","")
|
||||||
query = unquote(query)
|
query = unquote(query)
|
||||||
|
@ -97,11 +96,9 @@ def traceroute():
|
||||||
options = [ '-A', '-q1', '-N32', '-w1', '-m15' ]
|
options = [ '-A', '-q1', '-N32', '-w1', '-m15' ]
|
||||||
command = traceroute + src + options + [ query ]
|
command = traceroute + src + options + [ query ]
|
||||||
result = subprocess.Popen( command , stdout=subprocess.PIPE).communicate()[0].decode('utf-8', 'ignore').replace("\n","<br>")
|
result = subprocess.Popen( command , stdout=subprocess.PIPE).communicate()[0].decode('utf-8', 'ignore').replace("\n","<br>")
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/bird")
|
@app.route("/bird")
|
||||||
@app.route("/bird6")
|
@app.route("/bird6")
|
||||||
def bird():
|
def bird():
|
||||||
|
|
42
toolbox.py
42
toolbox.py
|
@ -29,16 +29,16 @@ resolv.timeout = 0.5
|
||||||
resolv.lifetime = 1
|
resolv.lifetime = 1
|
||||||
|
|
||||||
def resolve(n, q):
|
def resolve(n, q):
|
||||||
return str(resolv.query(n,q)[0])
|
return str(resolv.query(n,q)[0])
|
||||||
|
|
||||||
def mask_is_valid(n):
|
def mask_is_valid(n):
|
||||||
if not n:
|
if not n:
|
||||||
return True
|
return True
|
||||||
try:
|
try:
|
||||||
mask = int(n)
|
mask = int(n)
|
||||||
return ( mask >= 1 and mask <= 128)
|
return ( mask >= 1 and mask <= 128)
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def ipv4_is_valid(n):
|
def ipv4_is_valid(n):
|
||||||
try:
|
try:
|
||||||
|
@ -55,21 +55,21 @@ def ipv6_is_valid(n):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def save_cache_pickle(filename, data):
|
def save_cache_pickle(filename, data):
|
||||||
output = open(filename, 'wb')
|
output = open(filename, 'wb')
|
||||||
pickle.dump(data, output)
|
pickle.dump(data, output)
|
||||||
output.close()
|
output.close()
|
||||||
|
|
||||||
def load_cache_pickle(filename, default = None):
|
def load_cache_pickle(filename, default = None):
|
||||||
try:
|
try:
|
||||||
pkl_file = open(filename, 'rb')
|
pkl_file = open(filename, 'rb')
|
||||||
except IOError:
|
except IOError:
|
||||||
return default
|
return default
|
||||||
try:
|
try:
|
||||||
data = pickle.load(pkl_file)
|
data = pickle.load(pkl_file)
|
||||||
except:
|
except:
|
||||||
data = default
|
data = default
|
||||||
pkl_file.close()
|
pkl_file.close()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def unescape(s):
|
def unescape(s):
|
||||||
want_unicode = False
|
want_unicode = False
|
||||||
|
|
Loading…
Reference in a new issue