欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2021-05-04 10:50:15|栏目: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数据清洗容易遇到的函数-re.sub bytes string详解

栏    目:Python代码

下一篇:Python3实现爬虫爬取赶集网列表功能【基于request和BeautifulSoup模块】

本文标题:Python获取指定网段正在使用的IP

本文地址:http://www.codeinn.net/misctech/114560.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有