Python2.7.11:TypeError:期望字符串或缓冲区= re.findall

时间:2016-04-10 18:53:53

标签: python regex python-2.7 typeerror

我一直在研究这个脚本,用于在思科交换机上定位工作站的交换机端口。我被困在这几个小时,并到处寻找一些帮助。我知道当一个正则表达式需要一个字符串'通常我在列表中调用的错误。但是对于我的生活,我无法找到我做到的地方。任何帮助将不胜感激。感谢。

有问题的代码:

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

错误

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>

如果需要更多上下文,则需要整个脚本

# 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

2 个答案:

答案 0 :(得分:3)

re.findall的输入必须是字符串:

>>> 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

可能你可以试试这个:

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

答案 1 :(得分:0)

这里的数据参数可能是None,你能处理那个案例吗?

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