Python获取指定网段正在使用的IP

作者:清风软件测试 时间:2022-04-22 23:49:27 


#!/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

来源:https://www.cnblogs.com/111testing/p/14113297.html

标签:python,网段,IP
0
投稿

猜你喜欢

  • 数据库理论:学习基于SQL数据库的算法

    2009-01-13 13:38:00
  • sql server update 表的问题

    2009-10-04 20:32:00
  • 平面设计中的文字设计

    2009-01-15 12:09:00
  • Python内建类型int源码学习

    2023-07-02 19:22:15
  • 如何在SQL2000的查询中使用XML-Data?

    2010-06-18 19:26:00
  • php基础字符串与数组知识点讲解

    2023-05-25 08:28:14
  • Http头 Range、Content-Range

    2010-06-25 19:19:00
  • CODEPAGE=936是什么意思?

    2009-07-05 18:37:00
  • HTML和CSS中的视觉语义

    2010-07-09 13:08:00
  • PHP基于rabbitmq操作类的生产者和消费者功能示例

    2023-11-23 20:03:03
  • JavaScript的9个陷阱及评点

    2007-08-28 15:10:00
  • 14条最佳JS代码编写技巧[译]

    2009-11-09 19:37:00
  • python神经网络学习利用PyTorch进行回归运算

    2023-02-24 13:30:47
  • python基础之定义类和对象详解

    2023-06-15 05:35:12
  • Go语言sort包函数使用示例

    2023-10-15 03:29:59
  • SQL Server 2005中利用xml拆分字符串序列

    2009-01-06 11:30:00
  • Go gRPC服务客户端流式RPC教程

    2023-07-16 06:08:55
  • SqlServer参数化查询之where in和like实现详解

    2012-05-22 18:10:50
  • python selenium UI自动化解决验证码的4种方法

    2022-10-09 20:43:06
  • 区别Javascript中的Null与Undefined

    2007-12-13 20:24:00
  • asp之家 网络编程 m.aspxhome.com