27

I'm trying to send a broadcast message using netcat.

I have firewalls open and sending a regular message like this works for me:

  host: nc -l 192.168.1.121 12101
  client: echo "hello" | nc 192.168.1.121 12100

But I can't get something like this to work.

  host: nc -lu 0.0.0.0 12101
  client: echo "hello" | nc -u 255.255.255.255 12100

Am I using the right flags? Note, the host is on Mac and the client on Linux. Can you give me an example that works for broadcasting a message?

Thanks!

Edwin Evans
  • 2,726
  • 5
  • 34
  • 47
  • 1
    I came here trying to get busybox nc to do a UDP broadcast. With the -v option it told me that broadcasts were not allowed. Once I added `setsockopt_broadcast(netfd);` before the `connect_w_timeout(netfd);` call everything worked. – Keeely May 01 '20 at 14:11

4 Answers4

24

The GNU version of netcat might be broken. (I can't get to work under 0.7.1 anyway.) See http://sourceforge.net/p/netcat/bugs/8/

I've gotten socat to work. Code below does UDP broadcast to port 24000.

socat - UDP-DATAGRAM:255.255.255.255:24000,broadcast

(In socat-world "-" means "stdin".)

Robert Calhoun
  • 4,823
  • 1
  • 38
  • 34
9

You're not saying you want to broadcast, which is done using the -b option to nc/netcat.

nc -h 2>&1 | grep -- -b
-b          allow broadcasts
fork2execve
  • 1,561
  • 11
  • 16
  • 1
    Which version of netcat are you using? Neither macOS nc not the busybos one, have this flag... – Christian Ulbrich Sep 03 '18 at 22:30
  • The standard one on Linux, which is what the requestor asked for. macOS is its usual quirky self. busybox contains a very rudimentary subset of all utilities. – fork2execve Sep 05 '18 at 07:53
  • @fork2execve There is no “standard” `nc`. Linux has one version, BSD has another. Neither is quirky. – fuz Aug 02 '19 at 20:28
6

A simple example that works on Ubuntu. All the info in is in the other answers, but I had to piece it together, so thought I would share the result.

server

nc -luk 12101

client

echo -n "test data" | nc -u -b 255.255.255.255 12101

The client will hang until you do Ctrl-C

lordvlad
  • 5,200
  • 1
  • 24
  • 44
masebase
  • 4,915
  • 3
  • 24
  • 20
3

Sorry, if I am assuming wrong but you mentioned that you have your firewalls set up correctly so I am guessing that the host and client are not on the same subnet???

If that is the case and this firewall is also acting also as a router (or if the packet has to go through a router) then it is going to process that packet but it will not forward it out its other interfaces. If you wanted that to happen then you would need to send a directed broadcast. For example; for the subnet 192.168.1.0/24 the directed broadcast would be 192.168.1.255, the last IP in the subnet. Then the firewall, assuming it had a route to 192.168.1.0/24 and that it is set up to forward directed broadcast, would forward that broadcast out to the destination or next hop. Configuring your device to forward directed broadcast... you would need to reference its documentation. For Cisco IOS you would type in, under the interface, "ip directed-broadcast".

255.255.255.255 is a limited broadcast and is not going to get pass your routers regardless, it is solely intended for the layer 2 link that it resides.

As for how netcat is set up:

-l 0.0.0.0 12101, tells netcat to listen on port 12101 on all interfaces that are up and with an IP address assigned. The -u is not needed as it is telling netcat to listen on a unix domain socket, google IPC :) (this is the biggest reason that your scenario is not working.)

The below should work to get a broadcast forwarded to another network via netcat:

server: nc -l 0.0.0.0 12101
host: echo "hello" | nc 192.168.1.255 12101

Hope that helps, sorry if that was long winded or off from what you were looking for :)

matak
  • 173
  • 1
  • 8