hi
im trying to write a python script for a telnet client. So far im able to establish a connection with a server. My problem is that i want to print (at the client) all the activities at the server end. Let me put it this way.
Suppose i send a command "dir" from my client
at the server end it would list the contents....
i would like to print the same at my client.....
is there any way i can do this....
im not sure if the data being displayed at the server's end is being transmitted through the port.

my code is including below

import telnetlib
import time
import getpass

HOST = "192.168.36.186"
PORT = 23

class MyTelnetClient():
    def __init__(self,portNum=PORT,host=HOST):
        self.host = host
        self.portNum = portNum
        self.user = raw_input('Login :')        
        self.password = getpass.getpass('password:')
        self.tn = telnetlib.Telnet(self.host,self.portNum)
        self.tn.write(self.user+"\r")
        self.tn.write(self.password+"\r")

    def write(self,data):
        self.tn.write(data)
        print data
        
    def read(self):
        self.str = self.tn.read_all()
        return self.str   
    
    def close(self):
        self.tn.close
        
if __name__ == '__main__':
    TelnetClient = MyTelnetClient()
    print 'Connected to Server......'
    while 1:        
        data = str(raw_input('Telnet Client>'))
        TelnetClient.write(data+"\r")        
        if data == 'q':
            TelnetClient.tn.close()
            break
        else:
            datarx = TelnetClient.read()        
            print 'TCP Server>' + datarx        
    print 'Closing Connection with server...'

thanks alot in advance....

Recommended Answers

All 10 Replies

thanks a lot for the response....thing is im pretty new to python....and im not able to crack the method RunScript.
any assistance would help me to a great extend..

A little bit--- when I get more time I will do. (I have even got no Editor in my computer for developer)

def RunScript( self, script ):
		""" script is a list of strings """
                #Variable rBuffer is empty string
		rBuffer = ''
		#Iterate through script (See above), which is the list of strings
		for line in script:
			# cull out blank lines and comments -- self explanatory
                        #Test condition using OR condition -- Like OR gate in college 
			if (not line.strip()) or self.commentrx.search( line ): continue 
			
			m = self.parserx.search( line ).groups()
			if not m: raise 'Badly-formatted script line: %s' % line
			
			if m[1]:
			# If we find a % not followed by another %, apply formatting
				if self.fmtrx.search( m[1] ):
					arg = m[1] % self.__data
				else:
					arg = m[1]
				
				buff = apply( self.__functable[ m[0] ], arg )
			else:
				buff = apply( self.__functable[ m[0] ])
				
			if buff: rBuffer += buff
				
		return rBuffer

thank you very much....:)

you can point specifically where there is difficult and one can help.
I have no editor to write on

parserx = re.compile( '^\s*(\w+)(?:\s+)?(.*)' )
fmtrx = re.compile( '(%[^%])' )
commentrx = re.compile( '^\s+#' )

this is the first thing i cant understand....

im not sure how to explain my problem....let me try....
i have a telnet server running...im suppose to write a telnet client in python..which i have

import telnetlib
import getpass
import time

class MyTelnetClient():
    def __init__(self,portNum=PORT,host=HOST):
        self.host = host
        self.portNum = portNum
        self.user = username        
        self.password = password
        #self.tn = telnetlib.Telnet(self.host,self.portNum)
        self.tn = telnetlib.Telnet('192.168.36.186',23)
        time.sleep(2)
        self.tn.write(self.user+"\r")
        time.sleep(3)
        self.tn.write(self.password+"\r")

    def write(self,data):
        self.tn.write(data)
        print data
        
    def read(self):
        self.str = self.tn.read_all()
        return self.str   

    def close(self):
        self.tn.write('Q')
        self.tn.close

if __name__ == '__main__':
    TelnetClient = MyTelnetClient()
    print 'Connected to Server......'
    while 1:        
        data = str(raw_input('Telnet Client>'))
        if data == 'exit':
            TelnetClient.write(data+"\r")        
            TelnetClient.tn.close()
        else:
            TelnetClient.write(data+"\r")        
            if data == 'q':
                TelnetClient.write("exit"+"\r") 
                time.sleep(2)
                TelnetClient.tn.close()
                break
            else:
                datarx = TelnetClient.read()        
                print 'TCP Server>' + datarx        
    print 'Closing Connection with server...'

now this script is able to make the connection with the server....and when i pass the command "dir" i can see the list in the server and not in the client.....how do i print it on the client.....will that data which is being displayed at the server be sent on the port back to the client.....if yes is it on the same port...if no how can i get that data

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.