Skip to content

Commit

Permalink
Udpate main.py - preliminary v6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
knewell committed Mar 15, 2019
1 parent b136dfb commit b50a7d3
Showing 1 changed file with 83 additions and 81 deletions.
164 changes: 83 additions & 81 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,100 +152,101 @@ def getActiveFlowRoutes(self):

# data = dev.rpc.get_config(filter_xml='routing-options/flow/route/name')
frt = FlowRoutesTable(dev)
frt.get()

for flow in frt:

destination = flow.destination.split(',')

for index, item in enumerate(destination):
_item = item.split('=')
destination[index] = _item[1] if len(_item) > 1 else _item[0]

hash_object = hashlib.sha512(b'{0}{1}'.format(str(destination), str(value['ip'])))
hex_dig = hash_object.hexdigest()
_age = dict()

if len(flow.age) <= 2:
_age['current'] = datetime.timedelta(seconds=int(flow.age))
elif len(flow.age) == 4 or len(flow.age) == 5:
ms = flow.age.split(':')
_age['current'] = datetime.timedelta(minutes=int(ms[0]), seconds=int(ms[1]))
elif len(flow.age) == 7 or len(flow.age) == 8:
ms = flow.age.split(':')
_age['current'] = datetime.timedelta(hours=int(ms[0]), minutes=int(ms[1]),
seconds=int(ms[2]))
else:
pattern = r'(.*)\s(.*?):(.*?):(.*)'
for flowtable in ['inetflow.0', 'inet6flow.0']:
frt.get(table=flowtable)

for flow in frt:

destination = flow.destination.split(',')

for index, item in enumerate(destination):
_item = item.split('=')
destination[index] = _item[1] if len(_item) > 1 else _item[0]

hash_object = hashlib.sha512(b'{0}{1}'.format(str(destination), str(value['ip'])))
hex_dig = hash_object.hexdigest()
_age = dict()

if len(flow.age) <= 2:
_age['current'] = datetime.timedelta(seconds=int(flow.age))
elif len(flow.age) == 4 or len(flow.age) == 5:
ms = flow.age.split(':')
_age['current'] = datetime.timedelta(minutes=int(ms[0]), seconds=int(ms[1]))
elif len(flow.age) == 7 or len(flow.age) == 8:
ms = flow.age.split(':')
_age['current'] = datetime.timedelta(hours=int(ms[0]), minutes=int(ms[1]),
seconds=int(ms[2]))
else:
pattern = r'(.*)\s(.*?):(.*?):(.*)'
regex = re.compile(pattern)
age = re.findall(regex, flow.age)
_age['current'] = datetime.timedelta(days=int(age[0][0][:-1]), hours=int(age[0][1]),
minutes=int(age[0][2]), seconds=int(age[0][3]))

pattern = r'([^\s]+)'
regex = re.compile(pattern)
age = re.findall(regex, flow.age)
_age['current'] = datetime.timedelta(days=int(age[0][0][:-1]), hours=int(age[0][1]),
minutes=int(age[0][2]), seconds=int(age[0][3]))
_krt_actions = re.findall(regex, flow.tsi)

pattern = r'([^\s]+)'
regex = re.compile(pattern)
_krt_actions = re.findall(regex, flow.tsi)
if len(_krt_actions) <= 4:
krt_actions = _krt_actions
else:
krt_actions = _krt_actions[4]

if len(_krt_actions) <= 4:
krt_actions = _krt_actions
else:
krt_actions = _krt_actions[4]
# Junos 14.1RX different XPATH for BGP communities
version = dev.facts['version'].split('R')[0].split('.')

# Junos 14.1RX different XPATH for BGP communities
version = dev.facts['version'].split('R')[0].split('.')
if int(version[0]) <= 14 and int(version[1]) <= 1:

