1,105,169 Community Members

python telnet connection

Member Avatar
OmK@r
Newbie Poster
4 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Friends,

I had a issue with telnet conncection in python.

Can you send me a working module to connect to telnet windows from a linux machine using python scripting..

Thanks in Adwance,
Omkar

Member Avatar
misokolsky
Newbie Poster
5 posts since Mar 2012
Reputation Points: -1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Tested only with linux telnet server using python3:

from telnetlib import Telnet

tn = Telnet(HOST_IP)
telnet_data = tn.read_until(b'\r\r\n\r\n').decode('UTF-8')

HOST_IP -> byte object
In this example I read telnet data until EOL on linux machine.
In python2 you need a string as argument for read_until() method.

Why you cannot get simple example from docs.python.org?
Python2 telnet example

Member Avatar
CimmerianX
Practically a Master Poster
658 posts since Jul 2010
Reputation Points: 53 [?]
Q&As Helped to Solve: 83 [?]
Skill Endorsements: 13 [?]
 
0
 

This was done on Python 2.7

This will logon to a host via telnet, run a single command and exit.

#!/usr/bin/python

import getpass
import sys
import telnetlib

HOST = "<host IP>"
user = "<user id>"
password = "<user password>"

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")

tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
tn.read_until("~ $")
tn.write("<comman goes here\n")
tn.write("exit\n")

tn.read_all()
Member Avatar
OmK@r
Newbie Poster
4 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi CimmerianX.
Thanks for your reply.

I am using python2.6.6
Running your script on my linux machine throws me the below error.

tn.read_until("~ $")

File "/usr/lib64/python2.6/telnetlib.py", line 319, in read_until

    return self.read_very_lazy()

File "/usr/lib64/python2.6/telnetlib.py", line 395, in read_very_lazy

    raise EOFError, 'telnet connection closed'

EOFError: telnet connection closed

Please help me.

Thanks in Advance,
Omkar

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article