├── LICENSE ├── README.md └── udpping.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UDPping 2 | ping with UDP protocol 🛠 3 | 4 | # How it looks like 5 | ``` 6 | root@raspberrypi:~# ./udpping.py 44.55.66.77 4000 7 | UDPping 44.55.66.77 via port 4000 with 64 bytes of payload 8 | Reply from 44.55.66.77 seq=0 time=138.357 ms 9 | Reply from 44.55.66.77 seq=1 time=128.062 ms 10 | Request timed out 11 | Reply from 44.55.66.77 seq=3 time=136.370 ms 12 | Reply from 44.55.66.77 seq=4 time=140.743 ms 13 | Request timed out 14 | Reply from 44.55.66.77 seq=6 time=143.438 ms 15 | Reply from 44.55.66.77 seq=7 time=142.684 ms 16 | Reply from 44.55.66.77 seq=8 time=138.871 ms 17 | Reply from 44.55.66.77 seq=9 time=138.990 ms 18 | ^C 19 | --- ping statistics --- 20 | 10 packets transmitted, 8 received, 20.00% packet loss 21 | rtt min/avg/max = 128.06/138.44/143.44 ms 22 | ``` 23 | 24 | # Getting Started 25 | 26 | ### Step 1 27 | 28 | Set up a udp echo server at the host you want to ping. 29 | 30 | There are many ways of doing this, my favourite way is: 31 | 32 | ``` 33 | socat -v UDP-LISTEN:4000,fork PIPE 34 | ``` 35 | 36 | Now a echo server is listening at port 4000. 37 | 38 | ###### Note 39 | If you dont have socat, use `apt install socat` or `yum install socat`, you will get it. 40 | 41 | ### Step 2 42 | 43 | Ping you server. 44 | 45 | Assume `44.55.66.77` is the IP of your server. 46 | 47 | ``` 48 | ./udpping.py 44.55.66.77 4000 49 | ``` 50 | 51 | Done! 52 | 53 | Now UDPping will generate outputs as a normal ping, but the protocol used is `UDP` instead of `ICMP`. 54 | 55 | # Advanced Usage 56 | ``` 57 | root@raspberrypi:~# ./udpping.py 58 | usage: 59 | this_program 60 | this_program "" 61 | 62 | options: 63 | LEN the length of payload, unit:byte 64 | INTERVAL the seconds waited between sending each packet, as well as the timeout for reply packet, unit: ms 65 | 66 | examples: 67 | ./udpping.py 44.55.66.77 4000 68 | ./udpping.py 44.55.66.77 4000 "LEN=400;INTERVAL=2000" 69 | ./udpping.py fe80::5400:ff:aabb:ccdd 4000 70 | 71 | ``` 72 | -------------------------------------------------------------------------------- /udpping.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from __future__ import print_function 4 | 5 | import socket 6 | import sys 7 | import time 8 | import string 9 | import random 10 | import signal 11 | import sys 12 | import os 13 | 14 | INTERVAL = 1000 #unit ms 15 | LEN =64 16 | IP="" 17 | PORT=0 18 | 19 | count=0 20 | count_of_received=0 21 | rtt_sum=0.0 22 | rtt_min=99999999.0 23 | rtt_max=0.0 24 | 25 | def signal_handler(signal, frame): 26 | if count!=0 and count_of_received!=0: 27 | print('') 28 | print('--- ping statistics ---') 29 | if count!=0: 30 | print('%d packets transmitted, %d received, %.2f%% packet loss'%(count,count_of_received, (count-count_of_received)*100.0/count)) 31 | if count_of_received!=0: 32 | print('rtt min/avg/max = %.2f/%.2f/%.2f ms'%(rtt_min,rtt_sum/count_of_received,rtt_max)) 33 | os._exit(0) 34 | 35 | def random_string(length): 36 | return ''.join(random.choice(string.ascii_letters+ string.digits ) for m in range(length)) 37 | 38 | if len(sys.argv) != 3 and len(sys.argv)!=4 : 39 | print(""" usage:""") 40 | print(""" this_program """) 41 | print(""" this_program "" """) 42 | 43 | print() 44 | print(""" options:""") 45 | print(""" LEN the length of payload, unit:byte""") 46 | print(""" INTERVAL the seconds waited between sending each packet, as well as the timeout for reply packet, unit: ms""") 47 | 48 | print() 49 | print(" examples:") 50 | print(" ./udpping.py 44.55.66.77 4000") 51 | print(' ./udpping.py 44.55.66.77 4000 "LEN=400;INTERVAL=2000"') 52 | print(" ./udpping.py fe80::5400:ff:aabb:ccdd 4000") 53 | print() 54 | 55 | exit() 56 | 57 | IP=sys.argv[1] 58 | PORT=int(sys.argv[2]) 59 | 60 | is_ipv6=0; 61 | 62 | if IP.find(":")!=-1: 63 | is_ipv6=1; 64 | 65 | if len(sys.argv)==4: 66 | exec(sys.argv[3]) 67 | 68 | if LEN<5: 69 | print("LEN must be >=5") 70 | exit() 71 | if INTERVAL<50: 72 | print("INTERVAL must be >=50") 73 | exit() 74 | 75 | signal.signal(signal.SIGINT, signal_handler) 76 | 77 | if not is_ipv6: 78 | sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 79 | else: 80 | sock = socket.socket(socket.AF_INET6,socket.SOCK_DGRAM) 81 | 82 | print("UDPping %s via port %d with %d bytes of payload"% (IP,PORT,LEN)) 83 | sys.stdout.flush() 84 | 85 | while True: 86 | payload= random_string(LEN) 87 | sock.sendto(payload.encode(), (IP, PORT)) 88 | time_of_send=time.time() 89 | deadline = time.time() + INTERVAL/1000.0 90 | received=0 91 | rtt=0.0 92 | 93 | while True: 94 | timeout=deadline - time.time() 95 | if timeout <0: 96 | break 97 | #print "timeout=",timeout 98 | sock.settimeout(timeout); 99 | try: 100 | recv_data,addr = sock.recvfrom(65536) 101 | if recv_data== payload.encode() and addr[0]==IP and addr[1]==PORT: 102 | rtt=((time.time()-time_of_send)*1000) 103 | print("Reply from",IP,"seq=%d"%count, "time=%.2f"%(rtt),"ms") 104 | sys.stdout.flush() 105 | received=1 106 | break 107 | except socket.timeout: 108 | break 109 | except : 110 | pass 111 | count+= 1 112 | if received==1: 113 | count_of_received+=1 114 | rtt_sum+=rtt 115 | rtt_max=max(rtt_max,rtt) 116 | rtt_min=min(rtt_min,rtt) 117 | else: 118 | print("Request timed out") 119 | sys.stdout.flush() 120 | 121 | time_remaining=deadline-time.time() 122 | if(time_remaining>0): 123 | time.sleep(time_remaining) 124 | 125 | --------------------------------------------------------------------------------