Reputation: 521
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.
Upvotes: 9
Views: 19583
Reputation: 6782
See options in your telnet client: on an arbitrary listening socket and hit escape]
to enter the client.
telnet> help
telnet> set ?
Upvotes: 0
Reputation: 74094
Download Putty source code. Examine TELNET.C, the Telnet backend for Putty.
Upvotes: 2
Reputation: 32635
Check out the source code here. It has helped me out a lot in understanding Telnet protocol.
Upvotes: 0
Reputation: 12382
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
Upvotes: 3
Reputation: 3663
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.
Upvotes: 1
Reputation: 2413
I really find Beej's Guide to Network Programming a good introduction to network programming in C.
Upvotes: 2
Reputation: 10536
If Python is an option you could use telnetlib.
#!/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()
Upvotes: 10
Reputation: 73081
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.
Upvotes: 4