Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a telnet client

Tags:

python

c

telnet

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.

like image 610
MAC Avatar asked Mar 25 '10 21:03

MAC


1 Answers

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()
like image 171
compie Avatar answered Nov 15 '22 15:11

compie