import getpass import telnetlib HOST = "10.2.3.4" user = input('Enter your username: ') password = getpass.getpass() #Telnet into the host telnet = telnetlib.Telnet(host) #Apply Username from input telnet.read_until('Username: ') telnet.write(user + '\n') #Apply password if exists if password: telnet.read_until('Password: ') telnet.write(password + '\r') # Enter enable mode telnet.write('enable\n') telnet.write('password\n') # Execute the Show Version command telnet.write('show version\n') # Exit the session telnet.write('exit\n') # Write output to screen print(telnet.read_all())