[Tutor] ​How to use the returned telnet object after creating the telnet session.

Alan Gauld alan.gauld at btinternet.com
Sun Sep 13 14:03:17 CEST 2015


On 13/09/15 08:29, Manju M wrote:

> 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.

> 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.


Looking at your code:

> def open_telnet_conn(dname,ip,port):

You should probably set the port to some default value...

> 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

What do you get printed here?

>
> return(connection)
> except IOError:
>
> 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?

> #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)

You don't need to seek(0) on a newly opened file.
It starts at 0 automatically.

> 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?

> 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..

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.

> print T1

what prints here?

> 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.

> 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

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




More information about the Tutor mailing list