Netcat Python and UDP

Netcat

Netcat usually written as nc on most Linux systems, if often desctibed as the Swiss army knife of packet operations. I have been having a little trouble finding something... so this mini guide is to try and help me remember.

Data

My data is being generated by a Python program, and I am sending the data as UDP - on a broardcast.

Why ?

I would like the possibility of multiple clients to connect to the stream - and each client may perform seperate functions on the data stream.

Setup

My development enrinoment is as follows

  • Mac Book Pro i5 and i7
  • OS 10.10.3
  • Python 3.3.4 - in Venv
  • nc - 0.7.1
  • ip address is 192.168.1.215
  • broardcast address of 192.168.1.255

Code Example

This is my Python Broardcast class - nothing too special here.

Note: This is Python3 format code - due to the bytes string formatting.

# Send UDP broadcast packets
import sys, time
from socket import *

class broardcast:

    def __init__(self):
        self.s = socket(AF_INET, SOCK_DGRAM)
        self.s.bind(('192.168.1.215', 0))
        self.s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        self.MYPORT = 50000


    def send(self,data):
        print (".")
        self.s.sendto(bytes(data,'UTF-8'),("192.168.1.255", self.MYPORT))

if __name__ == "__main__":
    bc=broardcast()
    for i in range(50):
        bc.send(str.format("Data{0}",i))

Testing

Again - nothing difficult....

    python broardcast.py

So where is the issue ?

I want to verify what this is producing..

Python Client

This is the Python Receiver for the Broardcast


import socket

MCAST_GRP = '192.168.1.255'
MCAST_PORT = 50000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(('', MCAST_PORT))
while True:
  print (" "+str(sock.recv(10240)))

Other receivers

But .. these are rather heavy - I want to view this with netcat.

I can see these packets

  • Python Client
  • wireshark
  • tshark
  • netcat

I have verified the data using the first 3 - but I want to be able to see the data with netcat.

Netcat

Despite many warning about nc (netcat) not working on Mac - I have had no issues so far.

My data is being generated by

    for a in $(seq 10)
    do
         echo "Loop $a running"
         python broardcast.py
         sleep 5s
    done 
    
The netcat command to receive

* UDP
* Broardcast

is

``` bash
   nc -l -s 192.168.1.255 -p 50000 -u 
   

command line args

  • -s Source 192.168.1.255 the broardcast address
  • -p Port 50000, a non priv-port
  • -l Listen

You should see output like

Data0Data1Data2Data3Data4Data5Data6Data7Data8Data9Data10Data11Data12Data13Data14Data15Data16Data17Data18Data19Data20Data21Data22Data23Data24Data25Data26Data27Data28Data29Data30Data31Data32Data33Data34Data35Data36Data37Data38Data39Data40Data41Data42Data43Data44Data45Data46Data47Data48Data49

Which is the data without line sperators

Whats next ?

Apache Spark real time processing..... using yep the network data described above.