-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPathFinder-Client.py
More file actions
172 lines (154 loc) · 5.67 KB
/
Copy pathPathFinder-Client.py
File metadata and controls
172 lines (154 loc) · 5.67 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#******************************************************************************
# MAC path finder - Client part
# equivalent to traceroute but based on MAC address
# Version: 1.0
# Revision history:
# 1.0 - 30/06/2014 : Initial coding (Yannick Castano, Hewlett-Packard)
#
# Pre-requisite: LLDP enables on all switch to switch interfaces
# Management IP address configured on the lowest VLAN interface
#
# Remarks: This script needs more testing
# Any comment/feedback is welcome: castano@hp.com
#******************************************************************************
import socket
import os
import re
import sys
import termios
import time
import comware
__author__ = 'Yannick Castano'
#******************************************************************************
# Terminal input initialization (thanks to Dobias van Ingen)
#******************************************************************************
fd = sys.stdin.fileno();
new = termios.tcgetattr(fd)
new[3] = new[3] | termios.ICANON | termios.ECHO
new[6] [termios.VMIN] = 1
new[6] [termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, new)
termios.tcsendbreak(fd,0)
#******************************************************************************
# Global variables
#******************************************************************************
CLIENTPORT = 50001
SERVERPORT = 50000
TIMEOUT = 60 # seconds
#******************************************************************************
# Procedures
#******************************************************************************
def main_menu( ):
while True:
print "\n"
print "Trace a MAC address: Type T"
print "Quit : Type Q"
choice = raw_input("What is your choice?\n=> ")
if choice.upper() in ['Q','T']: break
return choice.upper()
def get_lldp_mgt_ip(interface):
command = comware.CLI('display lldp local-information interface ' + interface, False)
output = command.get_output()
remote_ip = 'none'
#Extracts the LLDP local IP management address
for s in output:
if s.find('Management address') != -1:
# keeps only the line with the management IP address
if '.' in s.split()[3]:
remote_ip = s.split()[3]
return remote_ip
def find_local_mac_next_if(mac):
command = comware.CLI('display mac-address ' + mac, False)
output = command.get_output()
if len(output) <= 2:
# no mac address corresponding, result is empty
return 'none'
else:
# get the third line and extract the local interface
# first line is the command, second line the header
result = output[2].split()
return result [3]
def find_local_arp_ip(mac):
command = comware.CLI('display arp | include ' + mac, False)
output = command.get_output()
if len(output) == 1:
# no mac address corresponding, result is empty
return 'none'
else:
# get the first line and extract the IP address
result = output[1].split()
return result [0]
def find_lldp_neighbor(interface):
# LLDP rule: the lowest interface ID with an IP address is used as management interface (loopback are excluded)
command = comware.CLI('display lldp neighbor-information interface ' + interface + ' verbose', False)
output = command.get_output()
neighbor = ['none', 'none']
if len(output) != 1:
# extracts the LLDP peer name
for s in output:
if s.find('System name') != -1:
neighbor[0] = s.split()[3]
# extracts the LLDP peer IP management address
for s in output:
if s.find('Management address') != -1:
# keeps only the line with the management IP address
if '.' in s.split()[3]:
neighbor[1] = s.split()[3]
return neighbor
def send_tracemac_request(localhost,neighbor_ip,mac):
# sending on UDP port 50000, any answer will be received on UDP port 50001
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((neighbor_ip, SERVERPORT))
sock.sendall(localhost + ' ' + mac)
sock.close()
return
def local_server_start(localhost):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(TIMEOUT)
sock.bind((localhost, CLIENTPORT))
while True:
received_data = sock.recv(1024)
print received_data
if received_data == 'END': break
sock.close()
def convert_interface_name(intf):
# currently supports 1G, 10G and 40G interfaces
if intf.find('XGE') != -1:
interface = intf.replace('XGE','Ten-GigabitEthernet')
elif intf.find('FGE') != -1:
interface = intf.replace ('FGE', 'FortyGigE')
elif intf.find('GE') != -1:
interface = intf.replace ('GE', 'GigabitEthernet')
return interface
#******************************************************************************
# Main code
#******************************************************************************
choice = main_menu()
# Trace MAC is requested
if choice == 'T':
# Ask user for the MAC address
mac = raw_input("What is the MAC address to trace?\n=> ")
# Try to find if this MAC has a local ARP entry
ip = find_local_arp_ip(mac.lower())
if ip != 'none':
print '[LOCAL]:\t MAC address {} has IP address {}'.format(mac,ip)
# Find the interface where this MAC has been learned
intf = find_local_mac_next_if(mac.lower())
if intf == 'none':
print '[LOCAL]:\t No corresponding MAC address on this device'
else:
interface = convert_interface_name(intf)
neighbor = find_lldp_neighbor(interface)
if neighbor[0] == 'none':
print '[LOCAL]:\t MAC address {} is on {}'.format(mac,interface)
else:
print '[LOCAL]:\t MAC address {} is on {} via switch {} (IP: {})'.format(mac,interface,neighbor[0],neighbor[1])
#find the local management IP address used on that interface
localhost = get_lldp_mgt_ip(interface)
#sending request to the next switch
send_tracemac_request(localhost,neighbor[1],mac)
#starting local server
local_server_start(localhost)
# Quit is requested
else:
print "Goodbye"