49

I am aware that this is not a direct 'development' question but I need this info to test a development project, so I think someone could've hit similar problem.

I will test a software that runs a TCP server and according to sent commands replies some answers. I will test the software and do not want to write code if it doesn't work well. So I want to send those commands and test drive the server software.

How can I achieve this with a Linux box?

3 Answers 3

69

From bash with dd:

dd if=/dev/zero bs=9000 count=1000 > /dev/tcp/$target_host/$port

or even with cat:

cat < /dev/urandom > /dev/tcp/$target_host/$port
4
  • 15
    /dev/tcp/*/* is a special filename that bash handles in a special way. It doesn't actually exist.
    – Flimm
    Commented Nov 14, 2012 at 15:45
  • (echo '2' > /dev/tcp/192.168.1.161/8888) && (sleep 1; echo '2' > /dev/tcp/192.168.1.163/8888) why more then 1 takes 5 minutes but when used 1 request then less then 1 second its done?
    – user285594
    Commented Apr 13, 2014 at 22:10
  • While nc is possibly the best tool, this should be the accepted answer.
    – Arj
    Commented Jan 18, 2017 at 16:01
  • 2
    Just a sidenote: With this, the response from the server will not be displayed. If you want to see the response on the screen, netcat fits better. Commented May 9, 2017 at 11:40
53

netcat or telnet, I have used both in the past to test simple text based protocols. netcat is more flexible.

1
  • @YannRamin how do I use socat to send a TCP message? I previously could send a UDP message with socat but socat doesn't have tcp-datagram. This is for UDP: echo "some-test-sample" | socat - UDP-DATAGRAM:localhost:43278,broadcast
    – Katie
    Commented Mar 23, 2018 at 18:37
0

Sounds like Expect may be what you want. There are implementations for multiple scripting languages, and you can script and assert the requests/server responses plus appropriate timeouts, error handling etc.

2
  • 4
    Please show some example, its rare to find someone who understand the expect properly for multiple TCP connection. I have an issue when i do single connect it works but when it comes multi connection it takes more then normal duration. Please show some example how can you do this with expect? (echo '2' > /dev/tcp/192.168.1.161/8888) && (sleep 1; echo '2' > /dev/tcp/192.168.1.163/8888)
    – user285594
    Commented Apr 13, 2014 at 22:15
  • 7
    I'm surprised this gets downvoted, when the top voted and accepted answer recommends telnet to "test simple text based protocols" Commented Dec 28, 2015 at 13:11