Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import telnetlib
  2. import time
  3.  
  4.  
  5. #Open telnet connection to devices
  6.  
  7. def open_telnet_conn(ip):
  8. try:
  9. username = 'jtavares'
  10. password = 'teste'
  11.  
  12.  
  13. port = 23
  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. router_output = connection.read_very_eager()
  35. print (router_output)
  36.  
  37.  
  38. #Closing the connection
  39. connection.close()
  40.  
  41. except IOError:
  42. print "Input parameter error!"
  43.  
  44. open_telnet_conn("128.8.4.100")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement