簡體   English   中英

Python 針對子網檢查 ipaddress 以創建路由

[英]Python check ipaddress against a subnet to create a route

為這個新手問題道歉,但我嘗試學習;)

我目前正在編寫一個腳本,該腳本應該創建一個路由並檢查用戶輸入是否有意義。 它應該檢查輸入的網絡 id 和掩碼是否有效,然后它還應該檢查 nexthop gw 是否在該網絡內。

但我面臨兩個問題:

  1. 當我在腳本運行時鍵入“no”時,我無法跳出 while 循環。 僅當我將其更改為“while selection ==”yes” without or “y”語句時才有效
  2. 我不明白為什么此檢查不起作用:如果網絡中的 ipaddress.ip_address(gateway) == True,則腳本總是通過 else。

最后,當我的字典充滿子網、ip 地址、網關和路由標簽時,我很高興。

我在下面添加了一些示例 output。

版本:Python3.9.1

import ipaddress

networks = {}

def network_input():
    choice = input('Propagate a new network, yes/no? ')
    while choice == 'yes' or 'y':
        network = input('Network-ID with CIDR notation (x.x.x.x/x): ')
        try:
            network = ipaddress.ip_network(network)
        except ValueError:
            print('invalid Address or Netmask:', network)
            return network_input()
        else:
            networks.update({str(network.network_address):(str(network.netmask))})
            print(networks)
            gateway = input("Next Hop Gateway: ")
            try:
                gateway = ipaddress.ip_address(gateway)
            except ValueError:
                print('invalid Gateway:', gateway)
                return network_input()
            else:
                if ipaddress.ip_address(gateway) in network == True:
                    tag = input("Route Description: ")
                    networks.update({str(network.network_address):(str(network.netmask),str(gateway),tag)})
                    print(networks)
                    return network_input()
                else:
                    print('Gateway is not part of the subnet', network)
                    return network_input()

network_input()

Output:

Propagate a new network, yes/no? yes
Network-ID with CIDR notation (x.x.x.x/x): 192.168.2.0/24
{'192.168.2.0': '255.255.255.0'}
Next Hop Gateway: 192.168.2.1
Gateway is not part of the subnet 192.168.2.0/24
Propagate a new network, yes/no?

提前致謝。

這里有很多問題。 我不確定我是否修復了所有這些:

  • 不要遞歸調用network_input()到 go 回到 function 的頂部。 使用 while 循環。
  • 非空字符串被認為是真的,所以if choice == 'yes' or 'no'被解析為if (choice == 'yes') or ('y')總是True
  • if x in <container>中的 x 為 True 如果 x 是容器的元素...無需檢查== True
import ipaddress

networks = {}

def network_input():
    while True:
        choice = input('Propagate a new network, yes/no? ')
        if choice in ('yes','y'):
            network = input('Network-ID with CIDR notation (x.x.x.x/x): ')
            try:
                network = ipaddress.ip_network(network)
            except ValueError:
                print('invalid Address or Netmask:', network)
            else:
                networks.update({str(network.network_address):(str(network.netmask))})
                print(networks)
                gateway = input("Next Hop Gateway: ")
                try:
                    gateway = ipaddress.ip_address(gateway)
                except ValueError:
                    print('invalid Gateway:', gateway)
                else:
                    if ipaddress.ip_address(gateway) in network:
                        tag = input("Route Description: ")
                        networks.update({str(network.network_address):(str(network.netmask),str(gateway),tag)})
                        print(networks)
                    else:
                        print('Gateway is not part of the subnet', network)
        else:
            break

network_input()

Output:

Propagate a new network, yes/no? y
Network-ID with CIDR notation (x.x.x.x/x): 192.168.2.0/24
{'192.168.2.0': '255.255.255.0'}
Next Hop Gateway: 192.168.2.1
Route Description: blah
{'192.168.2.0': ('255.255.255.0', '192.168.2.1', 'blah')}
Propagate a new network, yes/no? n

對於 1,我猜您正在尋找的是while choice == 'yes' or choice == 'y':而不是while choice == 'yes' or 'y': 這是因為語句的第二部分轉換為while 'y'而不是while choice == 'y' 由於 'y' 不會轉換為 0 或 False,並且條件語句需要 boolean,因此對於條件語句,'y' 被視為 True。

暫無
暫無

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

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