我如何在python中找到网络中的ips [英] How can i find the ips in network in python

查看:29
本文介绍了我如何在python中找到网络中的ips的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何通过python编程找到网络中范围(即132.32.0.3到132.32.0.44)的TCP ip,还想知道哪些ip是存活的,哪些是死的.请寄给我..谢谢你的回复......

解决方案

第 1 部分 - 寻找 IP"

您的示例范围 132.32.0.3132.32.0.44 不匹配任何子网,这很奇怪.

通常用于检查主机是否启动和关闭的应用程序通常在子网内,例如192.168.0.0/28(主机地址:192.168.0.1192.168.0.14).

如果您想计算子网内的地址,我建议您使用 ipaddr.例如:

<预><代码>>>>从 ipaddr 导入 IPv4Address、IPNetwork>>>对于 IPNetwork('192.168.0.0/28').iterhosts() 中的一个:...打印一个...192.168.0.1192.168.0.2192.168.0.3192.168.0.4192.168.0.5192.168.0.6192.168.0.7192.168.0.8192.168.0.9192.168.0.10192.168.0.11192.168.0.12192.168.0.13192.168.0.14

但是,如果您确定想要一个任意范围.您可以将 IPv4 地址转换为整数、递增并转换回点分 IP.例如:

def aton(a):"""将带点的 ip 地址更改为整数例如'192.168.0.1' ->3232235521L"""return reduce(lambda x,y: (x<<8) + y, [ int(x) for x in a.split('.') ])def ntoa(n):"""将整数更改为带点的 ip 地址.例如3232235522L ->'192.168.0.2'"""return "%d.%d.%d.%d" % (n >> 24,(n & 0xffffff) >> 16,(n & 0xffff) >> 8,(n & 0xffffff); 0xff))定义任意范围(a1,a2):"""生成包含两个地址之间的所有 IP 地址"""n1, n2 = aton(a1), aton(a2)断言 n1 

提供:

<预><代码>>>>对于任意范围内的 a('192.168.0.10','192.168.0.20'):...打印一个...192.168.0.10192.168.0.11192.168.0.12192.168.0.13192.168.0.14192.168.0.15192.168.0.16192.168.0.17192.168.0.18192.168.0.19192.168.0.20

<小时>

第 2 部分 - 生或死"

生"或死"的问题很复杂,完全取决于您对这些术语的含义.为了提供上下文和对比,以下是与 IP 地址/主机相关的可测试质量列表:

  • 响应 ARP 请求?
  • 响应 ICMP 回显请求?
  • 响应 TCP SYN?

How can i find the TCP ips in network with the range(i.e 132.32.0.3 to 132.32.0.44) through python programming and also want to know the which ips are alive and which are dead. please send me.. thanks for the repliers...

解决方案

Part 1 - "Finding IPs"

Your example range, 132.32.0.3 to 132.32.0.44 doesn't match any subnet, which is curious.

Typically applications for checking whether hosts are up and down are normally scoped within a subnet, e.g. 192.168.0.0/28 (host addresses: 192.168.0.1 to 192.168.0.14).

If you wanted to calculate the addresses within a subnet, I'd suggest you use ipaddr. E.g.:

>>> from ipaddr import IPv4Address, IPNetwork
>>> for a in IPNetwork('192.168.0.0/28').iterhosts():
...   print a
...
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
192.168.0.8
192.168.0.9
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14

However, if you're sure that you want an arbitrary range. You can convert an IPv4 address to an integer, increment and convert back to dotted IP. E.g.:

def aton(a):
  """
  Change dotted ip address to integer
  e.g. '192.168.0.1' -> 3232235521L
  """
  return reduce(lambda x,y: (x<<8) + y, [ int(x) for x in a.split('.') ])

def ntoa(n):
  """
  Change an integer to a dotted ip address.
  e.g. 3232235522L -> '192.168.0.2'
  """
  return "%d.%d.%d.%d" % (n >> 24,(n & 0xffffff) >> 16,(n & 0xffff) >> 8,(n & 0xff))

def arbitraryRange(a1,a2):
  """
  Generate all IP addresses between two addresses inclusively
  """
  n1, n2 = aton(a1), aton(a2)
  assert n1 < n2
  i = n1
  while i <= n2:
    yield ntoa(i)
    i += 1

Providing:

>>> for a in arbitraryRange('192.168.0.10','192.168.0.20'):
...   print a
...
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15
192.168.0.16
192.168.0.17
192.168.0.18
192.168.0.19
192.168.0.20


Part 2 - "Alive or Dead"

The question of "alive" or "dead" is complex and entirely dependent on what you mean by those terms. To provide context and contrast, here's a list of testable qualities with regard to an IP address / host:

  • Responds to ARP request?
  • Responds to ICMP echo request?
  • Responds to TCP SYN?

这篇关于我如何在python中找到网络中的ips的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