2

I've created a simple shell script named /usr/sbin/helloworld

#!/bin/bash
echo "Hello World"
mkdir ~/itran
read -p "Say Hello : " hello
echo "$hello"
exit 0

I created /etc/xinetd.d/testservice-map as below

service testservice
{
    port            = 4079
    socket_type     = stream
    protocol        = tcp
    wait            = no
    user            = root
    server          = /usr/sbin/helloworld
    server_args     = test
}

and added the line below in /etc/services

testservice 4079/tcp

I've assigned one IP to my host and added an IPTABLE rule to redirect the request on port 22 of that IP to port 4079 of localhost

Now when I SSH to that IP using putty from another machine, from the logs (/var/log/messages) I can see that my service is being executed on the host but I can't see the "Hello World" message. The directory "itran" gets created but I can't see the echo message nor can I input any value.

I'm new to this. Kindly let me know if any more information is needed. Any help would be much appreciated.

1 Answers1

1

The problem that you have is that ssh does a lot more than you expect.

What happens is:

  • you start ssh
  • your ssh client sends an initiation of the session
  • your script launches and sends "Hello world"
  • your ssh client expects a server public key, but gets the hello world
  • You ssh-client terminates

With some verbose-options on your ssh-client, you can probably observe this behaviour.

Now, other clients do not have a first protocol handshake. If you have a telnet-client, you will probably get your expected result with

telnet 127.0.0.1 4079

Otherwise, try nc.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • Is there any way I can use SSH? Can I make my script do the handshake, accept the username and password and then display something? Is there a library for that? – Jafari Sharib Sep 05 '18 at 09:52
  • 1
    There are C-libraries, Python's Twisted seems to do something like that, but don't go there. It is almost certainly not what you want.The result will not be a simple script like you produced. – Ljm Dullaart Sep 05 '18 at 11:59