简体   繁体   English

生成专用IP地址的Python程序

[英]Python Program to generate the Private IP Addresses

Below given program is to generate N number of private IP addresses randomly. 下面给出的程序是随机生成N个私有IP地址。

If x1 = 172,x2 value should be from 16 to 31...But it generate the value from 0 to 255... Can someone please have a look and let me know what could be the error? 如果x1 = 172,x2的值应该是16到31 ...但是它生成的值是0到255 ...可以请人看看,让我知道可能是什么错误吗?

Code: 码:

import random

n = int(raw_input("How many IP addresses need to be generated \n"))

x1 = random.choice(["10","172","192"])


for i in range (0,n):

    if (x1 == 10 ):

        x2 = random.randint(0,255)

        x3 = random.randint(0,255)

        x4 = random.randint(0,255)

        print ".".join(map(str,([x1,x2,x3,x4])))



    elif (x1 == 172):

        x2 = random.randint(16,31)

        x3 = random.randint(0,255)

        x4 = random.randint(0,255)

        print ".".join(map(str,([x1,x2,x3,x4])))


    else:

        x2 = random.randint(0,255)

        x3 = random.randint(0,255)

        x4 = random.randint(0,255)

        print ".".join(map(str,([x1,x2,x3,x4])))

That is because you are checking int with string 那是因为您正在int with string检查int with string

ie)

1=="1"
Out[10]: False

Modification: 修改:

import random
n = int(raw_input("How many IP addresses need to be generated \n"))
x1 = random.choice(["172","192","10"])    
for i in range (0,n):
    if (int(x1) == 10 ):
        x2 = random.randint(0,255)        
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    elif (int(x1) == 172):
        x2 = random.randint(16,31)            
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    else:    
        x2 = random.randint(0,255)
        x3 = random.randint(0,255)        
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

edit: 编辑:

import random
n = int(raw_input("How many IP addresses need to be generated \n"))
x1 = random.choice([172,192,10])    
for i in range (0,n):
    if (int(x1) == 10 ):
        x2 = random.randint(0,255)        
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    elif (int(x1) == 172):
        x2 = random.randint(16,31)            
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    else:    
        x2 = random.randint(0,255)
        x3 = random.randint(0,255)        
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

edit2: 编辑2:

Since if and else or doing the same thing you can merge them as else 因为if and else或做相同的事情,您可以将它们合并为else

import random
n = int(raw_input("How many IP addresses need to be generated \n"))
x1 = random.choice([172,192,10])    
for i in range (0,n):
    if (int(x1) == 172):
        x2 = random.randint(16,31)            
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    else:    
        x2 = random.randint(0,255)
        x3 = random.randint(0,255)        
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

output: 输出:

How many IP addresses need to be generated 
20
10.88.254.205
10.205.49.201
10.67.147.81
10.10.75.63
10.136.166.197
10.241.237.2
10.33.204.114
10.10.190.132
10.11.72.207
10.24.178.32
10.215.156.125
10.75.79.15
10.47.159.174
10.177.12.191
10.96.189.105
10.141.216.118
10.99.138.176
10.92.138.176
10.81.147.16
10.246.147.4

edit3: 编辑3:

And I personally think that the x1 that is ip first segment should change randomly when looping 而且我个人认为, x1 that is ip first segment should change randomly when loopingx1 that is ip first segment should change randomly when looping

Therefore this edit code: 因此,此编辑代码:

import random
n = int(raw_input("How many IP addresses need to be generated \n"))

for i in range (0,n):
    x1 = random.choice([172,192,10]) #moved inside loop
    if (int(x1) == 172):
        x2 = random.randint(16,31)            
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    else:    
        x2 = random.randint(0,255)
        x3 = random.randint(0,255)        
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

output: 输出:

How many IP addresses need to be generated 
20
192.178.231.68
192.253.233.190
192.114.158.193
192.196.30.127
172.22.44.3
172.23.180.6
10.69.55.105
10.105.63.195
172.21.0.195
192.83.125.135
10.36.196.137
10.8.251.102
192.38.130.4
172.22.21.131
10.204.243.231
192.136.121.203
172.30.89.149
192.40.178.100
192.155.127.75
172.23.97.228

When x1 is set you are using quotes making it a string. 设置x1后,您将使用引号将其设置为字符串。 Remove the quotes and it will be a numeric. 删除引号,它将是一个数字。 This is important later when you compare it to a numeric value. 稍后将其与数字值进行比较时,这一点很重要。

x1 = random.choice([10,172,192])

Then the comparison elif (x1 == 172) will work as expected. 然后比较elif(x1 == 172)将按预期工作。

Can be done with this code? 可以用此代码完成吗?

from random import randint
def randomPrivateIPaddress():
    network_type = randint(0, 2)
    if network_type == 0:
        return "10.%d.%d.%d" % (randint(0, 255), randint(0, 255), randint(0, 255))
    elif network_type == 1:
        return "172.%d.%d.%d" % (randint(16, 31), randint(0, 255), randint(0, 255))
    else:
        return "192.168.%d.%d" % (randint(0,255), randint(0,255))

Then, you can use those string with ipaddress module. 然后,您可以将那些字符串与ipaddress模块​​一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM