-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_mac.py
42 lines (32 loc) · 1.08 KB
/
search_mac.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
#! /usr/bin/python
import MySQLdb as mdb
import prettytable
import sys
import creds
def main():
try:
ip = sys.argv[1]
except IndexError:
ip = raw_input("\nWhich IP address are you interested in? ")
con = mdb.connect('localhost', creds.network_db_user, creds.network_db_pw, 'network');
cur = con.cursor(mdb.cursors.DictCursor)
sql_arp = "SELECT * FROM ARP_Table Where Node_IP='%s'" % ip
cur.execute(sql_arp)
arp_entry = cur.fetchall()
mac = arp_entry[0]["Node_MAC"]
sql_mac = "SELECT * FROM MAC_Table Where Node_MAC='%s' ORDER BY Switch, Port" % mac
cur.execute(sql_mac)
mac_entry = cur.fetchall()
if mac_entry:
tab = prettytable.PrettyTable(["Switch Name", "Switch IP", "VLAN", "Port", "Device IP", "Device MAC"])
for entry in mac_entry:
tab.add_row([entry["Switch"], entry["Switch_IP"], entry["VLAN"], entry["Port"], ip, entry["Node_MAC"]])
print(tab)
print("\n")
else:
print("\nNo information found for that IP Address. Exiting...\n")
if __name__ == "__main__":
try:
main()
except IndexError:
print("\nIP Address not found in database. Exiting...\n")