Advertisement
qiwichupa

IPScan - port_report.py

Aug 5th, 2011
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 44.74 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #Copyright (C) 2009  Allen Sanabria
  3. #This program is free software; you can redistribute it and/or modify it under
  4. #the terms of the GNU General Public License as published by the Free Software Foundation;
  5. #either version 2 of the License, or (at your option) any later version.
  6. #This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  7. #without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. #See the GNU General Public License for more details. You should have received a copy of i
  9. #the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc.,
  10. #51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  11.  
  12. """Revision 1.12 09/18/09
  13.    * Added follow switch option. Now when you run the Port Report with the --report option, you an also
  14.      pass the --follow option. This option will follow every dp neighbor connectetd to the switch you are
  15.      scaning as well as the switches it is scanning.
  16. """
  17. """Revision 1.11 09/13/09
  18.    * More code clean up and another increase in speed.
  19.    * Also port_report can now follow EtherChannel
  20.    * Fixed issue, where the matching of the cdp neighbor was not matching correctly
  21. """        
  22. """Revision 1.10 09/09/09
  23. Code Clean up and a slight increase in speed ( by a few seconds ) during the search by mac or ip
  24. """
  25.  
  26. """Revision 1.9 05/08/2009
  27. Code changes and Added CDP support..
  28.  
  29.    * Detect CDP Neighbors during the scan for MAC Addresses or IP Addresses
  30. """
  31.  
  32. """ Revision 1.7 04/30/09
  33. This is a big update for Port Report.... In this revision the following brands and devices are supported
  34.  
  35.   1. Cisco
  36.          * Catalyst 6509 w/ Supervisor 720 running IOS
  37.          * Catalyst 3560
  38.          * Catalyst 3550 (SMI)
  39.          * Cisco CIGESM series Chassis Blades
  40.          * Cisco Catalyst 2960
  41.   2. Foundry
  42.          * Foundry Server Iron
  43.   3. Nortel
  44.          * Nortel Passport 8600
  45.          * Nortel 5520 Ethernet Routing Switch
  46.   4. HP
  47.          * HP Procurve 5406xl
  48.  
  49. """
  50. #Now at the 5th revision 1.5 04/21/09
  51. """This is a complete rewrite of the get_port.py script 04/12/09
  52. This script now accurately reports all MAC Addresses on the Port that you specified
  53.   *Also better error checking added
  54.   *Cleaner Code
  55.   *Reusable Functions
  56. This scipt is intended for Administrator/Engineers who need to find the port on a switch
  57. that they are plugged into using either the MAC Address or the IP Address.
  58. So far this has been tested on Cisco Switches, though I assume it will work on other ones as well"""
  59.  
  60. import sys
  61. import re
  62. import string
  63. import getopt
  64. from time import ctime
  65. from socket import gethostbyaddr
  66.  
  67.  
  68. try:
  69.     from pysnmp.entity.rfc3413.oneliner import cmdgen
  70. except Exception, e:
  71.     print("You need to download pysnmp and pyasn1", e)
  72.     sys.exit(1)
  73.  
  74. def usage():
  75.     print("""
  76.     -d, --device    This is the device you want to scan
  77.     -c, --community This is the SNMP community string to use
  78.     -m, --mac       This is the MAC Address you are using to search foir what port your device is plugged into
  79.     -i, --ip        This is the IP Address you are using to find the MAC Address of the device and The port in the switched it is plugged into
  80.     -n, --pname     This is the Port Name you are searching For
  81.     -v, --verbose   This will print out the time stamps of the for loops and of the functions
  82.     -r, --report    This will print out all the mac addresses on the switch you are scanning and what ports they are connected to.
  83.     -f, --follow    This will follow all the cdp neighbors found per switch when using the -r or --report option.
  84.  
  85.    example below..
  86.    python port_report.py -d 192.168.101.1 -c public -i "192.168.101.201"
  87.    This IPAddress is not in the ARP table
  88.  
  89.    python port_report.py -d 192.168.101.1 -c public -i "192.168.101.209"
  90.    MAC  = 00 14 38 7f 6e 38
  91.    Port = GigabitEthernet1/17
  92.    Vlan = 175
  93.    IPAddr = 192.168.101.209
  94.  
  95.    python port_report.py -d 192.168.101.1 -c public -m "00 14 38 4f 5e 39"
  96.    MAC  = 00 14 38 4f 5e 39
  97.    Port = GigabitEthernet1/17
  98.    Vlan = 175
  99.    IPAddr = 192.168.101.201
  100.  
  101.    python port_report.py -d 192.168.101.1 -c public -n "1/40"
  102.    Port 1/40 has the below MAC Addresses associated with it
  103.    MAC  = 00 1b 95 97 3c 81
  104.    Port = GigabitEthernet1/40
  105.    Vlan = 1
  106.    IPAddr = The IP Address for this MAC is not in the ARP Table
  107.  
  108.    MAC  = 00 15 fa b4 10 06
  109.    Port = GigabitEthernet1/40
  110.    Vlan = 174
  111.    IPAddr = The IP Address for this MAC is not in the ARP Table
  112.  
  113.    Total MAC Addresses associated with this interface 2
  114.  
  115.    python port_report.py -d 192.168.101.1 -c public -n "1/2"
  116.    Port 1/2 has the below MAC Addresses associated with it
  117.    MAC  = 08 00 0f 20 b3 aa
  118.    Port = GigabitEthernet1/2
  119.    Vlan = 176
  120.    IPAddr = 192.168.101.104
  121.  
  122.    python port_report.py -d 192.168.101.1 -c public -r -f
  123.    Running Switch Report on 192.168.101.1
  124.    GigabitEthernet1/40,00 1b 90 99 3d 83,None,None,vlan1,up,up,unknown,1000mb,
  125.    GigabitEthernet10/15,00 23 5e ef 34 81,192.168.101.2,Pointer Record Not set for 192.168.101.62,vlan1,up,up,fullDuplex,1000mb,GIG Laser to 71Fifth
  126.    GigabitEthernet1/34,00 01 02 03 03 05,None,None,vlan174,up,up,unknown,1000mb,CCA_CAS Untrusted Interface IP 168.3
  127.  
  128.  
  129.    """)
  130.     sys.exit(0)
  131.  
  132.  
  133. def main():
  134.     if verbose: print(ctime(), " Main Started")
  135.     if ( community and device and ( mac or ip or pname or report ) ):
  136.         snmperror, switchtype = get( device, community, oTable["sysDescr"], 0 )
  137.         switchtype = str(switchtype)
  138.         if snmperror:
  139.             print(snmperror, "Either Wrong Community String or Firewall or SNMP Not Running")
  140.             sys.exit(1)
  141.         sbrand = None
  142.         scanned_neighbor = False
  143.         dswitch = device
  144.         switch = followSwitch( dswitch, community )
  145.         entIpList = {}
  146.         ipList = []
  147.         entaddr = walk( device, community, oTable["ipAdEntAddr"] )
  148.         for line in entaddr:
  149.             ipList.append(switch.convertOctectIp( line[0][1] ))
  150.         entIpList[device] = ipList
  151.         nip = ""
  152.         nmac = ""
  153.         count = 0
  154.         if mac or ip:
  155.             switch.set_duplex()
  156.             switch.set_speed()
  157.             switch.set_port_name()
  158.             switch.set_phys_addr()
  159.             if verbose: print(switchtype)
  160.             mTable = ""
  161.             if mac:
  162.                 nmac = mac
  163.                 nmac, valid = verify_mac( nmac )
  164.                 if not valid:
  165.                     print("you mac %s is in the wrong format" % (mac))
  166.                     sys.exit(1)
  167.                 nip = switch.findIpByMac( nmac )
  168.             if ip:
  169.                 nip = ip
  170.                 nmac = switch.findMacByIp( nip )
  171.                 if nmac == None:
  172.                     print("This IPAddress is not in the ARP table")
  173.                     sys.exit(1)
  174.  
  175.             if ( re.search("Cisco|PROCURVE|Nortel|ERS|Foundry", switchtype, re.IGNORECASE ) ):
  176.                 mTable, ifIndex = switch.find_mac( nmac, nip )
  177.                 count = 0
  178.                 sswitch = dswitch
  179.                 if (mTable):
  180.                     count += 1
  181.                     for key, val in list(mTable.items()):
  182.                         print("Switch Connected to %s" % ( dswitch ))
  183.                         print("SwitchPort = %s\nSwitchPortSpeed = %s\nSwitchPortDuplex = %s\nSwitchVlan = %s" % \
  184.                               ( val["ifDescr"], val["ifSpeed"], val["ifDuplex"], val["vlan"] ))
  185.                         print("SnmpHostName = %s\nSnmpHostDescr = %s\nHostMAC  = %s\nHostIP = %s\nHostName = %s\n" % \
  186.                               ( val["sysName"], val["sysDescr"], val["nmac"], val["ipAddr"], val["hostName"] ))
  187.                         if re.search("Port-channel", val["ifDescr"]):
  188.                             if verbose: print("ifIndex %d is a %s interface" % ( ifIndex, val["ifDescr"] ))
  189.                             ifIndex_pagp_list = switch.get_pagp_ports( ifIndex )
  190.                             if verbose: print("List of Interfaces is in EtherChannel: ", ifIndex_pagp_list)
  191.                             ifIndex = ifIndex_pagp_list[0]
  192.                 neighbor = followSwitch(dswitch, community).get_cdp_neighbor_ip( ifIndex )
  193.                 if neighbor:
  194.                     if verbose: print(neighbor)
  195.                     sswitch = dswitch
  196.                     while neighbor:
  197.                         old_neighbor = neighbor
  198.                         new_neighbor = followSwitch(sswitch, community)
  199.                         ifIndex, count1 = new_neighbor.get_mac_from_cdp_neighbor( neighbor, nmac, nip )
  200.                         count += count1
  201.                         sswitch = neighbor
  202.                         neighbor = followSwitch(sswitch, community).get_cdp_neighbor_ip( ifIndex )
  203.                         if neighbor:
  204.                             entaddr = walk( sswitch, community, oTable["ipAdEntAddr"] )
  205.                             iplist = []
  206.                             for line in entaddr:
  207.                                 ipList.append(new_neighbor.convertOctectIp( line[0][1] ))
  208.                             entIpList[neighbor] = ipList
  209.                             for key, value in list(entIpList.items()):
  210.                                 for line in value:
  211.                                     if line == neighbor:
  212.                                         scanned_neighbor = True
  213.                                         print("this neighbor has already been scanned")
  214.                                         print(line, neighbor)
  215.                                         neighbor = None
  216.                     if ( count >= 1 ):
  217.                         print("This MAC %s was finally traced to this switch %s" % ( nmac, sswitch ))
  218.             if ( count == 0 ):
  219.                 print("The Mac Address %s is not on this switch %s" % ( nmac, dswitch ))
  220.            
  221.  
  222.         if pname:
  223.             count = 0
  224.             switch.set_duplex()
  225.             switch.set_speed()
  226.             switch.set_port_name()
  227.             switch.set_phys_addr()
  228.             switch.find_port_match( pname )
  229.             ifIndex = switch.get_ifIndex()
  230.             ifName = switch.get_ifName()
  231.             sbrand = switch.get_sbrand()
  232.             connected_macs = []
  233.             if ( re.search("Cisco|PROCURVE", switchtype, re.IGNORECASE ) ):
  234.                 lcomm, lvlan = switch.retreive_communities( )
  235.                 if verbose:  print(ctime(), "Retreiving Community Strings\n %s" % ( lcomm ))
  236.                 for i in range(len(lcomm)):
  237.                     mdict = switch.return_mac_by_ifIndex( lcomm[i], lvlan[i] )
  238.                     if mdict:
  239.                         connected_macs.append(mdict)
  240.             elif ( re.search("Nortel|ERS|Foundry", switchtype, re.IGNORECASE ) ):
  241.                 comm = community
  242.                 vlan = None
  243.                 mdict = switch.return_mac_by_ifIndex( comm, vlan )
  244.                 if mdict:
  245.                     connected_macs.append(mdict)
  246.             if len(connected_macs) > 0:
  247.                 for host in connected_macs:
  248.                     for key, val in list(host.items()):
  249.                         count += 1
  250.                         print("SwitchPort = %s\nSwitchPortSpeed = %s\nSwitchPortDuplex = %s\nSwitchVlan = %s" \
  251.                                % ( val["ifDescr"], val["ifSpeed"], val["ifDuplex"], val["vlan"] ))
  252.                         print("SnmpHostName = %s\nHostDescr = %s\nHostMAC  = %s\nHostIP = %s\nHostName = %s\n" \
  253.                                % ( val["sysName"], val["sysDescr"], val["nmac"], val["ipAddr"], val["hostName"] ))
  254.                 print("There are %d MAC Addresses connected to port %s" % ( count, pname ))
  255.             else:
  256.                 print("There are not any MAC Addresses connected to this %s port" % ( pname ))
  257.  
  258.         if report:
  259.             dswitch = device
  260.             count = write_report(dswitch, entIpList)
  261.             tcount = 0
  262.             if follow:
  263.                 for item in count:
  264.                     tcount += item
  265.                 print("Total MAC Addresses found: %d" % tcount)
  266.            
  267. def write_report( dev, entIpList, tcount = [] ):
  268.     print("Running Switch Report on %s" % ( dev ))
  269.     scanned_neighbor = False
  270.     ipList = []
  271.     snmperror, switchtype = get( dev, community, oTable["sysDescr"], 0 )
  272.     switchtype = str(switchtype)
  273.     if snmperror:
  274.         print(snmperror, "Either Wrong Community String or Firewall or SNMP Not Running")
  275.         sys.exit(1)
  276.     switch = followSwitch( dev, community )
  277.     switch.set_duplex()
  278.     switch.set_speed()
  279.     switch.set_port_name()
  280.     switch.set_phys_addr()
  281.     switch.set_oper_status()
  282.     switch.set_admin_status()
  283.     switch.set_alias()
  284.     count = 0
  285.     conn_output = open("connnected_ports_on_"+dev+".csv", "a")
  286.     if ( re.search("Cisco|PROCURVE", switchtype, re.IGNORECASE ) ):
  287.         lcomm, lvlan = switch.retreive_communities( )
  288.         if verbose:  print(ctime(), "Retreiving Community Strings\n %s" % ( lcomm ))
  289.         for i in range(len(lcomm)):
  290.             macout = switch.switch_report( lcomm[i], lvlan[i], conn_output )
  291.             count += macout
  292.             tcount.append(macout)
  293.         print("total MAC Addresses found on %s: %d\n" % ( dev, count ))
  294.     elif ( re.search("Nortel|ERS|Foundry", switchtype, re.IGNORECASE ) ):
  295.         macout = switch.switch_report( community, None, conn_output )
  296.         count += macout
  297.         tcount.append(macout)
  298.         print("total MAC Addresses found on %s: %d\n" % ( dev, count ))
  299.     conn_output.close()
  300.     if follow:
  301.         neighbors = switch.get_cdp_neighbor_ip_table()
  302.         if len(neighbors) > 0:
  303.             for neighbor in neighbors:
  304.                 if verbose: "Print neighbor %s " % neighbor
  305.                 for key, value in list(entIpList.items()):
  306.                     for line in value:
  307.                         if line == neighbor:
  308.                             scanned_neighbor = True
  309.                             if verbose:
  310.                                 print("this neighbor has already been scanned")
  311.                                 print(line, neighbor)
  312.                             break
  313.                     if scanned_neighbor:
  314.                         break
  315.                 if scanned_neighbor:
  316.                     continue
  317.                 else:
  318.                     entaddr = walk( neighbor, community, oTable["ipAdEntAddr"] )
  319.                     if entaddr == "requestTimedOut":
  320.                         continue
  321.                     for line in entaddr:
  322.                         ipList.append(switch.convertOctectIp( line[0][1] ))
  323.                     entIpList[neighbor] = ipList
  324.                     write_report(neighbor, entIpList, tcount)
  325.     else:
  326.         tcount = count
  327.     return tcount    
  328.  
  329. def verify_mac( nmac ):
  330.     """Verifies the MAC Addresses that was inputed by the user
  331.       And returns the newly converted nmac and the Valid code"""
  332.     valid = ""
  333.     if re.search("([0-9a-fA-F]{2}\:){5}[0-9a-fA-F]{2}", mac):
  334.         nmac = re.sub("\:", " ", mac)
  335.         valid = 1
  336.     elif re.search("([0-9a-fA-F]{2}\-){5}[0-9a-fA-F]{2}", mac):
  337.         nmac = re.sub("\-", " ", mac)
  338.         valid = 1
  339.     elif re.search("([0-9a-fA-F]{4}\.){2}[0-9a-fA-F]{4}", mac):
  340.         nmac = re.sub("\s{2}", " ", re.sub("^\s|\s$", "", re.sub("\'|\,|\[|\]", "", str(re.split("([0-9a-fA-F]{2})", re.sub("\.", "", mac) ) ) ) ) )
  341.         valid = 1
  342.     elif re.search("([0-9a-fA-F]{2}\s){5}[0-9a-fA-F]{2}", mac):
  343.         valid = 1
  344.         pass
  345.     else:
  346.         valid = 0
  347.     if verbose: print(ctime(), " Finished Checking for mac")
  348.     return( nmac, valid )
  349.  
  350.  
  351. def walk( dswitch, commVlan, oid  ):
  352.     """This function will return the table of OID's that I am walking"""
  353.     errorIndication, errorStatus, errorIndex, \
  354.         generic = cmdgen.CommandGenerator().nextCmd(cmdgen.CommunityData('test-agent', commVlan), \
  355.         cmdgen.UdpTransportTarget((dswitch, 161)), oid)
  356.     if errorIndication:
  357.         return errorIndication
  358.     return generic
  359.  
  360.  
  361. def get( device, commVlan, oid, rval, indexOid="None" ):
  362.     """This is essentially my generic snmpget, but with options. Since if I am doing an
  363.       snmpget, I will usually either pass a index ID or a list of ID's, This function makes
  364.       my life easier, by not creating multiple getCmd's"""
  365.  
  366.     if not isinstance(rval, int):
  367.         rval = 0
  368.     oidN = list(oid)
  369.     if isinstance(indexOid, int):
  370.         oidN.append(indexOid)
  371.     elif type(indexOid) == list:
  372.         oidN = oidN + list(map(int, indexOid))
  373.     oidN = tuple(oidN)
  374.     errorIndication, errorStatus, errorIndex, \
  375.         generic = cmdgen.CommandGenerator().getCmd(cmdgen.CommunityData('test-agent', commVlan), \
  376.         cmdgen.UdpTransportTarget((device, 161)), oidN)
  377.     if errorIndication:
  378.         return (errorIndication, generic )
  379.     if rval == 0:
  380.         return (errorIndication, generic )
  381.     elif rval == 1:
  382.         return (errorIndication, generic[0][0] )
  383.     elif rval == 2:
  384.         return (errorIndication, str(generic[0][1]) )
  385.  
  386.  
  387.  
  388. def hex2dec(mack):
  389.     return int(mack, 16)
  390.  
  391. def dec2hex(mack):
  392.     return re.sub( "^0x", "", hex(mack) )
  393.  
  394. def alias_name( switch ):
  395.     aliasW = walk( switch, community, oTable['ifAlias'] )
  396.     aliasH = {}
  397.     for i in aliasW:
  398.         aliasH[i[0][0][-1]] = str(i[0][1])
  399.     return aliasH
  400.  
  401. def admin_settings( switch ):
  402.     adminW = walk( switch , community, oTable['ifAdminStatus'] )
  403.     adminH = {}
  404.     for i in adminW:
  405.         adminH[i[0][0][-1]] = str(ostatus[i[0][1]])
  406.     return adminH
  407.  
  408. def oper_settings( switch ):
  409.     operW = walk( switch, community, oTable['ifOperStatus'] )
  410.     operH = {}
  411.     for i in operW:
  412.         operH[i[0][0][-1]] = str(ostatus[i[0][1]])
  413.     return operH
  414.  
  415. def duplex_settings( switch ):
  416.     duplexW = walk( switch, community, oTable['dot3StatsDuplexStatus'] )
  417.     duplexH = {}
  418.     for i in duplexW:
  419.         duplexH[i[0][0][-1]] = str(duplex[i[0][1]])
  420.     return duplexH
  421.  
  422. def speed_settings( switch ):
  423.     speedW = walk( switch, community, oTable['ifSpeed'] )
  424.     speedH = {}
  425.     for i in speedW:
  426.         speedH[i[0][0][-1]] = str(port_speed(int(i[0][1]) ) )
  427.     return speedH
  428.  
  429. def port_name( switch ):
  430.     portW = walk( switch, community, oTable['ifDescr'] )
  431.     portH = {}
  432.     for i in portW:
  433.         portH[i[0][0][-1]] = str(i[0][1])
  434.     return portH
  435.  
  436. def port_speed( speed ):
  437.     speed = speed / 1000000
  438.     speed = str(speed)+"mb"
  439.     return speed
  440.  
  441. class followSwitch(object):
  442.     def __init__(self, switch, comm="public" ):
  443.         self.community = comm
  444.         self.switch = switch
  445.         self.sbrand = None
  446.         self.snmperror, self.switchtype = get( self.switch, self.community, oTable["sysDescr"], 2)
  447.         if ( re.search("Cisco", self.switchtype, re.IGNORECASE ) ):
  448.             self.sbrand = "Cisco"
  449.         elif ( re.search("PROCURVE", self.switchtype, re.IGNORECASE ) ):
  450.             self.sbrand = "HP"
  451.         elif ( re.search("Nortel|ERS", self.switchtype, re.IGNORECASE ) ):
  452.             self.sbrand = "Nortel"
  453.         elif ( re.search("Foundry", self.switchtype, re.IGNORECASE ) ):
  454.             self.sbrand = "Foundry"
  455.  
  456.     def get_sbrand( self ):
  457.         return self.sbrand
  458.  
  459.     def set_duplex( self ):
  460.         try:
  461.             self.duplexH = duplex_settings( self.switch )
  462.         except:
  463.             self.duplexH = None
  464.  
  465.     def set_speed( self ):
  466.         try:
  467.             self.speedH = speed_settings( self.switch )
  468.         except:
  469.             self.speedH = None
  470.  
  471.     def set_port_name( self ):
  472.         self.portNameH = port_name( self.switch )
  473.  
  474.     def set_oper_status( self ):
  475.         self.operH =  oper_settings( self.switch )
  476.  
  477.     def set_admin_status( self ):
  478.         self.adminH = admin_settings( self.switch )
  479.  
  480.     def set_alias( self ):
  481.         self.aliasH = alias_name( self.switch )
  482.  
  483.     def set_phys_addr( self ):
  484.         self.PhysAddr = walk( self.switch, community, oTable["atPhysAddress"] )
  485.  
  486.     def set_ifIndex_dict( self ):
  487.         self.portNameH = port_name( self.switch )
  488.         self.operH =  oper_settings( self.switch )
  489.         self.adminH = admin_settings( self.switch )
  490.         self.aliasH = alias_name( self.switch )
  491.         self.PhysAddr = walk( self.switch, community, oTable["atPhysAddress"] )
  492.         try:
  493.             self.duplexH = duplex_settings( self.switch )
  494.         except:
  495.             self.duplexH = None
  496.         try:
  497.             self.speedH = speed_settings( self.switch )
  498.         except:
  499.             self.speedH = None
  500.  
  501.  
  502.     def findMacByIp( self, nip ):
  503.         """This Function will return the MAC Address if the IPAddress that  was in the ARP table
  504.           if not it will return None"""
  505.         count = 0
  506.         nmac = ""
  507.         for ipAddress in self.PhysAddr:
  508.             ip1 = str(ipAddress[0][0][-4:])
  509.             ipmap = re.sub("\'|\[|\]|\(|\)", "", re.sub(",\s", ".", ip1))
  510.             if ( nip == ipmap ):
  511.                 count += 1
  512.                 nmac = self.convertOctectMac(ipAddress[0][1])
  513.                 break
  514.         if count == 1:
  515.             return( nmac )
  516.         else:
  517.             nmac = None
  518.             return( nmac )
  519.  
  520.     def findIpByMac( self, nmac  ):
  521.         """This Function will only return the IP Address of the MAC you are searching for if
  522.           the IP Address is in the ARP table.  """
  523.         self.ipAddr = None
  524.         for mack in self.PhysAddr:
  525.             cmac = self.convertOctectMac(mack[0][1])
  526.             if re.search(nmac, cmac, re.IGNORECASE):
  527.                 ip1 = str(mack[0][0][-4:])
  528.                 self.ipAddr = re.sub("\'|\(|\)|,", "", ip1).replace(" ", ".")
  529.                 break
  530.         return self.ipAddr
  531.  
  532.     def convertOctectMac(self, mack):
  533.         """This Function will convert the OctectString into a Valid MAC Address"""
  534.         mmap = list(map(hex, list(map(ord, mack)) ))
  535.         cmac = mmap
  536.         for i in range(len(mmap)):
  537.             mmap[i] = re.sub("0x", "", mmap[i])
  538.             mmap[i] = mmap[i].zfill(2)
  539.         cmac = re.sub("\'|\,|\[|\]", "", str(mmap) )
  540.         return cmac
  541.  
  542.     def convertOctectIp(self, hexip):
  543.         """This Function will convert the OctectString into a valid Ip Address"""
  544.         ip = list(map(hex, list(map(ord, hexip)) ))
  545.         ip = list(map(hex2dec, ip))
  546.         ip = re.sub("\,", ".",re.sub("\'|\[|\]|\s","", str(ip)))
  547.         return ip
  548.  
  549.     def convertDecMac(self, mack):
  550.         """This Function will convert the Decimal into HEX"""
  551.         mmap = list(map(hex, mack))
  552.         cmac = mmap
  553.         for i in range(len(mmap)):
  554.             mmap[i] = re.sub("0x", "", mmap[i])
  555.             mmap[i] = mmap[i].zfill(2)
  556.         cmac = re.sub("\'|\,|\[|\]", "", str(mmap) )
  557.         return cmac
  558.  
  559.     def get_pagp_ports( self, pagp_ifIndex ):
  560.         """get_pagp_ports will return a list of ifIndex ID's, that is associated with
  561.        the EtherChannel ifIndex. Will return a tuple of ifIndex ID's"""
  562.         pagp_group = walk( self.switch, self.community, pagpTable["pagpGroupIfIndex"] )
  563.         ifIndexList = []
  564.         for line in pagp_group:
  565.             if pagp_ifIndex == int(line[0][1]):
  566.                 ifIndexList.append(int(line[0][0][-1]))
  567.         return tuple(ifIndexList)
  568.            
  569.  
  570.     def retreive_communities( self ):
  571.         """ This function does exactly what it is defined as. It will return a list
  572.            of Community Strings from the entLogicalCommunity OID Table.
  573.            As well as grab the associated vlan ID. Then return both in a tuple"""
  574.         commTable = walk( self.switch, self.community, oTable["entLogicalCommunity"] )
  575.         if verbose: print(commTable)
  576.         lcomm = []
  577.         lvlan = []
  578.         for comm in commTable:
  579.             if verbose: print(ctime(), " Looping Through CommTable")
  580.             vlan = int(comm[0][0][-1])
  581.             comm = str(comm[0][1])
  582.             if len(lcomm) == 0:
  583.                 lcomm.append(comm)
  584.                 lvlan.append(vlan)
  585.             elif len(lcomm) >= 1:
  586.                 if lcomm[:len(comm[-1])].__contains__(comm):
  587.                     if verbose: print("Duplicate Community String")
  588.                     continue
  589.                 else:
  590.                     lcomm.append(comm)
  591.                     lvlan.append(vlan)
  592.         return( lcomm, lvlan )
  593.  
  594.     def find_port_match( self, pname ):
  595.         """ By passing this function the Port Name and The Brand of
  596.            this Switch, you will in return get the ifIndex and the ifName"""
  597.         if verbose: print(ctime(), " In generic_pname Function")
  598.         self.ifIndex = None
  599.         self.ifName = None
  600.         count = 0
  601.         ifNameTable = walk( self.switch, community, oTable["ifName"] )
  602.         pfield = re.compile("([0-9]{1,2})\\/([0-9]{1,2})")  # "1/9"
  603.         gen_pname = []
  604.         new_pname = ""
  605.         if ( pfield.search(pname) ):
  606.             gen_pname.append(pfield.match(pname).group(1))
  607.             gen_pname.append(pfield.match(pname).group(2))
  608.             if ( self.sbrand == "HP" ):
  609.                 new_pname = hpTable[str(gen_pname[0])]+str(gen_pname[1])
  610.                 count += 1
  611.                 if verbose: print(new_pname)
  612.             if ( self.sbrand == "Nortel" ):
  613.                 new_pname = "Slot: %s Port: %s" % ( str(gen_pname[0]), str(gen_pname[1]) )
  614.                 count += 1
  615.                 if verbose: print(new_pname)
  616.             if (self. sbrand == "Foundry" ):
  617.                 new_pname = "FastEthernet%s" % ( str(gen_pname[1]) )
  618.                 count += 1
  619.                 if verbose: print(new_pname)
  620.  
  621.         for iface in ifNameTable:
  622.             ifIndex = int(iface[0][0][-1])
  623.             ifName = str(iface[0][1])
  624.             if new_pname:
  625.                 if ( re.search(new_pname, ifName) ):
  626.                     count +=1
  627.                     self.ifIndex = ifIndex
  628.                     self.ifName = ifName
  629.                     if verbose: print("Found %s on %s and the ifIndex is %d" % ( new_pname, self.ifName, self.ifIndex ))
  630.                     break
  631.             if ( re.search("[A-Z]+"+pname+"|"+pname, ifName) ):
  632.                 count +=1
  633.                 self.ifIndex = ifIndex
  634.                 self.ifName = ifName
  635.                 if verbose: print("Found %s on %s and the ifIndex is %d" % ( pname, ifName, ifIndex ))
  636.                 break
  637.    
  638.     def get_ifIndex(self):
  639.         return( self.ifIndex )
  640.  
  641.     def get_ifName(self):
  642.         return( self.ifName )
  643.  
  644.     def find_mac(self, nmac, nip=None):
  645.         self.mac = nmac
  646.         snmperror, switchtype = get( self.switch, self.community, oTable["sysDescr"], 2)
  647.         mTable = ifIndex = None
  648.         if ( re.search("Cisco|HP", self.sbrand, re.IGNORECASE) ):
  649.             lcomm, lvlan = self.retreive_communities( )
  650.             if verbose:  print(ctime(), "Retreiving Community Strings\n %s" % ( lcomm ))
  651.             for i in range(len(lcomm)):
  652.                 mTable, ifIndex = self.find_mac_or_ip( self.mac, nip, lcomm[i], lvlan[i] )
  653.                 if len(mTable) >= 1:
  654.                     break
  655.         elif ( re.search("Nortel|ERS|Foundry", self.sbrand, re.IGNORECASE) ):
  656.                 mTable, ifIndex = self.find_mac_or_ip( self.mac, nip, self.community, None )
  657.         return mTable, ifIndex
  658.  
  659.     def find_mac_or_ip( self, nmac, nip, comm, vlanID ):
  660.         if verbose: print(ctime(), " In generic_mac_or_ip Function")
  661.         if verbose: print(nmac, nip, self.switch, comm)
  662.         macVlanTable = walk( self.switch, comm, oTable["dot1dTpFdbPort"] )
  663.         if verbose: print(macVlanTable)
  664.         mTable = {}
  665.         count = 0
  666.         ifIndex = None
  667.         if ( len(macVlanTable) > 0 ):
  668.             if verbose: print(ctime(), " First If Statement ")
  669.             for macVlan in macVlanTable:
  670.                 cmac = self.convertDecMac(list(macVlan[0][0][-6:]))
  671.                 if nmac == cmac:
  672.                     if verbose: print("MAC Addresses Match %s and %s" % ( nmac, cmac ))
  673.                     count += 1
  674.                     bIndex = int(macVlan[0][1])
  675.                     snmperror, ifIndex = get( self.switch, comm, oTable["dot1dBasePortIfIndex"], 2, bIndex )
  676.                     if snmperror:
  677.                         print snmperror
  678.                         sys.exit(1)
  679.                     ifIndex = int(ifIndex)
  680.                     ifSpeed = self.speedH[ifIndex]
  681.                     try:
  682.                         ifDuplex = self.duplexH[ifIndex]
  683.                     except:
  684.                         ifDuplex = None
  685.                     port = self.portNameH[ifIndex]
  686.                     vlan = ""
  687.                     hname = None
  688.                     if vlanID:
  689.                         try:
  690.                             snmperror, vlan = get( self.switch, comm, oTable["entLogicalDescr"], 2, vlanID )
  691.                         except:
  692.                             vlan = None
  693.                     if not nip:
  694.                         ipAddr = str(self.findIpByMac( nmac ))
  695.                     else:
  696.                         ipAddr = nip
  697.                     if re.search("(\d{1,3}\.){3}\d{1,3}", str(ipAddr) ):
  698.                         try:
  699.                             hname = gethostbyaddr(ipAddr)
  700.                             hname = hname[0]
  701.                         except:
  702.                             hname = "Pointer Record Not set for %s" % ( ipAddr )
  703.                         snmperror, sysName = get( ipAddr, community, oTable["sysName"], 2 )
  704.                         if snmperror: sysName = snmperror
  705.                         snmperror, sysDescr = get( ipAddr, community, oTable["sysDescr"], 2 )
  706.                         if snmperror: sysDescr = snmperror
  707.                         mTable[nmac] = {
  708.                                         "ifDescr" : port,
  709.                                         "ifSpeed" : ifSpeed,
  710.                                         "ifDuplex" : ifDuplex,
  711.                                         "vlan" : vlan,
  712.                                         "sysName" : sysName,
  713.                                         "sysDescr" : sysDescr,
  714.                                         "nmac" : nmac,
  715.                                         "ipAddr" : ipAddr,
  716.                                         "hostName" : hname
  717.                                        }
  718.                         count += 1
  719.                     else:
  720.                         sysName = "None"
  721.                         sysDescr = "None"
  722.                         mTable[nmac] = {
  723.                                         "ifDescr" : port,
  724.                                         "ifSpeed" : ifSpeed,
  725.                                         "ifDuplex" : ifDuplex,
  726.                                         "vlan" : vlan,
  727.                                         "sysName" : "No SNMP Access",
  728.                                         "sysDescr" : "No SNMP Access",
  729.                                         "nmac" : nmac,
  730.                                         "ipAddr" : ipAddr,
  731.                                         "hostName" : hname
  732.                                        }
  733.                     count += 1
  734.                     if verbose: print(ctime(), " Done ")
  735.                     break
  736.  
  737.         return mTable, ifIndex
  738.            
  739.  
  740.     def return_mac_by_ifIndex( self, comm, vlanID ):
  741.         """ This function will return a list of dictionairies by the port Index
  742.            So lets say you pass this function ifIndex 10, return_mac_by_ifIndex
  743.             will return any MAC Addresses Associated with that ifIndex."""
  744.  
  745.         mTable = {}
  746.         sTable = {}
  747.         switch = []
  748.         mcount = 0
  749.         vIfIndexTable = walk( self.switch, comm, oTable["dot1dBasePortIfIndex"] )
  750.         if ( len(vIfIndexTable) > 0 ):
  751.             for v in vIfIndexTable:
  752.                 vIndex = int(v[0][1])
  753.                 bIndex = int(v[0][0][-1])
  754.                 if verbose: print("Now Trying to Match the %d ifIndex to the bridge %d ifIndex table" % ( self.ifIndex, vIndex ))
  755.                 if ( self.ifIndex == vIndex ):
  756.                     if verbose: print("Match %d ifIndex to %d BridgeifIndex Table" % ( self.ifIndex, vIndex ))
  757.                     dM = walk( self.switch, comm, oTable["dot1dTpFdbPort"] )
  758.                     for d in dM:
  759.                         decmac = int(d[0][1])
  760.                         if verbose: print("Now Trying to Match the %d bIndex to the decimal bridge %d Index table" % ( bIndex, decmac ))
  761.                         if vlanID:
  762.                             snmperror, vlan = get( self.switch, comm, oTable["entLogicalDescr"], 2, vlanID )
  763.                         else:
  764.                             vlan = None
  765.                         if ( bIndex == decmac ):
  766.                             mcount += 1
  767.                             indexListOid = list(d[0][0][-6:])
  768.                             nmac =  self.convertDecMac( indexListOid )
  769.                             if verbose: print("Found %s Decimal Mac and Coverted into a HEX MAC %s " % ( indexListOid, nmac ))
  770.                             ipAddr = str( self.findIpByMac( nmac ) )
  771.                             hname = None
  772.                             try:
  773.                                 ifSpeed = self.speedH[self.ifIndex]
  774.                             except:
  775.                                 ifSpeed = None
  776.                             try:
  777.                                 ifDuplex = self.duplexH[self.ifIndex]
  778.                             except:
  779.                                 ifDuplex = None
  780.                             port = self.portNameH[self.ifIndex]
  781.                             if re.search("(\d{1,3}\.){3}\d{1,3}", ipAddr ):
  782.                                 if re.search("127.0.0.\d+", ipAddr ):
  783.                                     pass
  784.                                 else:
  785.                                     try:
  786.                                         hname = gethostbyaddr(ipAddr)
  787.                                         hname = hname[0]
  788.                                     except:
  789.                                         hname = "Pointer Record Not set for %s" % ( ipAddr )
  790.                                     snmperror, sysName =  get( ipAddr, community, oTable["sysName"], 2 )
  791.                                     if snmperror: sysName = snmperror
  792.                                     snmperror, sysDescr =  get( ipAddr, community, oTable["sysDescr"], 2 )
  793.                                     sysName = str(sysName)
  794.                                     if snmperror: sysDescr = snmperror
  795.                                     sysDescr = str(sysDescr)
  796.                                     snmperror, sysModel =  get( ipAddr, community, oTable["entPhysicalModelName"], 2 )
  797.                                     if re.search("Cisco|PROCURVE|Nortel|Foundry", sysDescr, re.IGNORECASE):
  798.                                         switch.append(ipAddr)
  799.                                         switch.append(sysName)
  800.                                         switch.append(sysDescr)
  801.                                         switch.append(sysModel)
  802.                                         switch.append(vlan)
  803.                                         switch.append(self.speedH[self.ifIndex])
  804.                                         switch.append(self.portNameH[self.ifIndex])
  805.                                     else:
  806.                                         mTable[nmac] = {
  807.                                                         "ifDescr" : port,
  808.                                                         "ifSpeed" : ifSpeed,
  809.                                                         "ifDuplex" : ifDuplex,
  810.                                                         "vlan" : vlan,
  811.                                                         "sysName" : sysName,
  812.                                                         "sysDescr" : sysDescr,
  813.                                                         "nmac" : nmac,
  814.                                                         "ipAddr" : ipAddr,
  815.                                                         "hostName" : hname
  816.                                                        }
  817.                             else:
  818.                                 mTable[nmac] = {
  819.                                                 "ifDescr" : port,
  820.                                                 "ifSpeed" : ifSpeed,
  821.                                                 "ifDuplex" : ifDuplex,
  822.                                                 "vlan" : vlan,
  823.                                                 "sysName" : "No SNMP Access",
  824.                                                 "sysDescr" : "No SNMP Access",
  825.                                                 "nmac" : nmac,
  826.                                                 "ipAddr" : ipAddr,
  827.                                                 "hostName" : hname
  828.                                                }
  829.         return mTable
  830.  
  831.     def switch_report( self, comm, vlanID, conn_output ):
  832.         """switch_report will write to stdout and to a .csv file
  833.           everytime it finds a mac attached to a specific Interface"""
  834.         vlan = ""
  835.         output = []
  836.         count = 0
  837.         if vlanID:
  838.             snmperror, vlan = get( self.switch, comm, oTable["entLogicalDescr"], 2, vlanID )
  839.         else:
  840.             vlan = None
  841.         bridgeTable = walk( self.switch, comm, oTable['dot1dBasePort'] )
  842.         decMacTable = walk( self.switch, comm, oTable['dot1dTpFdbPort'] )
  843.         if verbose: print(decMacTable)
  844.         if ( len(decMacTable) > 0 ):
  845.             for bindex in bridgeTable:
  846.                 bIndex = int(bindex[0][1])
  847.                 snmperror, ifIndex = get( self.switch, comm, oTable['dot1dBasePortIfIndex'], 2, int(bIndex) )
  848.                 ifIndex = int(ifIndex)
  849.                 for dm in decMacTable:
  850.                     if dm[0][1] == bIndex:
  851.                         ifDuplex = ""
  852.                         ifSpeed = self.speedH[ifIndex]
  853.                         try:
  854.                             ifDuplex = self.duplexH[ifIndex]
  855.                         except:
  856.                             ifDuplex = "Not Known"
  857.                         port = self.portNameH[ifIndex]
  858.                         ifOperStatus = self.operH[ifIndex]
  859.                         ifAdminStatus = self.adminH[ifIndex]
  860.                         alias = self.aliasH[ifIndex]
  861.                         decMac = list(dm[0][0][-6:])
  862.                         nmac = self.convertDecMac( decMac )
  863.                         ipAddr = str( self.findIpByMac( nmac ) )
  864.                         hname = None
  865.                         if re.search("(\d{1,3}\.){3}\d{1,3}", ipAddr ):
  866.                             if re.search("127.0.0.\d+", ipAddr ):
  867.                                 pass
  868.                             else:
  869.                                 try:
  870.                                     hname = gethostbyaddr(ipAddr)
  871.                                     hname = hname[0]
  872.                                 except:
  873.                                     hname = "Pointer Record Not set for %s" % ( ipAddr )
  874.                         output = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" % ( port,nmac,ipAddr,hname,vlan,ifAdminStatus,ifOperStatus,ifDuplex,ifSpeed,alias )
  875.                         count += 1
  876.                         print(output)
  877.                         conn_output.write(output)
  878.                         conn_output.flush()
  879.                     else:
  880.                         continue
  881.         return count
  882.  
  883.     def get_cdp_neighbor_ip(self, ifIndex):
  884.         """ This function will grab the cdp neigbor that is attached
  885.        to the ifIndex that is passed to this function
  886.        """
  887.         self.ifIndex = ifIndex
  888.         ctable = walk( self.switch, self.community, cdpTable["cdpCacheAddress"])
  889.         count = 0
  890.         self.ip = None
  891.         for host in ctable:
  892.             match = tuple(host[0])
  893.             if verbose:
  894.                 print(self.ifIndex, match[0][-2], self.convertOctectIp(str(host[0][1])))
  895.             if ( self.ifIndex == match[0][-2] ):
  896.                 if verbose:
  897.                     print("found ifIndex %s and here is the ip address of the cdpneighbor %s" \
  898.                            % (str(self.ifIndex), self.convertOctectIp(str(host[0][1]))))
  899.                 self.ip = self.convertOctectIp(str(host[0][1]))
  900.         return self.ip
  901.  
  902.     def get_cdp_neighbor_ip_table(self):
  903.         """ This function will grab the cdp neigbor table and return a list of neighbors"""
  904.         ctable = walk( self.switch, self.community, cdpTable["cdpCacheAddress"])
  905.         self.ipTable = []
  906.         for host in ctable:
  907.             self.ipTable.append(self.convertOctectIp(str(host[0][1])))
  908.             if verbose:
  909.                 print(self.convertOctectIp(str(host[0][1])))
  910.         self.ipTable = tuple(self.ipTable)
  911.         return self.ipTable
  912.  
  913.  
  914.     def get_mac_from_cdp_neighbor( self, cswitch, nmac, nip ):
  915.         self.cswitch = cswitch
  916.         self.nmac = nmac
  917.         self.nip = nip
  918.         snmperror, switchtype = get( self.cswitch, self.community, oTable["sysDescr"], 2)
  919.         if snmperror:
  920.             print("Wrong Community String %s for device %s" % ( self.community, self.cswitch ))
  921.             sys.exit(1)
  922.         get_nmac = followSwitch( self.cswitch, self.community )
  923.         get_nmac.set_duplex()
  924.         get_nmac.set_speed()
  925.         get_nmac.set_port_name()
  926.         if self.nip == None:
  927.             get_nmac.set_phys_addr()
  928.         cTable, self.new_ifIndex = get_nmac.find_mac( self.nmac, self.nip )
  929.         count = 0
  930.         if cTable:
  931.             count += 1
  932.             print("Found %s on %s\n" % ( self.nmac, self.cswitch ))
  933.             for key, val in list(cTable.items()):
  934.                 print("Switch Connected to %s" % ( self.cswitch ))
  935.                 print("SwitchPort = %s\nSwitchPortSpeed = %s\nSwitchPortDuplex = %s\nSwitchVlan = %s" % \
  936.                        ( val["ifDescr"], val["ifSpeed"], val["ifDuplex"], val["vlan"] ))
  937.                 print("SnmpHostName = %s\nSnmpHostDescr = %s\nHostMAC  = %s\nHostIP = %s\nHostName = %s\n" % \
  938.                        ( val["sysName"], val["sysDescr"], val["nmac"], val["ipAddr"], val["hostName"] ))
  939.                 if re.search("Port-channel", val["ifDescr"]):
  940.                     if verbose: print("ifIndex %d is a %s interface" % ( self.new_ifIndex, val["ifDescr"] ))
  941.                     ifIndex_pagp_list = get_nmac.get_pagp_ports( self.new_ifIndex )
  942.                     if verbose: print("List of Interfaces is in EtherChannel: ", ifIndex_pagp_list)
  943.                     self.new_ifIndex = ifIndex_pagp_list[0]
  944.         return (self.new_ifIndex, count)
  945.          
  946.  
  947.    
  948.  
  949. cdpTable = {
  950.             "cdpCacheAddress" : (1,3,6,1,4,1,9,9,23,1,2,1,1,4)
  951.            }
  952.  
  953. pagpTable = {
  954.             "pagpGroupIfIndex" : (1,3,6,1,4,1,9,9,98,1,1,1,1,8)
  955.             }
  956. oTable = {
  957.            "entLogicalCommunity" : (1,3,6,1,2,1,47,1,2,1,1,4),
  958.            "entPhysicalModelName" : (1,3,6,1,2,1,47,1,1,1,1,13,1),
  959.            "entLogicalDescr" : (1,3,6,1,2,1,47,1,2,1,1,2),
  960.            "dot1dBasePort" : (1,3,6,1,2,1,17,1,4,1,1),
  961.            "dot1dTpFdbPort" : (1,3,6,1,2,1,17,4,3,1,2),
  962.            "dot1dBasePortIfIndex" : (1,3,6,1,2,1,17,1,4,1,2),
  963.            "dot1dTpFdbAddress" :  (1,3,6,1,2,1,17,4,3,1,1),
  964.            "ifDescr" : (1,3,6,1,2,1,2,2,1,2),
  965.            "ifName" : (1,3,6,1,2,1,31,1,1,1,1),
  966.            "ifSpeed" : (1,3,6,1,2,1,2,2,1,5),
  967.            "ifAlias" : (1,3,6,1,2,1,31,1,1,1,18),
  968.            "sysName" : (1,3,6,1,2,1,1,5,0),
  969.            "sysDescr" : (1,3,6,1,2,1,1,1,0),
  970.            "dot3StatsDuplexStatus" : (1,3,6,1,2,1,10,7,2,1,19),
  971.            "ifAdminStatus" : (1,3,6,1,2,1,2,2,1,7),
  972.            "ifOperStatus" : (1,3,6,1,2,1,2,2,1,8),
  973.            "atPhysAddress" : (1,3,6,1,2,1,3,1,1,2),
  974.            "ipAdEntAddr" : (1,3,6,1,2,1,4,20,1,1),
  975.            "ipAdEntIfIndex" : (1,3,6,1,2,1,4,20,1,2)
  976.          }
  977.  
  978. hpTable = {"1" : "A", "2" : "B", "3" : "C", "4" : "D", "5" : "E",
  979.            "6" : "F", "7" : "G", "8" : "H", "9" : "J", "10" : "K"
  980.       }
  981.  
  982. duplex = {
  983.           1 : "unknown",
  984.           2 : "halfDuplex",
  985.           3 : "fullDuplex",
  986.           '': "NotSet"
  987.          }
  988.  
  989.  
  990. ostatus = {
  991.            1 : "up",
  992.            2 : "down",
  993.            3 : "testing",
  994.            4 : "unknown",
  995.            5 : "dormant",
  996.            6 : "notPresent",
  997.            7 : "lowerLayerDown"
  998.           }
  999.  
  1000. try:
  1001.      opts, args = getopt.getopt(sys.argv[1:], "c:d:i:m:n:h:rfv",
  1002.      [ 'community=', 'device=', "mac=", 'ip=', 'pname=', 'report', 'verbose', 'follow', 'help' ]
  1003.      )
  1004. except getopt.error:
  1005.      usage()
  1006.  
  1007. help = community = device = mac = ip = pname = verbose = report = follow = None
  1008.  
  1009. for opt, val in opts:
  1010.     if opt in ('-c', '--community'):
  1011.         community = val
  1012.     if opt in ('-d', '--device'):
  1013.         device = val
  1014.     if opt in ('-m', '--mac'):
  1015.         mac = val
  1016.     if opt in ('-i', '--ip'):
  1017.         ip = val
  1018.     if opt in ('-n', '--pname'):
  1019.         pname = val
  1020.     if opt in ('-h', '--help'):
  1021.         help = usage()
  1022.     if opt in ('-r', '--report'):
  1023.         report = True
  1024.     if opt in ('-f', '--follow'):
  1025.         follow = True
  1026.     if opt in ('-v', '--verbose'):
  1027.         verbose = True
  1028.  
  1029.    
  1030.  
  1031. if __name__ == '__main__':
  1032.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement