def get_mac_address_table(switch_ip, login, password, stdout=False): t = pexpect.spawn('telnet {}'.format(switch_ip), encoding='utf-8') if stdout: t.logfile = sys.stdout try: t.expect(['[Uu]ser [Nn]ame:', 'login:'******'[Pp]ass[Ww]ord:') t.sendline(password) t.expect('#', timeout=5) t.sendline(u'terminal datadump') t.expect('#') t.sendline(u'show mac address-table dynamic') t.expect('#', timeout=600) mac_table = t.before t.sendline(u'exit') mac_dict = {} for entry in mac_table.split('\n'): match = re.search('\s+\d+\s+(?P<mac_addr>[:a-f0-9]{17})\s+(?P<port>\S+)\s+dynamic', entry, re.IGNORECASE) if match: if match.group('port') == 'CPU': continue if not match.group('port') in mac_dict: mac_dict[match.group('port')] = [] mac_addr = mac.normalize_mac(match.group('mac_addr')) mac_dict[match.group('port')].append(mac_addr) except pexpect.TIMEOUT: return False return mac_dict
def get_mac_address_table(switch_ip, login, password, stdout=False): t = pexpect.spawn('telnet {}'.format(switch_ip), encoding='utf-8') if stdout: t.logfile = sys.stdout try: t.expect_exact('Login: '******'+ct') t.expect_exact('Password: '******'] > ', timeout=5) t.sendline(u"/interface bridge host print without-paging terse\r\n") t.expect('] >', timeout=600) mac_table = t.before t.sendline(u'quit') mac_dict = {} for entry in mac_table.split('\n'): match = re.search( '^\s+mac-address=(?P<mac_addr>\S+)\s+on-interface=(?P<port>\S+)\s+', entry, re.IGNORECASE) if match: if not match.group('port') in mac_dict: mac_dict[match.group('port')] = [] mac_addr = mac.normalize_mac(match.group('mac_addr')) mac_dict[match.group('port')].append(mac_addr) except pexpect.TIMEOUT: return False return mac_dict
def get_mac_address_table(switch_ip, login, password, stdout=False): t = pexpect.spawn('telnet {}'.format(switch_ip), encoding='utf-8') if stdout: t.logfile = sys.stdout try: t.expect(u'[Uu]ser[Nn]ame:') t.sendline(login) t.expect(u'[Pp]ass[Ww]ord:') t.sendline(password) ret = t.expect([u'#', u'>'], timeout=5) if ret == 1: t.sendline(u"enable") t.expect(u"#", timeout=5) t.sendline(u'terminal length 0') t.expect(u'#') t.sendline(u'show mac address-table dynamic') t.expect(u'#', timeout=600) mac_table = t.before t.sendline(u'exit') t.expect(u'>') t.sendline(u'exit') mac_dict = {} for entry in mac_table.split('\n'): match = re.search( '\d+\s+(?P<mac_addr>\S+)\s+DYNAMIC\s+(?P<port>\S+)', entry) if match: if match.group('port') == 'CPU': continue if not match.group('port') in mac_dict: mac_dict[match.group('port')] = [] mac_addr = mac.normalize_mac(match.group('mac_addr')) mac_dict[match.group('port')].append(mac_addr) except pexpect.TIMEOUT: return False return mac_dict
def get_mac_address_table(switch_ip, login, password, stdout=False): t = pexpect.spawn('telnet {}'.format(switch_ip), encoding='utf-8') if stdout: t.logfile = sys.stdout try: t.expect(['[Uu]ser[Nn]ame:', 'login:'******'[Pp]ass[Ww]ord:') t.sendline(password) t.expect('>', timeout=5) t.sendline(u'screen-length disable') t.expect('>') t.sendline(u'display mac-address') t.expect('>', timeout=600) mac_table = t.before t.sendline(u'quit') mac_dict = {} for entry in mac_table.split('\n'): match = re.search('^(?P<mac_addr>[-a-f0-9]{14})\s+\d+\s+Learned\s+(?P<port>\S+)\s+', entry, re.IGNORECASE) if match: if match.group('port') == 'CPU': continue if not match.group('port') in mac_dict: mac_dict[match.group('port')] = [] mac_addr = mac.normalize_mac(match.group('mac_addr')) mac_dict[match.group('port')].append(mac_addr) except pexpect.TIMEOUT: return False return mac_dict
def get_mac_address_table(switch_ip, login, password, stdout=False): t = pexpect.spawn('telnet {}'.format(switch_ip), encoding='utf-8') if stdout: t.logfile = sys.stdout nexus = False old_cisco = False # like c3500XL try: ret = t.expect(['[Uu]ser[Nn]ame:', 'login:'******'[Pp]ass[Ww]ord:') t.sendline(password) t.expect('#', timeout=5) t.sendline(u'terminal length 0') t.expect('#') t.sendline(u'show mac address-table dynamic') t.expect('#', timeout=600) mac_table = t.before if re.search('Invalid input detected', mac_table): # maybe this is old cisco t.sendline(u'show mac-address-table dynamic') t.expect('#', timeout=600) old_cisco = True mac_table = t.before t.sendline(u'logout') mac_dict = {} for entry in mac_table.split('\n'): if nexus: match = re.search( '\d+\s+(?P<mac_addr>\S+)\s+DYNAMIC\s+\S+\s+\S+\s+\S+\s+(?P<port>\S+)', entry, re.IGNORECASE) elif old_cisco: match = re.search( '(?P<mac_addr>\S+)\s+DYNAMIC\s+\d+\s+(?P<port>\S+)', entry, re.IGNORECASE) else: match = re.search( '\d+\s+(?P<mac_addr>\S+)\s+DYNAMIC ?([,ipxotherasigned]+)?\s+(?P<port>\S+)', entry, re.IGNORECASE) if match: if match.group('port') == 'CPU': continue if not match.group('port') in mac_dict: mac_dict[match.group('port')] = [] mac_addr = mac.normalize_mac(match.group('mac_addr')) mac_dict[match.group('port')].append(mac_addr) except pexpect.TIMEOUT: return False return mac_dict
def get_mac_address_table(switch_ip, login, password, stdout=False): des_small_cli = False t = pexpect.spawn('telnet {}'.format(switch_ip), encoding='utf-8') if stdout: t.logfile = sys.stdout try: t.expect(['[Uu]ser[Nn]ame:', 'login:'******'[Pp]ass[Ww]ord:') t.sendline(password) t.expect('#', timeout=5) if re.search('CMD>>', t.before, re.IGNORECASE): des_small_cli = True t.sendline(u'disable clipaging') t.expect('#') t.sendline(u'show fdb') # if we havn't enought right, we can't disable clipaging # we will better simulate tap on <ALL> ret = t.expect(['#', 'All'], timeout=120) mac_table = t.before if ret == 1: t.sendline(u'a') t.expect('#', timeout=600) mac_table += t.before t.sendline(u'enable clipaging') t.expect('#') t.sendline(u'logout') mac_dict = {} for entry in mac_table.split('\n'): if des_small_cli: match = re.search('^(?P<port>\d+)\s+(?P<mac_addr>\S+)\s+', entry) else: match = re.search( '\d+\s+\S+\s+(?P<mac_addr>\S+)\s+(?P<port>\S+)\s+', entry) if match: if match.group('port') == 'CPU': continue if match.group('port') not in mac_dict: mac_dict[match.group('port')] = [] mac_addr = mac.normalize_mac(match.group('mac_addr')) mac_dict[match.group('port')].append(mac_addr) except pexpect.TIMEOUT: return False return mac_dict
def get_mac_address_table(switch_ip, login, password, stdout=False): t = pexpect.spawn('telnet {}'.format(switch_ip), encoding='utf-8') if stdout: t.logfile = sys.stdout try: t.expect('>>User name:') t.sendline(login + u'\r\n') t.expect('>>User password:'******'\r\n') t.expect('>', timeout=5) t.sendline(u'enable\r\n') t.expect('#') t.sendline(u'config\r\n') t.expect('#') t.sendline(u'show mac-address dynamic\r\n') ret = t.expect(['#', 'Press'], timeout=600) mac_table = t.before while ret == 1: t.sendline(u'v\r\n') ret = t.expect(['#', 'Press'], timeout=600) mac_table += t.before t.sendline(u'end\r\n') t.expect('>') t.sendline(u'logout\r\n') mac_dict = {} for entry in mac_table.split('\n'): match = re.search('\s+(?P<mac_addr>\S+)\s+\d+\s+(?P<port>\S+)\s+', entry) if match: if match.group('port') == 'CPU': continue if not match.group('port') in mac_dict: mac_dict[match.group('port')] = [] mac_addr = mac.normalize_mac(match.group('mac_addr')) mac_dict[match.group('port')].append(mac_addr) except pexpect.TIMEOUT: return False return mac_dict
def get_mac_address_table(switch_ip, login, password, stdout=False): old_snr = False t = pexpect.spawn('telnet {}'.format(switch_ip), encoding='utf-8') if stdout: t.logfile = sys.stdout try: t.expect(['login:'******'[Uu]sername:']) t.sendline(login) t.expect('[Pp]assword:') t.sendline(password) t.expect('#', timeout=5) t.sendline(u'terminal length 0') t.expect('#') t.sendline(u'show mac address-table') t.expect('#', timeout=600) if 'Ambiguous command' in t.before: ret = t.sendline(u'show mac-address-table') t.expect('#', timeout=600) mac_table = t.before t.sendline(u'exit') mac_dict = {} for entry in mac_table.split('\n'): match = re.search( '\d+\s+(?P<mac_addr>\S+)\s+DYNAMIC( Hardware)?\s+(?P<port>\S+)', entry, re.IGNORECASE) if match: if match.group('port') == 'CPU': continue if not match.group('port') in mac_dict: mac_dict[match.group('port')] = [] mac_addr = mac.normalize_mac(match.group('mac_addr')) mac_dict[match.group('port')].append(mac_addr) except pexpect.TIMEOUT: return False return mac_dict