本文整理汇总了Python中ipcalc.Network.host_first方法的典型用法代码示例。如果您正苦于以下问题:Python Network.host_first方法的具体用法?Python Network.host_first怎么用?Python Network.host_first使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ipcalc.Network
的用法示例。
在下文中一共展示了Network.host_first方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import host_first [as 别名]
def main():
parser = OptionParser()
parser.add_option("-m", "--multithread", dest="multithread", help="number of threads for concurrency, default is 10")
parser.add_option("-t", "--target", dest="target", help="Target ip in network to scan -t 192.168.192.0.10/24")
parser.add_option("-s", "--sleeping", dest="sleeping", help="Seconds to sleep after one cycle, default is 1")
parser.add_option("-f", "--filename", dest="filename", help="the file to record recent counting result")
parser.add_option("-j", "--maxjump", dest="maxjump", help="if more than MAXJUMP continuous IPv4 addresses not respond, stop")
parser.add_option("-w", "--timeout", dest="timeout", help="Ping timeout, default: 2")
parser.add_option("-c", "--count", dest="count", help="Stop after sending <count> requests to host, default: 1")
parser.add_option("-i", "--interval", dest="interval", help="Wait <interval> seconds between sending packet, default: 0.2")
(options, args) = parser.parse_args()
if not options.target or not '/' in options.target:
print "please specify a target: -t 192.168.0.10/24"
print "use -h to view helping text"
return
interval = (options.interval if options.interval else '0.1')
timeout = (options.timeout if options.timeout else '1')
count = (options.count if options.count else '1')
multithread = (int(options.multithread) if options.multithread else 10)
sleeping = (int(options.sleeping) if options.sleeping else 1)
target = options.target
filename = (options.filename if options.filename else './subnet_count')
maxjump = (int(options.maxjump) if options.maxjump else 256*256*256*256)
net = Network(target)
DEVNULL = open(os.devnull, 'w')
commands.getstatusoutput("echo "+str(0)+" > "+filename)
global alive_num
global last_alive_index
while(1):
alive_num = 0
last_alive_index = 0
index = 0
thread_pool = []
need_to_ping = Network(str(net.host_first()) + '/' + target.split('/')[1])
for ip in need_to_ping:
index += 1
if(index-last_alive_index > maxjump):
break;
t = PingOne(interval,timeout,count,ip,index,DEVNULL)
thread_pool.append(t)
if( index > 0 and index % multithread == 0):
for tt in thread_pool:
tt.start()
for tt in thread_pool:
tt.join()
thread_pool[:] = []
# last turn
if thread_pool:
for tt in thread_pool:
tt.start()
for tt in thread_pool:
tt.join()
thread_pool[:] = []
commands.getstatusoutput("echo "+str(alive_num)+" > "+filename)
print "find "+str(alive_num)+" alive ipv4 addresses"
time.sleep(sleeping)