четверг, 22 октября 2015 г.

Скрипт в помощь поиска неуправляемых коммутаторов/hub на Python (seach unmanaged switch/hub connected to Cisco with python script)


#!/usr/bin/python
# version 0.1 Created Yevgenii Merezhko
# My assumptions:
# There are switches with many vlans. I select only "users switches"
# There are Cisco ip phones in voice vlan.
# Usually Po1 is uplink
# PC connected via IP Phones and somecase we have some hub/unmanaged switches after Cisco IP Phones
# Ip Phones have two mac address (one in voice vlan and second in data vlan)
#just for entering VoiceVlan number
VoiceVlan = str(input("Enter Voice Vlan number: "))
#put file in folder in put name of file
#may be better to work with all files in folder TBD
with open("/home/yevgenii/Downloads/test/test2/192.168.73.252.1.1") as textFile:
    lines = [line.split() for line in textFile]
# I expect some thing like 
# 73    0004.23e7.d793    DYNAMIC     Fa0/22
# 73    0005.1e35.de56    DYNAMIC     Fa0/15
# 
# for example  it could be extracted from "sh mac add" 
# My colleague collect it for me with bash+clogin (module from Rancid)
# and separate file with like this bash script
# #!/bin/sh
# for i in $(ls 1*);
# do 
# cat $i| grep "^\ "| grep -v "Po1" > $i.1
# done
row = 0
MacAddressPhones = []
ListWithoutPhones = []
macaddress = 0
macaddress1 = 0
macaddress2 = 0
mac1phones = 0
mac2phonesswitch = 0
Vlan = 0
x = 0
y = 0
B = 0
port = []
PortNumber = 0
result = {}
#create list with mac in voice vlan
while row < len(lines):
    Vlan = lines[row][0]
    macaddress = lines[row][1]
    if Vlan == VoiceVlan:
        MacAddressPhones.append(macaddress)
        del lines[row][:]
    row += 1
#for easy loop filtering 
ListWithoutPhones = lines
#remove empty from list
lines = filter(None, lines)
#compare Mac in voice vlan with mac in data vlan)
for mac1phones in range(len(MacAddressPhones)):
    macaddress1 = MacAddressPhones[mac1phones]
    mac2phonesswitch = 0
    while mac2phonesswitch < len(lines):
        macaddress2 = lines[mac2phonesswitch][1]
        if macaddress1 == macaddress2:
            del ListWithoutPhones[mac2phonesswitch][:]
        lines = filter(None, lines)
        ListWithoutPhones = filter(None,ListWithoutPhones)
        mac2phonesswitch += 1
    mac1phones += 1
#create only list of port
while x < len(ListWithoutPhones):
    port.append(ListWithoutPhones[x][3])
    x += 1
print("port",port)
#calucate how many times port in list if more then 1, put in interesting list
while y     PortNumber = port[y]
    print("PortNumber",PortNumber)
    B = port.count(PortNumber)
    print("B",B)
    if B >1:
        result[PortNumber]=B
        print("result len:",len(result))
    y += 1
print ("List of Ports: ",result)
# after this it need to be filter uplink port in most case it will be easy

Комментариев нет: