隐形无效语法

时间:2019-08-07 18:16:15

标签: python python-3.x networking

我正在运行此代码以列出网络上的IP地址以及mac地址,但是遇到了这个问题。它说语法无效,但我似乎找不到它的问题

我尝试删除空格并将其替换为制表符,但无法解决问题。我也尝试将它们向上或向下移动一个,但仍然无法正常工作。有帮助吗?

整个代码:

from getmac import get_mac_address

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

minr = int(input("Starting Ip: "))
maxr = int(input("Ending Ip: "))


while True:

    for num in range(minr, maxr + 1): #plus one is to include the last digit entered
        ip = "192.168.2." + str(num)

        from getmac import getmac

        exit_code = os.system("ping -n 1 -w 1 " + ip + " > nul") # Windows
        #exit_code = os.system("ping -c 1 -W 1 " + ip + " > /dev/null") # Linux

        getmac.PORT = 44444  # Default: 55555

        if exit_code == 0:
            print(ip, bcolors.OKGREEN + "ONLINE" + bcolors.ENDC + get_mac_address(ip=ip, network_request=True)

        elif (ip == '192.168.2.' + str(maxr + 1) and exit_code == 0):
            print('192.168.2.' + str(maxr), bcolors.OKGREEN + "ONLINE" + bcolors.ENDC + get_mac_address(ip=ip, network_request=True))
            print("")
            print(bcolors.HEADER + "Beginning" + bcolors.ENDC)
            print("")

        elif (ip == '192.168.2.' + str(maxr)):
            print('192.168.2.' + str(maxr), bcolors.FAIL + "OFFLINE" + bcolors.ENDC)
            print("")
            print(bcolors.HEADER + "Refreshed" + bcolors.ENDC)
            print("")

        else:
            print(ip, bcolors.FAIL + "OFFLINE" + bcolors.ENDC)

我应该可以在Mac上看到IP地址接收者,但是却收到此错误代码:

$ python test.py
  File "test.py", line 34
    elif (ip == '192.168.2.' + str(maxr + 1) and exit_code == 0):
       ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:0)

我只是忘了在上面一行的末尾添加)。感谢@depperm,他向我展示了我的错误。上面写着'''print(ip,bcolors.OKGREEN +“ ONLINE” + bcolors.ENDC + get_mac_address(ip = ip,network_request = True)

在括号的末尾再添加一个。 '''