Monitoring a given port for unknown incoming data

926 views Asked by At

I am testing a device (a GPS tracking) that is sending data (the GPS position) to a given IP on a given port. I don't know what kind of data format it sends, is there a way to listen on my CentOS server for incoming data on a given port and maybe print it on screen ?

2

There are 2 answers

6
Floris On BEST ANSWER

Expanding on the comment I left earlier (at the time I could not test): if you have access to Python on your machine, you can create the following simple script - e.g. in a file you call myServer.py (attribution: http://ilab.cs.byu.edu/python/socket/echoserver.html - you will find much more information about Python network programming there; well worth a visit):

#!/usr/bin/env python
import socket
host = ''       # your IP address
port = 50000    # pick a port you want to listen on. 
backlog = 5     # number of clients you could handle at once
size = 1024     # max size of data packet

# create socket:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to host / port:
s.bind((host,port))
# start listening:
s.listen(backlog)

# stay in this loop until ctrl-c is typed at console:
while 1:
    client, address = s.accept()
    data = client.recv(size)
    if data:
        print data          # echo data to console
        client.send(data)   # echo data to client (may confuse your application, great for browser)
    client.close()

When I ran this on my Mac by typing

chmod 755 myServer.py       # make it executable
./myServer                  # run the script

And then went to my browser where I typed

http://localhost:50000/?hello=world

I got the following in my browser:

GET /?hello=world HTTP/1.1
Host: localhost:50000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:25.0) Gecko/20100101 Firefox/25.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

And my console looked like this (I hit ctrl-c after servicing the request):

[myMac:~myCodePath] myName% ./myServer.py
GET /?hello=world HTTP/1.1
Host: localhost:50000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:25.0) Gecko/20100101 Firefox/25.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive


^CTraceback (most recent call last):
  File "./srv.py", line 13, in <module>
    client, address = s.accept()
  File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/socket.py", line 202, in accept
    sock, addr = self._sock.accept()
KeyboardInterrupt

I think this should be a reasonable way for you to take a look at the traffic from the GPS. It should be easy to see how you can change the response back to the client - right now it's just the request being echoed, but you can obviously send anything with

client.send('anything')

Let me know if you have any further questions about this. Note I used port 50000 - I was guessing it would be available. The way my machine was set up I could not use port 80 (the default html port) but that's mostly a question of firewall configuration etc.

0
adrianTNT On

I found a program/command netcat that seems to do what I needed. I installed it with Yum, then I can type this to listen on port 333 for example:

nc -l 333

And it constantly prints incoming data on port 333.