Python獲取指定網段正在使用的IP
更新時間:2020年12月14日 16:17:32 作者:清風軟件測試
這篇文章主要介紹了Python獲取指定網段正在使用的IP的示例代碼,幫助大家更好的利用python進行測試,感興趣的朋友可以了解下
#!/usr/bin/env python # -*- coding: utf-8 -*- ''''' 使用方法樣例 python test20.py 192.168.1.1 (會掃描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): ''''' 給出當前的127.0.0 ,然后掃描整個段所有地址 ''' 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('本次掃描共檢測到本網絡存在%s臺設備' % live_ip)
以上就是Python獲取指定網段正在使用的IP的詳細內容,更多關于python 獲取網段ip的資料請關注腳本之家其它相關文章!
相關文章
python?解決?pip?時報錯?no?suchoption:?--bulid-dir?的解決辦法(最新
在使用PyCharm虛擬環(huán)境pip時,有時會遇到錯誤提示“no?such?option:?--build-dir”,這可能是由于pip版本不兼容或其他原因導致的,本文將詳細講解如何解決這個問題,感興趣的朋友跟隨小編一起看看吧2023-05-05對python中數據集劃分函數StratifiedShuffleSplit的使用詳解
今天小編就為大家分享一篇對python中數據集劃分函數StratifiedShuffleSplit的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12