Advertisement
Guest User

Untitled

a guest
Mar 21st, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. import paramiko
  2. import time
  3.  
  4. # For debugging only
  5. paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
  6.  
  7. def Send_Command_and_Get_Response(command, reponse, result):
  8. # Send the su command
  9. shell.send(command)
  10.  
  11. # Create a new receive buffer
  12. receive_buffer = ""
  13.  
  14. while not reponse in receive_buffer:
  15. # Flush the receive buffer
  16. receive_buffer += shell.recv(1024)
  17.  
  18. # Print the receive buffer, if necessary
  19. if result:
  20. print receive_buffer
  21.  
  22. return receive_buffer
  23.  
  24. # VARIABLES THAT NEED CHANGED
  25. ip = '10.xx.xx.xx'
  26. username = 'root'
  27. password = ''
  28. port = 3008
  29.  
  30. # Create instance of SSHClient object
  31. client = paramiko.SSHClient()
  32.  
  33. # Make sure that we add the remote server's SSH key automatically
  34. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  35.  
  36.  
  37. # initiate SSH connection
  38. client.connect(ip, username=username, password=password,port=port, look_for_keys=False, allow_agent=False)
  39. print "SSH connection established to %s" % ip
  40.  
  41. # Use invoke_shell to establish an 'interactive session'
  42. shell = client.invoke_shell()
  43.  
  44.  
  45. print "Interactive SSH session established"
  46. time.sleep(1)
  47. shell.send("ls -ltrn")
  48. output = shell.recv(1000)
  49. print output
  50.  
  51.  
  52. #answer no to auto config
  53. is_auto = shell.recv(1024)
  54. if "Would you like" in is_auto:
  55. Send_Command_and_Get_Response("non", "#", False)
  56.  
  57. # clear any config sessions
  58. is_global = shell.recv(1024)
  59. if ")#" in is_global:
  60. Send_Command_and_Get_Response("endn", "#", False)
  61.  
  62. # if not in enable mode go to enable mode
  63. is_enable = shell.recv(1024)
  64. if ">" in is_enable:
  65. Send_Command_and_Get_Response("enablen", "#", False)
  66.  
  67. # Disable more
  68. Send_Command_and_Get_Response("terminal length 0n", "#", False)
  69.  
  70. strConfig = "conf tn"
  71. strConfig + "int g0/0/1n"
  72. strConfig + "ip address 192.168.1.21 255.255.255.0n"
  73. strConfig + "no shutn"
  74. strConfig + "exitn"
  75. strConfig + "ethernet-internal subslot 1/0n"
  76. strConfig + "platform switchport svin"
  77. strConfig + "endn"
  78. print strConfig
  79. Send_Command_and_Get_Response(strConfig, "#", False)
  80.  
  81. Send_Command_and_Get_Response("ping 192.168.1.1n", "#", True)
  82.  
  83. #reload the router and check for completion
  84. time.sleep(1)
  85. strReload = "reloadn yesn"
  86. Send_Command_and_Get_Response(strReload, "#", True)
  87.  
  88. is_reloading = None
  89. while (is_reloading) not in ">":
  90. time.sleep(7)
  91. is_loading = shell.recv(1024)
  92. print "Router has finished reloading and is ready for config."
  93.  
  94.  
  95. # Close the SSH connection
  96. client.close()
  97. sys.exit(-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement