4

I have a need for a Linux system to listen on a TCP port and dump what it receives to a file and/or stdout. The system does not have netcat installed. I have root permissions, but am not allowed to add new applications to the system for compliance reasons. Is it possible to do this with a simple bash, perl, or python script? The simpler, the better.

1
  • 1
    do you have tcpdump available? tcpdump -vv -x -X -s 1500 -i eth1 'port 25' -w /somewhere/dump.txt
    – cybernard
    Jul 19, 2014 at 1:35

1 Answer 1

3

To open a random port using Python:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 0))

To find out which port you have opened:

s.getsockname()[1]

To close the port:

s.close()

For more information, read the documentation for the socket module, the socket how-to, or a socket tutorial.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .