Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paramiko
- import time
- # For debugging only
- paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
- def Send_Command_and_Get_Response(command, reponse, result):
- # Send the su command
- shell.send(command)
- # Create a new receive buffer
- receive_buffer = ""
- while not reponse in receive_buffer:
- # Flush the receive buffer
- receive_buffer += shell.recv(1024)
- # Print the receive buffer, if necessary
- if result:
- print receive_buffer
- return receive_buffer
- # VARIABLES THAT NEED CHANGED
- ip = '10.xx.xx.xx'
- username = 'root'
- password = ''
- port = 3008
- # Create instance of SSHClient object
- client = paramiko.SSHClient()
- # Make sure that we add the remote server's SSH key automatically
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- # initiate SSH connection
- client.connect(ip, username=username, password=password,port=port, look_for_keys=False, allow_agent=False)
- print "SSH connection established to %s" % ip
- # Use invoke_shell to establish an 'interactive session'
- shell = client.invoke_shell()
- print "Interactive SSH session established"
- time.sleep(1)
- shell.send("ls -ltrn")
- output = shell.recv(1000)
- print output
- #answer no to auto config
- is_auto = shell.recv(1024)
- if "Would you like" in is_auto:
- Send_Command_and_Get_Response("non", "#", False)
- # clear any config sessions
- is_global = shell.recv(1024)
- if ")#" in is_global:
- Send_Command_and_Get_Response("endn", "#", False)
- # if not in enable mode go to enable mode
- is_enable = shell.recv(1024)
- if ">" in is_enable:
- Send_Command_and_Get_Response("enablen", "#", False)
- # Disable more
- Send_Command_and_Get_Response("terminal length 0n", "#", False)
- strConfig = "conf tn"
- strConfig + "int g0/0/1n"
- strConfig + "ip address 192.168.1.21 255.255.255.0n"
- strConfig + "no shutn"
- strConfig + "exitn"
- strConfig + "ethernet-internal subslot 1/0n"
- strConfig + "platform switchport svin"
- strConfig + "endn"
- print strConfig
- Send_Command_and_Get_Response(strConfig, "#", False)
- Send_Command_and_Get_Response("ping 192.168.1.1n", "#", True)
- #reload the router and check for completion
- time.sleep(1)
- strReload = "reloadn yesn"
- Send_Command_and_Get_Response(strReload, "#", True)
- is_reloading = None
- while (is_reloading) not in ">":
- time.sleep(7)
- is_loading = shell.recv(1024)
- print "Router has finished reloading and is ready for config."
- # Close the SSH connection
- client.close()
- sys.exit(-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement