Bash Script with Inetd

Okay, the following Solution is out there somewhere. But since it took me some time to Google it I thought i summarize it.

Following Problem:

I have a Linux System on which I want to execute a BASH Script over Telnet. So I used the help of the Inetd Service.

First I declared my own Service in /etc/services by adding the following line:

# Local services
myservice         4321/tcp

Then I make my Script known to inetd by adding the following Line to /etc/inetd.conf

myservice stream tcp nowait root /home/me/

and restart the service.

‘Til here it’s simple. But now comes the catch you will experience when you execute your script over the telnet connection. Since you’re not in a terminal enviroment, in which you’re when you use telnetd or sshd, there’s a different handling of Line endings. In a normal Unix Terminal a line end is represented by a simple \n aka. LF (Line Feed) aka 0x0A. But in TCP, as it’s used when you use inetd \r\n aka CR LF (Carriage Return, Line Feed) aka 0x0d 0x0a is used.

So what happens, is that if you read input into a variable the string is parsed until a \n occurs. So the \r is still in your string and fucks up most of your commands using this string. There are at least 2 Methods to get rid of this. The one i prefer is this one:

read foo
foo=`echo $foo | tr -d '\r'`

Which just kills all the \r in the string. It also enables you to use the script in terminal and non-terminal enviroments.  Second method would be to just cut off the last char. By which the Script becomes unusable in the normal shell.

read foo

In the output direction you’ll notice another effect of this behaviour. Since there is a \r expected where none is send you’ll experience this nice stair effect. So you have to replace the \n in all your output with \r\n to get this right. Most telnet clients like Putty on Windows have an option (Terminal -> “Implicit CR on LF”) to do this automatically. It’s  your choice how you solve this.

Please Leave a comment if this was of any use to you.


Bash Webserver using Inetd –

Discussion Thread with most of the solution –

Another Thread on the Topic –