How can i find the ips in network in python

2024/10/6 8:59:23

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...

Answer

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 integere.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 < n2i = n1while 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?
https://en.xdnf.cn/q/119430.html

Related Q&A

Python Coding (call function / return values)

I am having trouble writing code for this question. I have seen this question asked in a few places but I still cannot figure out the answer from the tips they provided. The question is: Write a progr…

discord.py Mention user by name

I am trying to mention a user by their name in discord.py. My current code is: @bot.command(name=mention) @commands.has_role(OwnerCommands) async def mention(ctx, *, member: discord.Member):memberid = …

Python unexpected EOF while parsing : syntax error

I am trying to do a simple toto history with a dictionary and function however I have this funny syntax error that keeps appearing that states "unexpected EOF while parsing" on the python she…

How can I read part of file and write the rest to another file?

I have multiple large csv file. How can I read part of each file and write 10% of the data/rows to another file?

Encryption/Decryption - Python GCSE [duplicate]

This question already has an answer here:Encryption and Decryption within the alphabet - Python GCSE(1 answer)Closed 8 years ago.I am currently trying to write a program, for school, in order to encryp…

Convert decimal to binary (Python) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Kivy Digital Clock Issues

Im trying to add a digital clock to my Kivy program, it seems to be having trouble.Here is the .py:import kivykivy.require(1.10.0)from kivy.lang import Builder from kivy.uix.screenmanager import Screen…

Read a file name and create a column with it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Python : T test ind looping over columns of df

My dataframe is composed of accounting variables and a dummy variable that allows me to identify two types of company. I would like to perform a t-test for every column of my dataframe in order to comp…

I want to understand which line of code outputs **none** in the function

The last line of the output is none can someone explain why pls def just_lyrics():print ("i am a bad coder")print (" i keep trying to learn everday")def double_lyrics():just_lyrics(…