Hi ,

Currently i am writing telnet python script for connecting remote server.
I would like to save the telnet session activites in a file.
Is there any better way to grap the remote output in a local machine file.

example

Login: ---------

Password: *********

Login successful

--> show port lan1

Switch Port information
--------------------------------------------------------------------------------
Port: lan1
Status Enabled
Link state Up
UpTime 018 days, 15:13:30
Port media type ISO8802-3 CSMACD
Configured speed/duplex Autonegotiate
Acceptable frame type packet sizes up to 1536 bytes (inclusive)
Broadcast limit Disabled
Multicast limit Disabled


thanks in advance

...Currently i am writing telnet python script for connecting remote server. I would like to save the telnet session activites in a file. Is there any better way to grap the remote output in a local machine file.

I don't know what you mean by "better". I have managed to brute-force this several times. The key is knowing what the remote server will output when it next expects input. This should get you started.

import telnetlib		# For talking to LNX LnxPCs
LNX_prompt  = 'bash-2.05# ' # What our LNX LnxPCs prompt with
def LNX_login(LnxPC):
	"""Create a telnet session and log in to linux on node LnxPC"""
	sess = telnetlib.Telnet(LnxPC)	# Talk to the LnxPC
	sess.set_debuglevel(debuglevel)	# In case of debug
	time.sleep(2.0)				# Give it a rest
	try:
		sess.read_until('login: ', 5)	# Wait for login
		sess.write('root\n')	    # Always root
		sess.read_until('Password: ',5) # Wait for Password
		# Nobody will ever guess this password :-)
		sess.write('DumbellDoor\n')
		sess.read_until(LNX_prompt, 5)	# Fixed prompt
	except EOFError, socket.error:
		print time.strftime("%H:%M:%S  "),
		print "Telnet failure on LnxPC " + LnxPC
		return None
	# No exception. Good login.
	return sess

Note the sess.read_until() functions return all the output from the remote computer up to the prompt. You would want to write these to a file.
You will have to handle things that happen after login, using the above as examples.

Actualy i know how to telnet the remote machine using python script.
I just want to know the script flow to pipe the output of telnet session in file

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.