Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import telnetlib
  2. import time
  3. import os
  4.  
  5.  
  6. #Open telnet connection to devices
  7.  
  8. def open_telnet_conn(ip):
  9. try:
  10. username = 'jtavares'
  11. password = '1010522Jr@'
  12. port = 23
  13.  
  14. #Specify the connection timeout in seconds for blocking operations
  15. connection_timeout = 5
  16. reading_timeout = 5
  17. #Loggin into device
  18. connection = telnetlib.Telnet(ip,port, connection_timeout)
  19.  
  20. #Waiting to be asked for a username
  21. router_output = connection.read_until("username:", reading_timeout)
  22. #Enter the username
  23. connection.write(username + "\n")
  24. time.sleep(1)
  25.  
  26. #Waiting to be asked the password
  27. router_output = connection.read_until("password:", reading_timeout)
  28. #Enter the password when asked
  29. connection.write(password + "\n")
  30.  
  31. #Sends a command to the routter
  32. connection.write("terminal length 0\n")
  33. connection.write("show running 0\n")
  34. a=connection.read_very_eager()
  35. print(a)
  36.  
  37.  
  38. #Closing the connection
  39. connection.close()
  40.  
  41. except IOError:
  42. print ("Input parameter error!")
  43.  
  44. open_telnet_conn('10.8.4.100')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement