Skip to content
Advertisement

Simplest way to script slowly sending data through a socket connection

I need to periodically connect to a server from a Linux client and send some commands, so I wrote this script:

#!/bin/sh
telnet theserver.somewhere.com 9999 <<EOF
command 1
command 2
command 3
EOF

The problem is that I need to wait between commands. This script sends the commands and disconnects too quickly, which the server detects, and drops the commands. It works if I open the connection from the command line and then paste the commands one at a time, and it works from non-local clients, but from local clients the script fails. It would be enough to just pause for a second or so between each command.

I can write a C or Java program that does this, but what is the simplest way? The usual scripting languages (Perl and Python) are available, if you need them.

Advertisement

Answer

Shell commands to echo a line at a time from a file to telnet:

cat commands.txt | 
    ( while read line; do echo $line; sleep 1; done ) |
    telnet theserver.somewhere.com 9999
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement