views:

40

answers:

1

I am modifying a python script to make changes en masse to a hand full of switches via telnet:

import getpass
import sys
import telnetlib

HOST = "192.168.1.1"
user = input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("User Name: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("?\n")
tn.write("exit\n")

When the script executes I receive a "TypeError: expected an object with the buffer interface" Any insight would be helpful.

+1  A: 

Per the docs, read_until's specs are (quoting, my emphasis):

Read until a given byte string, expected, is encountered

You're not passing a byte string, in Python 3, with e.g.:

tn.read_until("User Name: ")

Instead, you're passing a text string, which in Python 3 means a Unicode string.

So, change this to

tn.read_until(b"User Name: ")

the b"..." form is one way to specify a literal byte string.

(Similarly for other such calls of course).

Alex Martelli