Python Forum
Need some guidance on a script to ping a list of ip's
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some guidance on a script to ping a list of ip's
#1
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?
Reply
#2
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/
Reply
#3
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.
Reply
#4
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
Reply
#5
Post your code.
Reply
#6
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!')
Reply
#7
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!
Reply
#8
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
Reply
#9
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!')
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  non-stop ping script kucingkembar 1 1,370 Aug-23-2022, 06:29 AM
Last Post: menator01
  Need some coding guidance for a task peny 5 2,197 Sep-27-2021, 02:02 PM
Last Post: peny
  Win32\ping.exe windows pops up -very annoying... tester_V 9 3,235 Aug-12-2021, 06:54 AM
Last Post: tester_V
  Looking for discord bot to make loop ping for address ip tinkode 0 1,836 Jul-26-2021, 03:51 PM
Last Post: tinkode
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 3,619 May-17-2021, 11:29 AM
Last Post: throwaway34
  Ping command using python 3.6.5 Martin2998 6 17,439 Apr-19-2021, 06:24 PM
Last Post: blazejwiecha
  Your Guidance caslor 1 2,147 Mar-28-2021, 09:34 PM
Last Post: Larz60+
  GPIO high if network IP has good ping duckredbeard 3 2,351 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  Noob needing guidance.... bako 0 1,868 Mar-29-2020, 06:55 PM
Last Post: bako
  Create a program that PING a list of IPs skaailet 7 6,381 Mar-26-2020, 10:46 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020