1

I'm a python noob here just trying to learn a new skill so please be gentle :)

After executing the following script it appears that my script creates a telnet session and I can see the cisco devices banner but is not passing the username/password when prompted.

I've tried changing tn.read_until() and tn.read_very_eager() neither of which is triggering the script to move on to write the desired input. I've also tried forcing the username and password to be input as a byte as well as adding + '\n' to my write functions. I've also used sleep to pass a little extra time to wait for the banner to finish printing out.

tldr: when executing the script I see the username prompt, but can't get further than that.

any assistance here is welcomed.

'''
   from time import sleep
   import telnetlib
   from telnetlib import Telnet
   from getpass import getpass

   Username = input('please provide your username ')
   password = getpass()

   # f is the .txt document that lists the IP's we'll be using.
   f = open('devicess.txt')

   for line in f:
       print ('Configuring Device ' + (line))
       device = (line)

   #open connection to variable    
   tn = telnetlib.Telnet(device)
   #For those devices in the above list, connect and run the below commands
   for device in f:
       tn.expect('Username: ')
       tn.write(Username)
       tn.expect('Password: ')
       tn.write(password)
       tn.write('show ver')              
       print('connection established to ' + device)
       tn.write('logout')

   output = tn.read_all()
   print(output)
   print('script complete')
''' 
Andrew Lee
  • 11
  • 1
  • Ended up figuring out that using tn.write('logout') did not kill the session, instead using tn.close() or tn.write('exit') in succession from enable mode would kill the session and stop the script from hanging. – Andrew Lee Oct 17 '19 at 16:33

0 Answers0