def __init__(self, logfile, instance, namespace, location, remote_conn_details): super(ConnectMUMPS, self).__init__() self.type = str.lower(instance) self.namespace = str.upper(namespace) self.prompt = self.namespace + '>' # Create a new SSH client object client = paramiko.SSHClient() # Set SSH key parameters to auto accept unknown hosts client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the host client.connect(hostname=remote_conn_details.remote_address, port=remote_conn_details.remote_port, username=remote_conn_details.username, password=remote_conn_details.password) # Create a client interaction class which will interact with the host from paramikoe import SSHClientInteraction interact = SSHClientInteraction(client, timeout=10, display=False) self.connection = interact self.connection.logfile_read = file(logfile, 'w') self.client = client # apparently there is a deconstructor which disconnects (probably sends a FYN packet) when client is gone
def py_ssh(ip_add, u_name, p_word, com): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip_add, username=u_name, password=p_word) interact = SSHClientInteraction(ssh, timeout=10, display=True) for i in com: interact.send(com) interact.expect('*:~$') conData = interact.current_output_clean return conData
def workon(host, username, password, scp_url, scp_pass): cmd1 = 'sshpass -p ' + scp_pass.rstrip() + ' scp -v -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ' \ + scp_url.rstrip() + ' /bootflash/' cmd2 = 'setup-bootvars.sh ' + scp_url[scp_url.rfind("/") + 1:].rstrip() + ' ' cmd3 = 'setup-clean-config.sh ; vsh -c "reload"' ssh = paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) host = host.rstrip() username = username.rstrip() password = password.rstrip() print '******** HOST IP ' + host + " ********" + '\n' try: ssh.connect(hostname=host, username=username, password=password) interact = SSHClientInteraction(ssh, timeout=50, display=True) try: interact.send(cmd1) interact.expect('.*Exit status 0.*', timeout=20) interact.current_output_clean except: print "\n" + host + " image did not download correctly. Please check scp information and try again." ssh.close() return try: interact.send(cmd2) interact.expect('.*Done.*') interact.current_output_clean except: print "\n" + host + " A issue arose while trying to set the boot variables. Please verify the switch is" \ " supported under this script" ssh.close() return try: interact.send(cmd3) interact.expect('Done') interact.current_output_clean except: print "\n" + host + " Something went wrong while trying to reload the switch. " \ "Please check the switch or reload manually" ssh.close() return except paramiko.AuthenticationException: print host + " is unable to authenticate with the credentials provided. Please double check them and try again." ssh.close() print '*' * 40 print '*' * 40
def main(): # Set login credentials and the server prompt hostname = 'localhost' username = '******' password = '******' prompt = 'fots@fotsies-ubuntu-testlab:~\$ ' # Use SSH client to login try: # Create a new SSH client object client = paramiko.SSHClient() # Set SSH key parameters to auto accept unknown hosts client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the host client.connect(hostname=hostname, username=username, password=password) # Create a client interaction class which will interact with the host interact = SSHClientInteraction(client, timeout=10, display=False) interact.expect(prompt) # Send the tail command interact.send('tail -f /var/log/auth.log') # Now let the class tail the file for us interact.tail(line_prefix=hostname + ': ') except KeyboardInterrupt: print 'Ctrl+C interruption detected, stopping tail' except Exception: traceback.print_exc() finally: try: client.close() except: pass
def main(): # Set login credentials and the server prompt hostname = 'localhost' username = '******' password = '******' prompt = 'fots@fotsies-ubuntu-testlab:~\$ ' # Use SSH client to login try: # Create a new SSH client object client = paramiko.SSHClient() # Set SSH key parameters to auto accept unknown hosts client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the host client.connect(hostname=hostname, username=username, password=password) # Create a client interaction class which will interact with the host interact = SSHClientInteraction(client, timeout=10, display=True) interact.expect(prompt) # Run the first command and capture the cleaned output, if you want # the output without cleaning, simply grab current_output instead. interact.send('uname -a') interact.expect(prompt) cmd_output_uname = interact.current_output_clean # Now let's do the same for the ls command interact.send('ls -l /') interact.expect(prompt) cmd_output_ls = interact.current_output_clean # To expect multiple expressions, just use a list. You can also # selectively take action based on what was matched. # Method 1: You may use the last_match property to find out what was # matched interact.send('~/paramikoe-demo-helper.py') interact.expect([prompt, 'Please enter your name: ']) if interact.last_match == 'Please enter your name: ': interact.send('Fotis Gimian') interact.expect(prompt) # Method 2: You may use the matched index to determine the last match # (like pexpect) interact.send('~/paramikoe-demo-helper.py') found_index = interact.expect([prompt, 'Please enter your name: ']) if found_index == 1: interact.send('Fotis Gimian') interact.expect(prompt) # Send the exit command and expect EOF (a closed session) interact.send('exit') interact.expect() # Print the output of each command print '-'*79 print 'Cleaned Command Output' print '-'*79 print 'uname -a output:' print cmd_output_uname print 'ls -l / output:' print cmd_output_ls except Exception: traceback.print_exc() finally: try: client.close() except: pass