Follow me!

LinkedIn Twitter

Who's Online

We have 112 guests online

Python News



  

Newest Downloads

Mar.18

  [zenoss@zenoss2 ~]$ python Zenoss_Template_Manager.py -husage: Examples: python Zenoss_Templa...

HowTo find the port on a switch that a host belongs to, the easy way, part 1
Programming HowTo's - Python HowTo's
Written by Allen Sanabria   
Tuesday, 07 April 2009 10:52

 

The Port Report Project is essentially a Switch Port Mapper Tool or a Switch Port Mapping Tool like a few other commercial products out there, except The Port Report Project is free. Right now there is no GUI or WEB interface for the project but it is in the works.

The other day I was speaking to a buddy of mine. I asked him how cool would it be, if you could just get the port on a switch that you are plugged into, in one line?? So since I thought about it... I figured why not.... I just finished writing the first revision ( I'm assuming more to come). I must say that I am quite pleased with it. Right now you can pass the switch you want to talk too, the community string, and either the MAC or IP address of the host device. In return you will get the MAC Address, IP Address, Port Description (VLAN), and Port you are plugged into.

This script requires, Pysnmp and Pyasn1. If you have python-setuptools, you can install it by running this..
easy_install pysnmp, easy_install pyasn1

 

Three things you will need for this script to work..

  1. Pysnmp
  2. Pyasn1
  3. SNMP Access to the switch you want to talk too and its community string.

To make your life easier you should do the following
  1. install python-setuptools
  2. then run easy_install pysnmp
  3. and easy_install pyasn1
  4. or you can download the 2 modules manually.
    pysnmp and pyasn1
  5. then unzip the 2 files and in each directory run python setup.py install

I am using the following revisions from the python cheese shop pysnmp 4.1.7a  and pyasn1 0.0.6a

 

Revision 2, will even add more features like...

  • Trying to find out what is plugged into a certain Port.
  • Passing a list of switches
  • follow chained switches
  • Gettinginfo from non vlan ports.
  • If you have more ideas, please let me know..

Update 1.7....This is a big update for Port Report.... In this revision the following brands and devices are supported

  1. Cisco
    • Catalyst 6509 w/ Supervisor 720 running IOS
    • Catalyst 3560
    • Catalyst 3550 (SMI)
    • Cisco CIGESM series Chassis Blades
    • Cisco Catalyst 2960
  2. Foundry
    • Foundry Server Iron
  3. Nortel
    • Nortel Passport 8600
    • Nortel 5520 Ethernet Routing Switch
  4. HP
    • HP Procurve 5406xl

The Script has been tested with the above devices... If you have run this script against other devices, please let us know. Also the speed in the report function has drastically increased. I ran this script against a 6509 with 800+ devices connected to it in just over 2 minutes.

You can download the script here 
 Download Port Report 1.9
File Title:Port Report 1.9 (Details)
File Type:py
File Version:1.9
File Size:37.70 Kb
License:
File Author:Allen Sanabria
File HomePage:
Downloads:123
Rating: ( Votes)
Your Vote:

Update..  revision 1.6 has been released.

  • Support for HP PROCURVE switches
  • Added Verbose option
  • Combined switch_report.py in port_report.py

Update, version 1.4...
Only changes that have been made are below..

  • Code Clean up
  • added check by port name
  • Added Error Checking
  • fixed a few bugs with matching the ifIndex port to the bridgeport

So from the looks of it this script does work on Cisco Switches,which has been tested. But does not work on HP Procurve switches. I would like to add this feature to this script but I do not have HP switches to test on.  If anyone would like me to add this feature to HP switches oranyother manufacturer please let me know and maybe we can work together to get it working.

     example below...
python get_port.py -d "switch" -c "community" -m "mac address"

MAC Address = 00 14 28 1f 2d 38
IP Address = 192.168.101.100
PortDescr = Vlan175
Port = GigabitEthernet1/17

python get_port.py -d "switch" -c "community" -i "ip address"

MAC Address = 00 14 28 1f 2d 38
IP Address = 192.168.101.100
PortDescr = Vlan175
Port = GigabitEthernet1/17
 

Please post any questions related to this script here.. http://www.linuxdynasty.org/forums/Scripting/scripting/port_report
You can download the script here... get_port.py

python port_report.py -d 192.168.101.1 -c public --report 
GigabitEthernet1/11,00 21 5a 80 0b a6,192.168.101.23,vlan51,up,up,fullDuplex,1gbps,
GigabitEthernet1/12,00 12 79 83 3b f3,192.168.101.24,vlan51,up,up,fullDuplex,1gbps,

python port_report.py -d 192.168.101.1 -c public -i "192.168.101.201"
This IPAddress is not in the ARP table

python port_report.py -d 192.168.101.1 -c public -i "192.168.101.202"--verbose
Fri Apr 24 15:15:41 2009 Main Started
Fri Apr 24 15:15:41 2009 In snmpget function
Fri Apr 24 15:15:42 2009 Out of snmpget function
Cisco Internetwork Operating System Software
IOS (tm) s72033_rp Software (s72033_rp-JK9S-M), Version 12.2(17d)SXB7, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2005 by cisco Systems, Inc.
Compiled Thu
Fri Apr 24 15:15:42 2009 Finished Checking for mac
Fri Apr 24 15:15:42 2009 Found IP
Fri Apr 24 15:15:42 2009 192.168.101.202 is a Cisco Switch
Fri Apr 24 15:15:42 2009 In generic_mac_or_ip Function
00 14 38 4f 5e 38
Fri Apr 24 15:15:42 2009 Looping Through CommTable
Fri Apr 24 15:15:42 2009 In CommTable For Loop
Fri Apr 24 15:15:42 2009 First If Statement
Fri Apr 24 15:15:42 2009 Looping Through CommTable
Fri Apr 24 15:15:42 2009 In CommTable For Loop
Fri Apr 24 15:15:42 2009 Looping Through CommTable
Fri Apr 24 15:15:42 2009 In CommTable For Loop


python port_report.py -d 192.168.101.1 -c public -i "192.168.101.209"
MAC = 00 14 38 7f 6e 38
Port = GigabitEthernet1/17
Vlan = 175
IPAddr = 192.168.101.209

python port_report.py -d 192.168.101.1 -c public -m "00 14 38 4f 5e 39"
MAC = 00 14 38 4f 5e 39
Port = GigabitEthernet1/17
Vlan = 175
IPAddr = 192.168.101.201

python port_report.py -d 192.168.101.1 -c public -n "1/40"
Port 1/40 has the below MAC Addresses associated with it
MAC = 00 1b 95 97 3c 81
Port = GigabitEthernet1/40
Vlan = 1
IPAddr = The IP Address for this MAC is not in the ARP Table

MAC = 00 15 fa b4 10 06
Port = GigabitEthernet1/40
Vlan = 174
IPAddr = The IP Address for this MAC is not in the ARP Table

Total MAC Addresses associated with this interface 2

python port_report.py -d 192.168.101.1 -c public -n "1/2"
Port 1/2 has the below MAC Addresses associated with it
MAC = 08 00 0f 20 b3 aa
Port = GigabitEthernet1/2
Vlan = 176
IPAddr = 192.168.101.104

MAC = 08 00 0f 21 d3 78
Port = GigabitEthernet1/2
Vlan = 173
IPAddr = 192.168.101.105

MAC = 08 00 0f 20 b3 aa
Port = GigabitEthernet1/2
Vlan = 175
IPAddr = 192.168.101.115


Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
Comments
Search RSS
phila   |Registered |2010-07-21 14:59:38
Hi,

Is there any way to make this script work on the Juniper EX switches?
If I
do a report it does not return any MAC addresses/ports.
I do see in the packet
capture that its doing a bunch of SNMP queries.

Let me know if you need more
info.

Regards,
dynasty   |SAdministrator |2010-07-22 04:05:32
hmmm can you run it in debug mode and post the output in the forums section? The
option is --verbose or -v
phila   |Registered |2010-07-22 07:30:03
Thank you for replying so fast.

Here it is:
[root@lx-log01 ~]# python
port_report.py -c public -d sc9-acc21 -r -v
('Thu Jul 22 09:28:39 2010', ' Main
Started')
Running Switch Report on sc9-acc21
[root@lx-log01 ~]#
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated on Tuesday, 08 September 2009 11:12