日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

利用python实现IP扫描

發布時間:2023/11/27 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用python实现IP扫描 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求:寫一個腳本,判斷192.168.11.0/24網絡里,當前在線ip有哪些?

知識點:

1 使用subprocess模塊,來調用系統命令,執行ping 192.168.11.xxx 命令

2 調用系統命令執行ping命令的時候,會有返回值(ping的結果),需要用到stdout=fnull, stderr=fnull方法,屏蔽系統執行命令的返回值

常規版本(代碼)

import os
import time
import subprocess
def ping_call():start_time = time.time()fnull = open(os.devnull, 'w')for i in range(1, 256):ipaddr = 'ping 192.168.11.' + str(i)result = subprocess.call(ipaddr + ' -n 2', shell=True, stdout=fnull, stderr=fnull)current_time = time.strftime('%Y%m%d-%H:%M:%S', time.localtime())if result:print('時間:{} ip地址:{} ping fall'.format(current_time, ipaddr))else:print('時間:{} ip地址:{} ping ok'.format(current_time, ipaddr))print('程序耗時{:.2f}'.format(time.time() - start_time))fnull.close()
ping_call()

執行效果:

上面的執行速度非常慢,怎么能讓程序執行速度快起來?

python提供了進程,線程,協程。分別用這三個對上面代碼改進,提高執行效率,測試一波效率

進程池異步執行 – 開啟20個進程

import os
import time
import subprocess
from multiprocessing import Pool
def ping_call(num):fnull = open(os.devnull, 'w')ipaddr = 'ping 192.168.11.' + str(num)result = subprocess.call(ipaddr + ' -n 2', shell=True, stdout=fnull, stderr=fnull)current_time = time.strftime('%Y%m%d-%H:%M:%S', time.localtime())if result:print('時間:{} ip地址:{} ping fall'.format(current_time, ipaddr))else:print('時間:{} ip地址:{} ping ok'.format(current_time, ipaddr))fnull.close()if __name__ == '__main__':start_time = time.time()p = Pool(20)res_l = []for i in range(1, 256):res = p.apply_async(ping_call, args=(i,))res_l.append(res)for res in res_l:res.get()print('程序耗時{:.2f}'.format(time.time() - start_time))

執行結果:

線程池異步執行 – 開啟20個線程

import os
import time
import subprocess
from concurrent.futures import ThreadPoolExecutor
def ping_call(num):fnull = open(os.devnull, 'w')ipaddr = 'ping 192.168.11.' + str(num)result = subprocess.call(ipaddr + ' -n 2', shell=True, stdout=fnull, stderr=fnull)current_time = time.strftime('%Y%m%d-%H:%M:%S', time.localtime())if result:print('時間:{} ip地址:{} ping fall'.format(current_time, ipaddr))else:print('時間:{} ip地址:{} ping ok'.format(current_time, ipaddr))fnull.close()if __name__ == '__main__':start_time = time.time()thread_pool = ThreadPoolExecutor(20)ret_lst = []for i in range(1, 256):ret = thread_pool.submit(ping_call, i)ret_lst.append(ret)thread_pool.shutdown()for ret in ret_lst:ret.result()print('線程池(20)異步-->耗時{:.2f}'.format(time.time() - start_time))

執行結果:

協程執行—(執行多個任務,遇到I/O操作就切換)

使用gevent前,需要pip install gevent

from gevent import monkey;monkey.patch_all()
import gevent
import os
import time
import subprocessdef ping_call(num):fnull = open(os.devnull, 'w')ipaddr = 'ping 192.168.11.' + str(num)result = subprocess.call(ipaddr + ' -n 2', shell=True, stdout=fnull, stderr=fnull)current_time = time.strftime('%Y%m%d-%H:%M:%S', time.localtime())if result:print('時間:{} ip地址:{} ping fall'.format(current_time, ipaddr))else:print('時間:{} ip地址:{} ping ok'.format(current_time, ipaddr))fnull.close()def asynchronous(): # 異步g_l = [gevent.spawn(ping_call, i) for i in range(1, 256)]gevent.joinall(g_l)if __name__ == '__main__':start_time = time.time()asynchronous()print('協程執行-->耗時{:.2f}'.format(time.time() - start_time))

執行結果:

遇到I/O操作,協程的效率比進程,線程高很多!

總結:python中,涉及到I/O阻塞的程序中,使用協程的效率最高

最后附帶協程池代碼

gevent.pool

from gevent import monkey;monkey.patch_all()
import gevent
import os
import time
import subprocess
import gevent.pooldef ping_call(num):fnull = open(os.devnull, 'w')ipaddr = 'ping 192.168.11.' + str(num)result = subprocess.call(ipaddr + ' -n 2', shell=True, stdout=fnull, stderr=fnull)current_time = time.strftime('%Y%m%d-%H:%M:%S', time.localtime())if result:print('時間:{} ip地址:{} ping fall'.format(current_time, ipaddr))else:print('時間:{} ip地址:{} ping ok'.format(current_time, ipaddr))fnull.close()if __name__ == '__main__':start_time = time.time()res_l = []p = gevent.pool.Pool(100)for i in range(1, 256):res_l.append(p.spawn(ping_call, i))gevent.joinall(res_l)print('協程池執行-->耗時{:.2f}'.format(time.time() - start_time))

執行結果:

總結

以上是生活随笔為你收集整理的利用python实现IP扫描的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:利用python实现IP扫描