Skip to main content
 首页 » 编程设计

python之这个用于扫描子网的 python 代码有什么问题

2024年10月01日11Renyi-Fan

此代码用于使用 ping 每个主机来查找事件主机。但我遇到了一些问题。我的代码:

#!/usr/bin/python 
import os  
global lis 
lis = [] 
global cnt 
cnt = 0  
print("Scanning the Subnet : ") 
subnet_input = input("->> ") 
subnet_input = subnet_input.split(".") 
for x in range(98,105): 
    subnet_input[3] = x 
    str(subnet_input) 
    new_sub = str(subnet_input[0])+'.'+str(subnet_input[1])+'.'+str(subnet_input[2])+'.'+str(subnet_input[3]) 
    res = os.system("ping -c 4 {}".format(new_sub)) 
    if res==0: 
        cnt = cnt+1 
        print("[*] {} is Alive".format(new_sub)) 
        str(new_sub) 
        lis.append(new_sub) 
print('--------------------------------------') 
print("[*] ALL ALIVE HOSTS ARE {}".format(cnt)) 
print('--------------------------------------') 
 
print(lis) 

我收到以下错误:

Traceback (most recent call last): 
File "Ping.py", line 8, in <module> 
subnet_input = input("->> ") 
File "<string>", line 1 
192.168.1.0 
        ^ 

请您参考如下方法:

使用 python 2,用户发送到 input() 的内容会通过 eval(),因此会出现错误。

您可以改用 raw_input(),或者切换到 python 3。