def get_next_hop(src_info, dst_info, qrouter, params):
next_hop_list = []
next_hop = None
username = params['username']
passwd = params['passwd']
src_ip = src_info['ip']
dst_ip = dst_info['ip']
remote_cmd = ' ip route get %s' % dst_ip
cmd = 'sudo ip netns exec ' + qrouter
cmd += ' python run_nms_cmd.py --host_ip %s --username "%s" --passwd "%s" --cmd "%s" ' % \
(src_ip, username, passwd, remote_cmd)
output = run_remote_cmd(cmd)
a = json.loads(output)
if not a['pass']:
return []
json_file = params['json_file']
info = load_json(json_file)
next_hop = {}
for cmd in a['command_list']:
if re.search('ip route get', cmd['cmd']):
m = re.search('\S+\s+via\s+(\S+)', cmd['output'][0])
if m:
next_hop['ip'] = m.group(1)
next_hop['dev'] = 'qr-' + ip_to_intf(info, next_hop['ip'])
next_hop['nms'] = intf_to_namespace(info, next_hop['dev'])
break
next_hop_list.append(next_hop)
cmd = 'sudo ip netns exec ' + next_hop['nms']
cmd += remote_cmd
output = run_remote_cmd(cmd).split('\n')
prev_nms = next_hop['nms']
next_hop = {}
m = re.search('\S+\s+dev\s+(\S+)', output[0])
if m:
next_hop['dev'] = m.group(1)
next_hop['nms'] = prev_nms
next_hop_list.append(next_hop)
return next_hop_list
评论列表
文章目录