Search code examples
linuxbashtelnetxinetd

bash script user input with xinetd


Doing some exercises / tutorials with xinetd. I setup the script below as a xinetd controlled service.

#!/bin/bash

str1="password"
printf 'Hello...'
printf '%(%Y-%m-%d)T\n' -1

# Prompt for password that will be compared with str1.
echo "Please enter a password"
read str2

# compare strings
if [ "$str1" = "$str2" ]; then
    echo "right"
else
    echo "wrong"
fi

Created file "check" with the below content in /etc/xinetd.d directory.

service check
{
    port = 41200
    server = /tmp/script.sh
    user = root
    socket_type = stream
    protocol = tcp
    wait = no
    only_from = 192.168.0.1
}

I added the following line in the file /etc/services: check 41200/tcp

The script works fine as expected locally. When user enters "password", response is "right" and "wrong" when anything else is entered. However, it doesn't seem to work when a remote host connects via telnet. Always get the response "wrong".

Is there something I am missing here?


Solution

  • telnet uses DOS line endings. Try stripping off a trailing carriage-return from str2 before comparing it with str1.

    str2=${str2%$'\r'}