Skip to content

Instantly share code, notes, and snippets.

@supertylerc
Created August 27, 2014 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save supertylerc/3b11111a219b0a49a56c to your computer and use it in GitHub Desktop.
Save supertylerc/3b11111a219b0a49a56c to your computer and use it in GitHub Desktop.
Find Port a Server is Known On
#!/usr/bin/env python2.7
from pprint import pprint
from multiprocessing.dummy import Pool
from functools import partial
from jnpr.junos import Device
from jnpr.junos.op.arp import ArpTable
user_name = 'tyler'
def get_arp(router, ip):
router_session = Device(router, user=user_name)
router_session.open()
arp_table = ArpTable(router_session)
arp_table = arp_table.get()
mac_address = ''
for arp_entry in arp_table:
if arp_entry.ip_address == ip:
mac_address = arp_entry.mac_address
return mac_address
def get_mac_table(switch, mac):
switch_session = Device(switch, user=user_name)
switch_session.open()
results = {switch: []}
mac_table = switch_session.rpc.get_ethernet_switching_table_information()
mac_table = mac_table.find('ethernet-switching-table')
for mac_entry in mac_table:
if mac_entry.tag != 'mac-table-entry': continue
if mac_entry.findtext('mac-address') != mac_address: continue
vlan = mac_entry.findtext('mac-vlan')
interfaces = mac_entry.find('mac-interfaces-list')
for interface in interfaces:
interface_results = {'interface': interface.text,
'vlan': vlan}
results[switch].append(interface_results)
return results
if __name__ == '__main__':
router = 'fw01.example.com'
ip = '192.168.1.1'
mac_address = get_arp(router, ip)
switches = ['sw01.example.com', 'sw02.example.com', 'sw03.example.com']
pool = Pool(4)
switch_results = pool.map(partial(get_mac_table, mac=mac_address), switches)
pool.close()
pool.join()
results = {'ip_address': ip,
'mac_address': mac_address,
'switches': switch_results}
pprint(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment