Skip to content

Instantly share code, notes, and snippets.

@hirman74
Forked from Symeon-Carle/ping.py
Last active May 7, 2023 08:03
Show Gist options
  • Save hirman74/1f6a6b1abeddd6cbe66e4191b3808c51 to your computer and use it in GitHub Desktop.
Save hirman74/1f6a6b1abeddd6cbe66e4191b3808c51 to your computer and use it in GitHub Desktop.
async ping for python 3 on Windows
import asyncio
WaitTimeOut = 600 # -w milliseconds
PingProbe = 10 # -n
PingSize = 65500 # -l
## for Mac 'macosx'
## PingSize = 8184 # -s plus 8 bytes of ICMP header
ParallelPing = 100 # Concurrent ping stream
sendIP = ['192.168.10.1'] # Peer IP address
list_machines = sendIP * ParallelPing
# Code to flood the interface with ping and measure bandwidth at the link pipe
async def ping(addressIP, indexes, table):
cmdArray = ['ping', '-w', str(WaitTimeOut), '-n', str(PingProbe), '-l', str(PingSize), addressIP]
## for Mac 'macosx'
## cmdArray = ['ping', '-W', str(WaitTimeOut), '-c', str(PingProbe), '-s', str(PingSize), addressIP]
sendCMD = " ".join(cmdArray)
proc = await asyncio.create_subprocess_shell(sendCMD, stdin=asyncio.subprocess.PIPE , stdout=asyncio.subprocess.PIPE, stderr = asyncio.subprocess.PIPE, shell=True)
data, _ = await proc.communicate()
print(data)
return
if __name__ == '__main__':
loop = asyncio.ProactorEventLoop()
## for Mac 'macosx'
## loop = asyncio.SelectorEventLoop()
asyncio.set_event_loop(loop)
indexes = 0
table = []
eachAsync = []
for computer in list_machines:
table.append([computer.strip(),""])
eachAsync.append(asyncio.ensure_future(ping(computer.strip(),indexes,table)))
indexes += 1
loop.run_until_complete(asyncio.wait(eachAsync))
print("\nDONE !")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment