Python 脚本失败并丢弃 0 CPU 用法
Python scrips failing and dropping 0 CPU usage
我的脚本总体目标是对 /8 网络执行有效的 ping。为此,我 运行 同时扫描 32 个脚本,每个脚本扫描一个 /13。
脚本的基础是
import ipaddress
import time
import fileinput
import pingpack
set = 0
mainset =0
while mainset < 8:
while set < 256:
net_addr = ("10.{}.{}.0/24".format(mainset,set))
ip_net = ipaddress.ip_network(net_addr)
all_hosts = list(ip_net.hosts())
f_out_file=('{}_Ping.txt'.format(mainset))
for i in range(len(all_hosts)):
output = pingpack.ping("{}".format(all_hosts[i]))
if output != None:
with open(f_out_file,'a',newline="\n") as file:
file.write("{}, Y\r\n".format(all_hosts[i]))
print ("{}".format("Subnet Complete"))
set = set + 1
set=0
它自己运行的脚本和 运行s 并且当它自己 运行 时给了我一个很好的输出。我 运行ning 遇到的问题是,当我为每个子网获得其中的 32 个 运行ning 时,它们 运行 在 python 进程锁定并停止之前进行了大约 8 个设置循环写作。
我用来启动32的脚本如下
from subprocess import Popen, PIPE
import time
i = 0
count=0
while i < 32:
process = Popen(['ping{}.py'.format(i),"{}".format(count)], stdout=PIPE, stderr=PIPE, shell=True)
print(count)
print(i)
i = i + 1
count=count+8
time.sleep(1)
在这种情况下;是的;我确实有 32 个重复的脚本,每个脚本都为不同的 /13 subents 更改了 2 行。它可能和某些人一样有效;但它让他们开始 运行ning。
我将如何找到停止这些脚本的原因?
旁注:是的,我知道我可以使用 NMAP 或 Angry IP Scanner 之类的东西来做到这一点;但他们都需要 90 多个小时来扫描整个 /8;我试图在更合理的时间范围内将其缩短为 运行。
你的第一个问题是当你移动到下一个主集时,set
永远不会设置回零。你的第二个问题是 mainset
永远不会增加。而不是一对晦涩的 while 循环,为什么不呢:
for mainset in xrange(8):
for set in xrange(256):
此外,in range(len(all_hosts))
是一种代码味道(事实证明你从不使用 i
除了写 all_hosts[i]
)。为什么不呢:
for host in all_hosts:
我的脚本总体目标是对 /8 网络执行有效的 ping。为此,我 运行 同时扫描 32 个脚本,每个脚本扫描一个 /13。
脚本的基础是
import ipaddress
import time
import fileinput
import pingpack
set = 0
mainset =0
while mainset < 8:
while set < 256:
net_addr = ("10.{}.{}.0/24".format(mainset,set))
ip_net = ipaddress.ip_network(net_addr)
all_hosts = list(ip_net.hosts())
f_out_file=('{}_Ping.txt'.format(mainset))
for i in range(len(all_hosts)):
output = pingpack.ping("{}".format(all_hosts[i]))
if output != None:
with open(f_out_file,'a',newline="\n") as file:
file.write("{}, Y\r\n".format(all_hosts[i]))
print ("{}".format("Subnet Complete"))
set = set + 1
set=0
它自己运行的脚本和 运行s 并且当它自己 运行 时给了我一个很好的输出。我 运行ning 遇到的问题是,当我为每个子网获得其中的 32 个 运行ning 时,它们 运行 在 python 进程锁定并停止之前进行了大约 8 个设置循环写作。
我用来启动32的脚本如下
from subprocess import Popen, PIPE
import time
i = 0
count=0
while i < 32:
process = Popen(['ping{}.py'.format(i),"{}".format(count)], stdout=PIPE, stderr=PIPE, shell=True)
print(count)
print(i)
i = i + 1
count=count+8
time.sleep(1)
在这种情况下;是的;我确实有 32 个重复的脚本,每个脚本都为不同的 /13 subents 更改了 2 行。它可能和某些人一样有效;但它让他们开始 运行ning。
我将如何找到停止这些脚本的原因?
旁注:是的,我知道我可以使用 NMAP 或 Angry IP Scanner 之类的东西来做到这一点;但他们都需要 90 多个小时来扫描整个 /8;我试图在更合理的时间范围内将其缩短为 运行。
你的第一个问题是当你移动到下一个主集时,set
永远不会设置回零。你的第二个问题是 mainset
永远不会增加。而不是一对晦涩的 while 循环,为什么不呢:
for mainset in xrange(8):
for set in xrange(256):
此外,in range(len(all_hosts))
是一种代码味道(事实证明你从不使用 i
除了写 all_hosts[i]
)。为什么不呢:
for host in all_hosts: