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

Nick
  • 41
  • 1
  • 6

2 Answers2

2

A parenthesis is missing in the last line.

print(tn.read_all())
Sudipta
  • 4,773
  • 2
  • 27
  • 42
  • Thank you a prompt answer, i made the change as you told but now i'm getting some new error : **Traceback (most recent call last): File "C:\Users\nick_a\workspace\main\src\telnet2.py", line 20, in tn.write("sh run" + "\r") File "c:\python33\lib\telnetlib.py", line 279, in write if IAC in buffer: TypeError: 'in ' requires string as left operand, not bytes – Nick Aug 20 '13 at 13:01
  • @Nick If the solution worked for you, accept it and post a new question for new unrelated problems. Firstly, comments should not contain questions. Secondly, if you post it as a question, you'll get the deserved exposure. :) – Sudipta Aug 20 '13 at 13:17
0

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

Try this.