Discussion:
[Tutor] ​How to use the returned telnet object after creating the telnet session.
Manju M
2015-09-13 07:29:02 UTC
Permalink
Hello all,

First I would like thank you for creating such good platform for discussing
python..!!!

Assume that I will pass IP and port information from a function to open the
telnet session. have opened the telnet session and after opening the telnet
session I returned telnet object to calling function.

Now in the calling function If I use that object to read or write to
terminal I'm getting ERROR “AttributeError: 'NoneType' object has no
attribute 'read_very_eager'”.


#Open telnet connection to devices

def open_telnet_conn(dname,ip,port):

try:
TELNET_PORT = port
TELNET_TIMEOUT = 5
READ_TIMEOUT = 5
cmd = "show config"
#Logging into device

connection=telnetlib.Telnet(ip, TELNET_PORT, TELNET_TIMEOUT)

time.sleep(1)
connection.write(cmd + "\n")

#Here I'm able to write to connection object..
connection.write("\n")
time.sleep(2)

router_output = connection.read_very_eager()
print router_output

return(connection)

except IOError:

print "Input parameter error! Please check username, password and file
name."


#Function to read device IP and port . and this info for opening the telnet
session.
def IP_port(file):
T = []
F = open(file,'r')
F.seek(0)

line=F.read()
tuples = re.findall(r'(.+?)\s+(.+?)\s+(\d+)',line)

#(dname,IP,port)= tuples

for (dname,ip,port) in tuples:
T1=open_telnet_conn(dname,ip,port)
#HERE I will get the telnet object point to the same location as the
connection object. But I'm unable to write or read anything here. I think
need to convert the object T1 to TELNET CLASS object type..

print T1
T1.write("show config") <<<<<<<<<ERROR PART

router_output = T1.read_very_eager()
print router_output
T.append(T1)
return(T)


# import the LC/RSP name ,port and IP address
file='/users/manmahal/MANJU/IP_port.txt'
list_of_telnet=IP_port(file)



*NOTE*: the content of the file is as below:

RSP1 172.27.40.60 2002

RSP0 172.27.40.60 2001

LC0 172.27.40.60 2010


Idea is to create the telnet sessions store them in list or dictionary use
them as and when needed.
Any thought or solution is welcome...!!!



Regards

Manju
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/m
Alan Gauld
2015-09-13 12:03:17 UTC
Permalink
Post by Manju M
Assume that I will pass IP and port information from a function to open the
telnet session. have opened the telnet session and after opening the telnet
session I returned telnet object to calling function.
That makes sense so far. Unfortunately its hard to read your code
below as it has lost all line formatting. Usually this is because
you have used HTML mail. It helps a lot if you post code as plain
text.
Post by Manju M
Now in the calling function If I use that object to read or write to
terminal I'm getting ERROR “AttributeError: 'NoneType' object has no
attribute 'read_very_eager'”.
A None type object often means you have a second return path
from your function that does not explicitly return anything.
For example in your function, if there is an exception you
will return None.
You should probably set the port to some default value...
Post by Manju M
TELNET_PORT = port
TELNET_TIMEOUT = 5
READ_TIMEOUT = 5
cmd = "show config"
#Logging into device
connection=telnetlib.Telnet(ip, TELNET_PORT, TELNET_TIMEOUT)
time.sleep(1)
connection.write(cmd + "\n")
#Here I'm able to write to connection object..
connection.write("\n")
time.sleep(2)
router_output = connection.read_very_eager()
print router_output
What do you get printed here?
Post by Manju M
return(connection)
print "Input parameter error! Please check username, password and file
name."
It looks like you always return connection unless you get an
exception which generates an error message.
I assume you do not see the error printed?

What happens if the connection does not succeed?
Do you only get IOError for all of the operations
you try? Could there be other errors? Are there?
Post by Manju M
#Function to read device IP and port . and this info for opening the telnet
session.
T = []
F = open(file,'r')
F.seek(0)
You don't need to seek(0) on a newly opened file.
It starts at 0 automatically.
Post by Manju M
line=F.read()
tuples = re.findall(r'(.+?)\s+(.+?)\s+(\d+)',line)
I didn't check the regex but I assume you have printed
out tuples to ensure its correct?
Post by Manju M
T1=open_telnet_conn(dname,ip,port)
#HERE I will get the telnet object point to the same location as the
connection object. But I'm unable to write or read anything here. I think
need to convert the object T1 to TELNET CLASS object type..
Python doesn't need type conversions of that sort. You
are returning a telnet object from the function so the
T1 variable will reference a telnet object.
Post by Manju M
print T1
what prints here?
Post by Manju M
T1.write("show config") <<<<<<<<<ERROR PART
Show us the full error text. Python errors contain lots of
useful information but without seeing exactly what it says
we are partly guessing.
Post by Manju M
router_output = T1.read_very_eager()
print router_output
T.append(T1)
return(T)
# import the LC/RSP name ,port and IP address
file='/users/manmahal/MANJU/IP_port.txt'
list_of_telnet=IP_port(file)
RSP1 172.27.40.60 2002
Please tell us more about the output.
And please repost the code in plain text.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/
Danny Yoo
2015-09-13 19:16:49 UTC
Permalink
On Sun, Sep 13, 2015 at 12:29 AM, Manju M
Post by Manju M
Assume that I will pass IP and port information from a function to open the
telnet session. have opened the telnet session and after opening the telnet
session I returned telnet object to calling function.
Hi Manju,

I apologize for potentially hijacking your question, but you may want
to consider SSH over the telnet protocol. There are Python libraries
that know how to speak SSH:

https://wiki.python.org/moin/SecureShell

and the use of Telnet is often discouraged because it does not protect its data.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...