Telnet via a python script?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jmleczko
Posts: 7
Joined: Fri Feb 09, 2018 5:13 pm

Telnet via a python script?

Post by jmleczko » Wed Feb 21, 2018 7:21 pm

Hello All!

I've figured out how to telnet to my esp8266 nodemcu module and control the micropython repl cli.

However, when I try to incorporate that into a regular python script, I can connect, but executing commands throws an error. Here's what I'm seeing:

####################################################
# This is where I telnet to the esp8266 successfully from the terminal of my Mac #
###################################################

lklklk@silo1650:~/Documents/python stuff$ telnet 10.0.1.20
Trying 10.0.1.20...
Connected to 10.0.1.20.
Escape character is '^]'.

>>>
>>> import os
>>> os.listdir()
['boot.py', 'webrepl_cfg.py', 'utelnetserver.py'] - (the utelnetserver.py file is from here on github --> https://github.com/cpopp/MicroTelnetServer)
>>>
telnet> quit
Connection closed.



#####################################################
# Here, I try to telnet to the esp8266 from the python interpreter and get an error #
####################################################

lklklk@silo1650:~/Documents/python stuff$ python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import telnetlib
>>> tn = telnetlib.Telnet(10.0.1.20)
File "<stdin>", line 1
tn = telnetlib.Telnet(10.0.1.20)
^
SyntaxError: invalid syntax
>>> host = "10.0.1.20"
>>> tn=telnetlib.Telnet(host)
>>> tn.write("import os")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/telnetlib.py", line 287, in write
if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes
>>>


Anyone else able to do this successfully?

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Telnet via a python script?

Post by cefn » Wed Feb 21, 2018 11:02 pm

The tn.write() code you are invoking expects a bytes object. See this python3 session as an example.

Code: Select all

>>> "hell" in "hello"
True
>>> b"hell" in "hello"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not bytes
>>> b"hell" in b"hello"
True

So when you call tn.write("import os") the string "import os" is actually a string but is treated as a bytes buffer.

Later there is a test to see if specific bytes are in the bytes buffer if IAC in buffer:, which creates an error just like the session above.

To fix it run tn.write(b"import os") (note the b"")

jmleczko
Posts: 7
Joined: Fri Feb 09, 2018 5:13 pm

Re: Telnet via a python script?

Post by jmleczko » Thu Feb 22, 2018 1:01 am

Thanks! That seems to do the trick.

Now I'm running into the issue of carriage return. I've got the module connected to a terminal session, so I can see that the tn.write(b"import os") command gets typed out. But not executed. I tried running tn.write(b"import os\n") and nothing. Not until I walk over to the terminal session and hit 'Enter' does it get executed. I've tried a few variants to see if one would work

tn.write(b"\n")
tn.write("\n")
tn.write(\n) - of course, that was an error :)

Nothing works

-John

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Telnet via a python script?

Post by dhylands » Thu Feb 22, 2018 3:48 am

Try using \r\n or maybe even just \r

jmleczko
Posts: 7
Joined: Fri Feb 09, 2018 5:13 pm

Re: Telnet via a python script?

Post by jmleczko » Thu Feb 22, 2018 4:20 am

Dave! Yes! Thanks you! :)

Man I love how it's the little things that light me up :)

Post Reply