簡體   English   中英

將新 ip 插入列表

[英]Inserting new ips into a list

我有這個程序可以列出我網絡上的 ip 地址,以及它們是在線還是離線以及 mac 地址。 我希望離線 ip 位於列表中,以便我可以檢查該 ip 是否不在列表中,它將顯示 NEW。

知道要做什么

import os

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: "))

ofip = ["192.168.2.0"]

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

        getmac.PORT = 44444  # Default: 55555

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

        elif exit_code != 0:
            print(ip, bcolors.FAIL + "OFFLINE" + bcolors.ENDC)
            ip = ofip

        elif exit_code != 0 and ip != ofip:
            print(ip, bcolors.OKGREEN + "NEW " + bcolors.ENDC + bcolors.OKBLUE + get_mac_address(ip=ip, network_request=True) + bcolors.ENDC)

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

我應該看到聯機打印新的脫機 IP 地址

這條線不會做你想要的。 您需要進行一些更改。

elif exit_code != 0 and ip != ofip:

ofip是一個列表(至少一開始是)而ip是一個字符串, !=在這里不起作用。 您應該使用in運算符。

elif exit_code != 0 and ip not in ofip:

第二個問題是解決ip是一個字符串而ofip是一個列表(當你第一次分配它時,后來你將它設置為一個字符串)。

而不是做,

ip = ofip

嘗試附加到列表中

ofip.append(ip)

最后一件事是,由於 if/elif 語句的流動方式,第二個 elif 永遠不會運行。 如果退出代碼不是 0,那么它將始終命中第一個 elif 並且永遠不會命中第二個。 切換這些。 將更具體的條件放在不太具體的條件之前。

提示:您可以使用集合而不是列表來加快查找速度。

import os

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: "))

ofip = ["192.168.2.0"]

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

        getmac.PORT = 44444  # Default: 55555

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

        elif exit_code != 0:
            if ip not in ofip:
                ofip.append(ip)
                print(ip, bcolors.OKGREEN + "NEW " + bcolors.ENDC + bcolors.OKBLUE + get_mac_address(ip=ip, network_request=True) + bcolors.ENDC)
            else:
                print(ip, bcolors.FAIL + "OFFLINE" + bcolors.ENDC)

我的問題已經解決,代碼如下:

import os

from getmac import get_mac_address

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    YELLOW = "\033[29m"
    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: "))

ofip = []

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

        getmac.PORT = 44444  # Default: 55555

        if exit_code == 0:
            if ip in ofip:
                print(ip, bcolors.HEADER + "NEW " + bcolors.ENDC + bcolors.OKBLUE + get_mac_address(ip=ip, network_request=True) + bcolors.ENDC)
                ofip.remove(ip)
            print(ip, bcolors.OKGREEN + "ONLINE " + bcolors.ENDC + bcolors.OKBLUE + get_mac_address(ip=ip, network_request=True) + bcolors.ENDC)

        elif exit_code != 0:
            print(ip, bcolors.FAIL + "OFFLINE" + bcolors.ENDC)
            ofip.append(ip)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM