Search code examples
pythonpython-2.7string-formattingtelnetlib

'str' object does not support item assignment telnetlib


I am trying to use the telnet library in python to obtain information from a device. However I have an error as listed above which gets thrown when I try to run my code.

'str' object does not support item assignment

 tn = telnetlib.Telnet(host)
    tn.expect([r".*ogin:.*", r".*sername:.*"])
    tn.write(username+"\n")
    tn.expect(r".*assword:.*")
    tn.write(password+"\n")
    tn.write("terminal length 0"+"\n")
    tn.write("show " + configType + "-config"+"\n")
    tn.write("exit"+"\n")
    output = tn.read_all()

Solution

  • In the 4th line you are doing

    tn.expect(r".*assword:.*")
    

    But telnet.expect only accepts list.