Вопрос

  1. I need to get a connection to a device via TELNET and write in to telnet session.
  2. I Use - Python 3.3.2 and PyDev For Eclipse 2.7.5
  3. I use IP2COM cause it allows me to open another telnet to the same device and see how the commands are executed.

The main purpose of this is to Read\Write in to Telnet session using Python.

Here is the code that i use:

import getpass
import sys
import telnetlib

HOST = "172.17.174.50"
port = "1003"
#user = input("Enter your remote account: ")
#password = getpass.getpass()

tn = telnetlib.Telnet(HOST, port)

#tn.read_until("user:")
#tn.write(user.encode('ascii') + b"\r")

#tn.write(user.encode("test" + "\r")

#if password:
#    tn.read_until(b"Password: ")
#   tn.write(password.encode('ascii') + b"\n")
tn.write("sh run" + "\r")
tn.write("exit" + "\r")
print(tn.read_all()

Here is the error that i'm getting :

File "C:\Users\user\workspace\main\src\telnet.py", line 23 ^ SyntaxError: unexpected EOF while parsing

The strange thing is that i have only 22 lines.. line number 23 is empty...

Can someone help me with that?

Thanks.

Это было полезно?

Решение

A parenthesis is missing in the last line.

print(tn.read_all())

Другие советы

print(tn.read_all().decode('ascii'))

Try this.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top