您好,登錄后才能下訂單哦!
利用Python怎么獲取指定網(wǎng)段的IP?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
實(shí)現(xiàn)方法如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- ''''' 使用方法樣例 python test20.py 192.168.1.1 (會(huì)掃描192.168.1.1-255的ip) 多線程加速后大概十幾秒 ''' import platform import sys import os import time import threading live_ip = 0 def get_os(): os = platform.system() if os == "Windows": return "n" else: return "c" def ping_ip(ip_str): cmd = ["ping", "-{op}".format(op=get_os()), "1", ip_str] output = os.popen(" ".join(cmd)).readlines() for line in output: if str(line).upper().find("TTL") >= 0: print("ip: %s is ok ***" % ip_str) global live_ip live_ip += 1 break def find_ip(ip_prefix): ''''' 給出當(dāng)前的127.0.0 ,然后掃描整個(gè)段所有地址 ''' threads = [] for i in range(1, 256): ip = '%s.%s' % (ip_prefix, i) threads.append(threading.Thread(target=ping_ip, args={ip, })) for i in threads: i.start() for i in threads: i.join() if __name__ == "__main__": print("start time %s" % time.ctime()) cmd_args = sys.argv[1:] args = "".join(cmd_args) ip_pre = '.'.join(args.split('.')[:-1]) find_ip(ip_pre) print("end time %s" % time.ctime()) print('本次掃描共檢測(cè)到本網(wǎng)絡(luò)存在%s臺(tái)設(shè)備' % live_ip)
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。