def _ssh_exec(self, command, events=None, silent=False): if not silent: app.print_verbose("SSH Command on " + self.server + ": " + command) # Setting default events if events is None: events = {} events["Verify the SYCO master password:"******"\n" keys = events.keys() value = events.values() # Timeout for ssh.expect timeout_event = len(keys) keys.append(pexpect.TIMEOUT) # The ssh session was terminated before the command had finished it's # execution. keys.append("Connection to .* closed by remote host") terminate_event = len(keys) keys.append("Terminated") # When the ssh command are executed, and back to the command prompt. pexpect_event = len(keys) keys.append("\[PEXPECT\]?") # When ssh.expect reaches the end of file. Probably never # does, is probably reaching [PEXPECT]# first. keys.append(pexpect.EOF) # Disable verbose output for SSH login process. # This outputs a lot of ugly useless text. ssh = expect.sshspawn(timeout=120) ssh.disable_output() ssh.login(self.server, username=self.user, password=self.password) ssh.enable_output() app.print_verbose("---- SSH Result - Start ----", 2) ssh.sendline(command) # First output from ssh.expect doesn't print the caption text before # the output commes. This is for the executed command. app.print_verbose("", 2, new_line=False, enable_caption=True) index=0 while (index <= terminate_event): # Check for strings in keys in the output from the SSH command, # also uses print_verbose on all output from the result. index = ssh.expect(keys, timeout=3600) if 0 <= index and index < timeout_event: ssh.sendline(value[index]) elif index == timeout_event: app.print_error("Caught a timeout from ssh.expect, lets try again.") elif timeout_event < index and index <= terminate_event: raise SSHTerminatedException() # Print new line when finding the PEXPECT prompt. if (app.options.verbose >= 2): if index == pexpect_event: print "" # An extra line break for the looks. if (app.options.verbose >= 2): app.print_verbose("---- SSH Result - End-------\n", 2) # Disable verbose output for SSH logout process. # This because our CENTOS installation outputs a lot of newlines # and a "clear screen" command, that makes the output look ugly. ssh.disable_output() ssh.logout() ssh.enable_output()
def _ssh_exec(self, command, events=None, silent=False): if not silent: app.print_verbose("SSH Command on " + self.server + ": " + command) # Setting default events if events is None: events = {} events["Verify the SYCO master password:"******"\n" keys = events.keys() value = events.values() # Timeout for ssh.expect timeout_event = len(keys) keys.append(pexpect.TIMEOUT) # The ssh session was terminated before the command had finished it's # execution. keys.append("Connection to .* closed by remote host") terminate_event = len(keys) keys.append("Terminated") # When the ssh command are executed, and back to the command prompt. pexpect_event = len(keys) keys.append("\[PEXPECT\]?") # When ssh.expect reaches the end of file. Probably never # does, is probably reaching [PEXPECT]# first. keys.append(pexpect.EOF) # Disable verbose output for SSH login process. # This outputs a lot of ugly useless text. ssh = expect.sshspawn(timeout=120) ssh.disable_output() ssh.login(self.server, username=self.user, password=self.password) ssh.enable_output() app.print_verbose("---- SSH Result - Start ----", 2) ssh.sendline(command) # First output from ssh.expect doesn't print the caption text before # the output commes. This is for the executed command. app.print_verbose("", 2, new_line=False, enable_caption=True) index = 0 while (index <= terminate_event): # Check for strings in keys in the output from the SSH command, # also uses print_verbose on all output from the result. index = ssh.expect(keys, timeout=3600) if 0 <= index and index < timeout_event: ssh.sendline(value[index]) elif index == timeout_event: app.print_error( "Caught a timeout from ssh.expect, lets try again.") elif timeout_event < index and index <= terminate_event: raise SSHTerminatedException() # Print new line when finding the PEXPECT prompt. if (app.options.verbose >= 2): if index == pexpect_event: print "" # An extra line break for the looks. if (app.options.verbose >= 2): app.print_verbose("---- SSH Result - End-------\n", 2) # Disable verbose output for SSH logout process. # This because our CENTOS installation outputs a lot of newlines # and a "clear screen" command, that makes the output look ugly. ssh.disable_output() ssh.logout() ssh.enable_output()