if int(version[0]) <= 14 and int(version[1]) <= 1:
if isinstance(flow.action_141, str):
if 'traffic-action' in flow.action_141:
commAction = flow.action_141.split(":")[1].lstrip().strip()
else:
commAction = flow.action_141

if isinstance(flow.action_141, str):
if 'traffic-action' in flow.action_141:
commAction = flow.action_141.split(":")[1].lstrip().strip()
elif isinstance(flow.action_141, list):
commAction = flow.action_141[1].split(':')[1].lstrip().strip()
else:
commAction = flow.action_141

elif isinstance(flow.action_141, list):
commAction = flow.action_141[1].split(':')[1].lstrip().strip()
else:
commAction = flow.action_141

else:
if isinstance(flow.action, str):
if 'traffic-action' in flow.action:
commAction = flow.action.split(":")[1].lstrip().strip()
else:
commAction = flow.action

if isinstance(flow.action, str):
if 'traffic-action' in flow.action:
commAction = flow.action.split(":")[1].lstrip().strip()
elif isinstance(flow.action, list):
commAction = flow.action[1].split(':')[1].lstrip().strip()
else:
commAction = flow.action

elif isinstance(flow.action, list):
commAction = flow.action[1].split(':')[1].lstrip().strip()
else:
commAction = flow.action

if hex_dig not in self.flow_active:
if hex_dig not in self.flow_active:

self.flow_active[hex_dig] = {'router': name, 'term': flow.term, 'destination': destination,
'commAction': commAction, 'krtAction': krt_actions,
'age': str(_age['current']),
'hash': hex_dig, 'status': 'new'}
else:
self.flow_active[hex_dig] = {'router': name, 'term': flow.term, 'destination': destination,
'commAction': commAction, 'krtAction': krt_actions,
'age': str(_age['current']),
'hash': hex_dig, 'status': 'new'}
else:

if 'term:N/A' in flow['term']:
self.flow_active.pop(hex_dig, None)
if 'term:N/A' in flow['term']:
self.flow_active.pop(hex_dig, None)

if _age['current']:
if _age['current']:

if _age['current'] > datetime.timedelta(hours=t.hour, minutes=t.minute,
seconds=t.second):
self.flow_active[hex_dig]['status'] = 'old'
if _age['current'] > datetime.timedelta(hours=t.hour, minutes=t.minute,
seconds=t.second):
self.flow_active[hex_dig]['status'] = 'old'

try:
if hex_dig in self.flow_active:
self.flow_active[hex_dig].update({'term': flow.term, 'destination': destination,
'commAction': commAction,
'krtAction': krt_actions,
'age': str(_age['current'])})
try:
if hex_dig in self.flow_active:
self.flow_active[hex_dig].update({'term': flow.term, 'destination': destination,
'commAction': commAction,
'krtAction': krt_actions,
'age': str(_age['current'])})

except KeyError as ke:
return False, ke.message
except KeyError as ke:
return False, ke.message

return True, self.flow_active

Expand All @@ -261,18 +262,19 @@ def getActiveFlowRouteFilter(self):
with Device(host=value['ip'], user=self.dev_user, password=self.dev_pw) as dev:

frft = FlowFilterTable(dev)
frft.get()
for table in ['__flowspec_default_inet__', '__flowspec_default_inet6__']:
frft.get(filtername=table)

for filter in frft:
for filter in frft:

data = filter.name.split(',')
data = filter.name.split(',')

for didx, item in enumerate(data):
_item = item.split('=')
data[didx] = _item[1] if len(_item) > 1 else _item[0]
for didx, item in enumerate(data):
_item = item.split('=')
data[didx] = _item[1] if len(_item) > 1 else _item[0]

self.filter_active[name].append({'data': data, 'packet_count': filter.packet_count,
'byte_count': filter.byte_count})
self.filter_active[name].append({'data': data, 'packet_count': filter.packet_count,
'byte_count': filter.byte_count})

return True, self.filter_active

Expand Down

0 comments on commit b50a7d3

Please sign in to comment.