Python Forum
Need some guidance on a script to ping a list of ip's - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need some guidance on a script to ping a list of ip's (/thread-40509.html)

Pages: 1 2


Need some guidance on a script to ping a list of ip's - cubangt - Aug-09-2023

Im trying to tweak this logic a bit to return some additional information or even look at threading it so it runs a little faster.
I have a list of 3036 ip addresses i need to ping in a timely manner about 3 times aday.

Currently the below run at just under 4 minutes, which is better than the macro someone wrote in excel to accomplish the same thing(takes 30 minutes)

import os
import time

start = time.time()

with open("ip_list.txt") as file:
    park = file.read()
    park = park.splitlines()
    print(" {park}  \n")
    # ping for each ip in the file
for ip in park:
    response = os.popen(f"ping -c 1 {ip} ").read()
    # Pinging each IP address 4 times
    
    #saving some ping output details to output file
    if("Request timed out." or "unreachable") in response:
	    print(response)
	    f = open("ip_output.txt","a")
	    f.write(str(ip) + ' link is down'+'\n')
	    f.close() 
    else:
	    print(response)
	    f = open("ip_output.txt","a")  
	    f.write(str(ip) + ' is up '+'\n')
	    f.close() 
    # print output file to screen
with open("ip_output.txt") as file:
    output = file.read()
    f.close()
    print(output)
#with open("ip_output.txt","w") as file:    
	#pass

end = time.time()
print(end - start)
Right now this outputs the IP address and the status, but what we would like to capture for historical purposes is the following in a CSV file to consume elsewhere.

File to contain:
Ip, Ping Time(ms) value, timestamp of run(this needs to be the same for all records at time of each run)
Example:
8.8.8.8, 20, 8:50pm


Also the above when run, returns this message:
Access denied. Option -c requires administrative privileges.

Even though it displays this on the console the script still runs and returns what i need, but not sure how to get around that OR if that is impacting the run time of the script. I mean 4minutes is good compared to 30minutes, but if i can improve that time even more it would be great.

Any suggestions or examples on how to tweak this to capture the additional data? Subprocess? Threading?


RE: Need some guidance on a script to ping a list of ip's - deanhystad - Aug-09-2023

There is a Python ICMP library, so you could write you own python version of ping. You might not even need ICMP. Just test if you can complete a socket connection. I don't see any reason why you couldn't "ping" 3000 ip addresses in a few seconds.

Checxk multiping example:
https://pypi.org/project/icmplib/


RE: Need some guidance on a script to ping a list of ip's - cubangt - Aug-09-2023

im reading the installation steps and dont see a way to install on my instance of conda/spyder

Reading the capabilities sounds like it would handle what im trying to do.. just need to find a way to install and test it out.
Any suggestions?

Retrieving notices: ...working... done
Collecting package metadata (current_repodata.json): done
Solving environment: unsuccessful initial attempt using frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: unsuccessful initial attempt using frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

  - icmplib

Current channels:

  - https://repo.anaconda.com/pkgs/main/win-64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/win-64
  - https://repo.anaconda.com/pkgs/r/noarch
  - https://repo.anaconda.com/pkgs/msys2/win-64
  - https://repo.anaconda.com/pkgs/msys2/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.



RE: Need some guidance on a script to ping a list of ip's - cubangt - Aug-09-2023

I did do the install using the pip command and when i run the sample code from the site i get this error:

Error:
File J:\ProgramData\Anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals) File i:\projects\python\pingregisters\subprocess.py:73 hosts = multiping(['10.0.0.5', '127.0.0.1', '::1']) File J:\ProgramData\Anaconda3\Lib\site-packages\icmplib\multiping.py:267 in multiping return asyncio.run( File J:\ProgramData\Anaconda3\Lib\asyncio\runners.py:186 in run raise RuntimeError( RuntimeError: asyncio.run() cannot be called from a running event loop



RE: Need some guidance on a script to ping a list of ip's - deanhystad - Aug-09-2023

Post your code.


RE: Need some guidance on a script to ping a list of ip's - cubangt - Aug-09-2023

What code is there to post, the original post at the top has my code. There is no new code to post or share... The sample code i tried was this:

from icmplib import multiping

hosts = multiping(['10.0.0.5', '127.0.0.1', '::1'])
for host in hosts:
     if host.is_alive:
         # See the Host class for details
         print(f'{host.address} is up!')
     else:
         print(f'{host.address} is down!')



RE: Need some guidance on a script to ping a list of ip's - deanhystad - Aug-09-2023

I wouldn't expect that to use asyncio. According to the home page it looks like there is a multiping and an async_multiping.
Quote:Import basic functions

from icmplib import ping, multiping, traceroute, resolve
Import asynchronous functions

from icmplib import async_ping, async_multiping, async_resolve
I would expect async_multiping to use asyncio and multiping to not. Instead they just use different asyncio functions. Try using async_multiping. It doesn't use asyncio.run().

From the source:
    Usage::

        >>> import asyncio
        >>> from icmplib import async_multiping
        >>> hosts = asyncio.run(async_multiping(['10.0.0.5', '::1']))

        >>> for host in hosts:
        ...     if host.is_alive:
        ...         print(f'{host.address} is up!')
        ...     else:
        ...         print(f'{host.address} is down!')

        10.0.0.5 is down!
        ::1 is up!



RE: Need some guidance on a script to ping a list of ip's - cubangt - Aug-09-2023

I will give it a try this evening. Yea in order to test it out on my instance, i literally just copied their example and provided 1 valid IP and one invalid to make sure it worked as expected and instantly received that error.

Ill try today and post back my results and anything else if i do receive an error.

thank you


RE: Need some guidance on a script to ping a list of ip's - cubangt - Aug-10-2023

I think im confused on the suggested code to try.

This is the code i currently have in my script and nothing else when i receive the above error.

from icmplib import multiping

hosts = multiping(['10.0.0.5', '127.0.0.1', '::1'])

for host in hosts:
     if host.is_alive:
         # See the Host class for details
         print(f'{host.address} is up!')
     else:
         print(f'{host.address} is down!')
I also tried just this basic example from the website and received the same error:

import asyncio
from icmplib import async_multiping
hosts = asyncio.run(async_multiping(['10.0.0.5', '::1']))
 
for host in hosts:
      if host.is_alive:
        print(f'{host.address} is up!')
      else:
        print(f'{host.address} is down!')



RE: Need some guidance on a script to ping a list of ip's - deanhystad - Aug-10-2023

Are you running from Spyder? Try running from the command line. I would test this if I used Anaconda, but I'm avoiding that for now.