1

I have been working on this script for locating workstation's switchport on Cisco switches. I have been stuck on this for hours and have looked everywhere for some help. I know that when a regex 'expects a string' error that usually I am calling on a list. But for the life of me, I cannot find where I did that. Any help would be greatly appreciated. Thanks.

Code in question:

def check_ping(data = None):
    ping_result = re.findall(r'!!!!!', data)
    return ping_result

Error

Traceback (most recent call last):
File "IPtracetest.py", line 97, in <module>
ping_results = check_ping(data)
File "IPtracetest.py", line 25, in check_ping
ping_results = re.findall( r'!!!!!', data )
File "C:\Python27\lib\re.py", line 181, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer
P:\Python\Scripts>

Entire script if more context is needed

# get the user credentials
    def get_credentials():
        username = raw_input('Enter your secondary LANID: ')
        password = getpass.getpass('Enter password for user %s: ' % username)
        return username, password

    def get_ip():
        return raw_input('\nEnter IP address of device you which to trace: ')

    def get_ping():
         ping = "ping " + ip
         return ping

    def check_ping(data = None):
        ping_result = re.findall(r'!!!!!', data)
        return ping_result

    def get_ping_results(ping_results, ip ):
        if ping_results is '!!!!!' :
            trace = 'PING OK. Device is accessible'
        else:
            trace = 'Device is currently off the network!'
        return trace


    def get_gateways(data):
        gateways = re.findall(r'[0-9]+(?:\.[0-9]+){3}', data )  
        return gateways

    def get_gateway(gateway_results):
        gateway = gateway_results[-2]
        return gateway

    def get_mac(data):
        mac = re.findall(r'[0-9a-f]+(?:\.[0-9a-f]+){2}', data ) 
        mac = mac[-1]
        return mac  

    def get_access_switch(data):
        pattern = r'cs\d+\-\d+\w'
        access_switch = re.findall(pattern, data)
        return access_switch[0] #--- could return the index value, that should give you a string to pass off to the host

    #def get_end_switch(data):
    #   print data
    #   pattern = r'cs\d+\-\d+\w*'
    #   end_switch = re.findall(pattern, data)
    #   return end_switch


    def get_end_switch(data):
        #print data
        #pattern = r'cs\d+\-\d+\w*'
        end_switch = re.findall(r'[c][s][0-9][0-9][0-9][?:\-][\w][\w]', data)
        return end_switch[-2]   


    def get_port(data):
        pattern = r'[GF][ai]\d[\d|\/]*'
        port = re.findall(pattern, data)
        return port[0]

    def connect_to_device(username, password, host, command):

        try: 
            print '.... connecting'
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(host, username=username, password=password)
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            chan = ssh.invoke_shell()
            chan.send(command + ('\r'))
            time.sleep(.5)
            data = chan.recv(9999)
            return data
        except: 
            print "\n\n ****  Unable to connect. Check password, device may be down, or IP not in ARP table  ****\n\n"

    # ______________________________________MAIN______________________________________#             
    if __name__ == "__main__":
        data = 0
        #print '\nEnter an IP address at a location and you will get the end point switch and port: \n' 
        username, password = get_credentials()
        ip = get_ip()
        ping = get_ping()
        data = connect_to_device(username, password, 'mls319-2c', ping)
        ping_results = check_ping(data)
        trace = get_ping_results(ping_results, ip)
        if trace is 'PING OK. Device is accessible':
            data = connect_to_device(username, password, 'mls319-2c', 'traceroute ' + ip)
        gateway_results = get_gateways(data)
        gateway = get_gateway(gateway_results)
        data = connect_to_device(username, password, gateway, 'sh ip arp | i ' + ip)
        mac = get_mac(data)
        data = connect_to_device(username, password, gateway, 'sh cdp nei' )
        access_switch = get_access_switch(data)
        #end_host, end_port = trace_mac(username, password, access_switch, mac)
        data = connect_to_device(username, password, access_switch, 'trace mac ' + mac + ' ' + mac)
        end_switch = get_end_switch(data)
        port = get_port(data)
        print '\nRouter IP is ', gateway
        print '\nMAC address is ', mac
        print  '\nThe end switch is ', end_switch
        print '\nThe port is ', port
0

3 Answers 3

3

Input to re.findall must be string:

>>> import re
>>> re.findall("\w+", None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\re.py", line 181, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

May be you can try this:

def check_ping(data = None):
    if data:
        ping_result = re.findall(r'!!!!!', data)
        return ping_result
Sign up to request clarification or add additional context in comments.

1 Comment

That worked! I had to add the if statement to all my regex functions. Thank you!
0

May be data parameter here might be None, can you handle that case ?

def check_ping(data = None):
    if data is None:
       return
    ping_result = re.findall(r'!!!!!', data)
    return ping_result

Comments

0

re.findall expects a string as input so you can use an empty string as the default value.

def check_ping(data=""):
    ping_result = re.findall(r'!!!!!', data)
    return ping_result

This will remove the error. Also, have in mind that re.findall will return a list of strings, even if no matches are found, you'll get an empty list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.