views:

67

answers:

3

I would like to add the ability for users to telnet into my app where by they can type in commands etc. I would like a simple Java Telnet server whereby i can provide the authentication strategy.

By "telnet" i am not referring what lib provides telnet connectivity but something a little more. I want the boring protocol stuff done along with a shell that has hooks that i can use to process and execute command lines. Where or what these commands are, is of course my implementation.

This q is not about how to start a socket server or other protocol plumbing type.

A: 

Telnet servers only do telnet.

You might find http://telnetd.sourceforge.net/ suitable.

"Target Audience: Developers that want to integrate reasonable telnet access into their application."

Thorbjørn Ravn Andersen
I want all the protocols available in one library, so users dont have to fiddle with lots of separate things.
mP
To the best of my knowledge no such thing exist.
Thorbjørn Ravn Andersen
+1  A: 

In my experience, telnet is telnet, ssh is ssh... if you want the latter then you might look at something like: http://mina.apache.org/sshd/

I've never used it, though.

You could also use the native operating system SSH (if it has it) to tunnel and then run regular telnet on the server. Creating your own telnet access isn't at all complicated if it's just a custom command shell. And as the other poster mentions, there are libraries that make it easy.

...but ssh would be more secure and more straight-forward.

PSpeed
I want ssh,telnet etc in one java library, so users dont have to fiddle with ssh separately etc. Mina if i recall only does plain old vanilla telnet.
mP
This ssh was just folded into the "mina" project as it's built in the underlying mini communication API, the description from that link: "Apache SSHD is a 100% pure java library to support the SSH protocols on both the client and server side." From my perspective, telnet != ssh... they are as different as ftp and http. That links looks to point to an actual ssh implementation.
PSpeed
Are you trying to support telnet and ssh at the same time, ie: a user can telnet in or a user can ssh in? Why support ssh at all then?
PSpeed
A: 

What exactly do you mean by "telnet into my app where by they can type in commands etc."?

Since both telnet and ssh requires some application to run on the remote machine (most often an instance of a command shell), they really only provide the transport mechanism for the commands. Telnet in particular can be (and has been) abused to send general text-commands over a tcp connection. You can browse the web by using the command telnet www.target-domain.com 80 and manually enter all the http protocol stuff, but i wouldn't recommend it. ssh is just the same, although it adds ssl/tls security to the channel.

Therefore, what I guess you want is something like this:

import java.io.*;
import java.net.*;

public class telnettest {
    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            //Open a listening socket on port 8022 and wait for a connection
            echoSocket = new ServerSocket(8022).accept();
            System.out.println("connection established");

            //Get input and output streams
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                        echoSocket.getInputStream()));
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                    + "the connection");
            System.exit(1);
        }

        //Read lines from the input stream (corresponding to user commands)
        String userInput;
        while ((userInput = in.readLine()) != null) {

            //For each line entered, just output it to both remote and local terminal
            out.println("echo: " + userInput);
            System.out.println("echo: " + userInput);
        }

        //Clean up connections.
        out.close();
        in.close();
        echoSocket.close();
    }
}

which is a shameless edit of this tutorial example.

I'm not sure about ssh login, but I suspect you could get there using javax.net.ssl.SSLServerSocket instead of ServerSocket.

What remains is to do something sensible with the user input, instead of just throwing it back in their faces. Depeding on the amount of commands and their parameters, you can either do the command parsing yourself, or find a library that handles it for you.

Peter
mP
Think of what and why one uses telnet on a *nix box. Telnet lets one login to a shell and do stuff. Sorry the original q was more about the first bit the low level connectivity rather than the higher level stuff that the user actually uses which is built on top of telnet.
mP