tags:

views:

695

answers:

9

HI,

I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.

I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.

What would be a good way to proceed. I tried writing a simple socket program but it stops at the login prompt. My guess is that i am not following the TCP protocol.

Has anyone attempted this, is there an opensource library out there to do this?

Thanks

Addition: Eventually i wish to expose it through a web api/webservice. The platform is linux.

+7  A: 

If Python is an option you could use telnetlib.

Code example:

#!/usr/bin/env python
import getpass
import sys
import telnetlib

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()
compie
At the moment this seems to be the most convenient way forward.I tired a a quick script by reading the online documentation and it works!Now all i have to do is learn python which doesnt seem that hard!I wonder if i can communicated between python and other C programs maybe?
MAC
Why in python?I don't get it.
systempuntoout
+2  A: 

This might be useful reading for you.

Nate Bross
Thanks that was helpful too!
MAC
+2  A: 

Download Putty source code and look for TELNET.C file, it's the telnet backend for Putty.

systempuntoout
Studying Putty would be like reading the technical specifications for an internal combustion engine in effort to understand how to drive. What MAC needs is a simple example of sockets and the Telnet protocol.
Dave Jarvis
You are definitively right and i agree with you but, as you well know, Putty is THE telnet client and its source code is one of the best reference out there :).
systempuntoout
@systempuntoout I would rather say Putty is THE **SSH** client **for Windows**.
Tiberiu Ana
@Tiberiu I would rather say Putty is THE Telnet, ssh, rlogin client for Windows but, on putty page you got source code for unix too ( platform requested by the OP)
systempuntoout
+2  A: 

telnet's protocol is pretty straightforward... you just create a TCP connection, and send and receive ASCII data. That's pretty much it.

So all you really need to do is create a program that connects via TCP, then reads characters from the TCP socket and parses it to update the GUI, and/or writes characters to the socket in response to the user manipulating controls in the GUI.

How you would implement that depends a lot on what software you are using to construct your interface. On the TCP side, a simple event loop around select() would be sufficient.

Jeremy Friesner
True, i tried some but was running into problems. So i was hoping to find a pre-built library.
MAC
wrong, the real telnet protocol is way more complicated (teerminal type, option negociation, etc... see [RFC 854](http://www.rfc-editor.org/rfc/rfc854.txt)). but the vast majority of poorly implemented telnet clients are simple ASCII socket.
Adrien Plisson
Good point; I don't think that MAC needs to worry about those wrinkles for his purpose, however.
Jeremy Friesner
+1  A: 

Take a look at expect. You might not even need to write a C program:

These use the Telnet protocol:

I will post a simple C-based Telnet application shortly.

Dave Jarvis
Hmm Expect sounds interesting! But for now i am exploring using python.
MAC
Let me know if you still would like the C-based application.
Dave Jarvis
+1  A: 

I really find Beej's Guide to Network Programming a good introduction to network programming in C.

Frank
+1  A: 

Unless the application is trivial, a better starting point would be to figure out how you're going to create the GUI. This is a bigger question and will have more impact on your project than how exactly you telnet into the device. You mention C at first, but then start talking about Python, which makes me believe you are relatively flexible in the matter.

Once you are set on a language/platform, then look for a telnet library -- you should find something reasonable already implemented.

Tiberiu Ana
That is a reasonable assessment!The application has to have two parts, one is a central server that will actually be doing all the controlling and an iPhone based UI application that users will use to make changes.Personally I am experienced with microsoft technologies, but i have to do this using free/opensource technologies. Hence i am at somewhat of a loss as to where to begin.I need to expose this api in a manner that can be consumed by a iphone application. In my mind that is something like a webservice. I am still not sure how i go about making one without .net =)
MAC
+1  A: 

While telnet is almost just a socket tied to a terminal it's not quite. I believe that there can be some control characters that get passed shortly after the connection is made. If your device is sending some unexpected control data then it may be confusing your program. If you haven't already, go download wireshark (or tshark or tcpdump) and monitor your connection. Wireshark (formerly ethereal) is cross platform and pretty easy to use for simple stuff. Filter with tcp.port == 23

nategoose
A: 

Check out the source code here. It has helped me out a lot in understanding Telnet protocol.

ShaChris23