Created
September 22, 2021 14:33
-
-
Save fagci/d001ae445a7a43a80f1b24383f6314ed to your computer and use it in GitHub Desktop.
ICMP ping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from socket import AF_INET, IPPROTO_ICMP, SOCK_DGRAM, gethostbyname, socket | |
from struct import pack | |
from time import sleep, time | |
def main(): | |
seq = 1 | |
with socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP) as s: | |
s.settimeout(1) | |
ip = gethostbyname('google.com') | |
while True: | |
header = pack('bbHHh', 8, 0, 0, 0, seq) | |
try: | |
t = time() | |
s.sendto(header, (ip, 0)) | |
s.recvfrom(64) | |
print(round((time() - t) * 1000), 'ms') | |
except Exception as e: | |
print(e) | |
seq += 1 | |
sleep(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment