Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import paramiko |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 4 | from paramiko.ssh_exception import AuthenticationException |
| 5 | from paramiko.ssh_exception import NoValidConnectionsError |
| 6 | from paramiko.ssh_exception import SSHException |
| 7 | from paramiko.ssh_exception import BadHostKeyException |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 8 | from paramiko.buffered_pipe import PipeTimeout as PipeTimeout |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 9 | from scp import SCPClient, SCPException |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 10 | import time |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 11 | import socket |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 12 | import logging |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 13 | from socket import timeout as SocketTimeout |
| 14 | |
| 15 | |
| 16 | class SSHRemoteclient: |
| 17 | r""" |
| 18 | Class to create ssh connection to remote host |
| 19 | for remote host command execution and scp. |
| 20 | """ |
| 21 | |
| 22 | def __init__(self, hostname, username, password): |
| 23 | |
| 24 | r""" |
| 25 | Description of argument(s): |
| 26 | |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 27 | hostname Name/IP of the remote (targeting) host |
| 28 | username User on the remote host with access to FFCD files |
| 29 | password Password for user on remote host |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 30 | """ |
| 31 | |
| 32 | self.ssh_output = None |
| 33 | self.ssh_error = None |
| 34 | self.sshclient = None |
| 35 | self.scpclient = None |
| 36 | self.hostname = hostname |
| 37 | self.username = username |
| 38 | self.password = password |
| 39 | |
| 40 | def ssh_remoteclient_login(self): |
| 41 | |
| 42 | r""" |
| 43 | Method to create a ssh connection to remote host. |
| 44 | """ |
| 45 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 46 | is_ssh_login = True |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 47 | try: |
| 48 | # SSHClient to make connections to the remote server |
| 49 | self.sshclient = paramiko.SSHClient() |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 50 | # setting set_missing_host_key_policy() to allow any host |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 51 | self.sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 52 | # Connect to the server |
| 53 | self.sshclient.connect(hostname=self.hostname, |
| 54 | username=self.username, |
George Keishing | 1c3d86b | 2021-06-16 22:29:30 -0500 | [diff] [blame] | 55 | password=self.password, |
Peter D Phan | e0472f8 | 2021-08-26 12:14:03 -0500 | [diff] [blame] | 56 | banner_timeout=120, |
| 57 | timeout=60, |
George Keishing | 1c3d86b | 2021-06-16 22:29:30 -0500 | [diff] [blame] | 58 | look_for_keys=False) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 59 | |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 60 | except (BadHostKeyException, AuthenticationException, |
| 61 | SSHException, NoValidConnectionsError, socket.error) as e: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 62 | is_ssh_login = False |
| 63 | |
| 64 | return is_ssh_login |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 65 | |
| 66 | def ssh_remoteclient_disconnect(self): |
| 67 | |
| 68 | r""" |
| 69 | Clean up. |
| 70 | """ |
| 71 | |
| 72 | if self.sshclient: |
| 73 | self.sshclient.close() |
| 74 | |
| 75 | if self.scpclient: |
| 76 | self.scpclient.close() |
| 77 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 78 | def execute_command(self, command, |
| 79 | default_timeout=60): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 80 | """ |
| 81 | Execute command on the remote host. |
| 82 | |
| 83 | Description of argument(s): |
| 84 | command Command string sent to remote host |
| 85 | |
| 86 | """ |
| 87 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 88 | empty = '' |
| 89 | cmd_start = time.time() |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 90 | try: |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 91 | stdin, stdout, stderr = \ |
| 92 | self.sshclient.exec_command(command, timeout=default_timeout) |
| 93 | start = time.time() |
| 94 | while time.time() < start + default_timeout: |
Peter D Phan | ba48e9b | 2021-08-12 11:35:50 -0500 | [diff] [blame] | 95 | # Need to do read/write operation to trigger |
| 96 | # paramiko exec_command timeout mechanism. |
Peter D Phan | fd631a1 | 2021-08-12 12:58:08 -0500 | [diff] [blame] | 97 | xresults = stderr.readlines() |
Peter D Phan | ba48e9b | 2021-08-12 11:35:50 -0500 | [diff] [blame] | 98 | results = ''.join(xresults) |
Peter D Phan | fd631a1 | 2021-08-12 12:58:08 -0500 | [diff] [blame] | 99 | time.sleep(1) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 100 | if stdout.channel.exit_status_ready(): |
| 101 | break |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 102 | cmd_exit_code = stdout.channel.recv_exit_status() |
Peter D Phan | 8a7ec17 | 2021-08-03 13:20:26 -0500 | [diff] [blame] | 103 | |
| 104 | # Convert list of string to one string |
| 105 | err = '' |
| 106 | out = '' |
Peter D Phan | ba48e9b | 2021-08-12 11:35:50 -0500 | [diff] [blame] | 107 | for item in results: |
Peter D Phan | fd631a1 | 2021-08-12 12:58:08 -0500 | [diff] [blame] | 108 | err += item |
| 109 | for item in stdout.readlines(): |
Peter D Phan | 8a7ec17 | 2021-08-03 13:20:26 -0500 | [diff] [blame] | 110 | out += item |
| 111 | |
| 112 | return cmd_exit_code, err, out |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 113 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 114 | except (paramiko.AuthenticationException, paramiko.SSHException, |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 115 | paramiko.ChannelException, SocketTimeout) as e: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 116 | # Log command with error. Return to caller for next command, if any. |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 117 | logging.error("\n\tERROR: Fail remote command %s %s" % (e.__class__, e)) |
| 118 | logging.error("\tCommand '%s' Elapsed Time %s" % |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 119 | (command, time.strftime("%H:%M:%S", time.gmtime(time.time() - cmd_start)))) |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 120 | return 0, empty, empty |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 121 | |
| 122 | def scp_connection(self): |
| 123 | |
| 124 | r""" |
| 125 | Create a scp connection for file transfer. |
| 126 | """ |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 127 | try: |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 128 | self.scpclient = SCPClient(self.sshclient.get_transport(), sanitize=lambda x: x) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 129 | logging.info("\n\t[Check] %s SCP transport established.\t [OK]" % self.hostname) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 130 | except (SCPException, SocketTimeout, PipeTimeout) as e: |
| 131 | self.scpclient = None |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 132 | logging.error("\n\tERROR: SCP get_transport has failed. %s %s" % (e.__class__, e)) |
| 133 | logging.info("\tScript continues generating FFDC on %s." % self.hostname) |
| 134 | logging.info("\tCollected data will need to be manually offloaded.") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 135 | |
| 136 | def scp_file_from_remote(self, remote_file, local_file): |
| 137 | |
| 138 | r""" |
| 139 | scp file in remote system to local with date-prefixed filename. |
| 140 | |
| 141 | Description of argument(s): |
| 142 | remote_file Full path filename on the remote host |
| 143 | |
| 144 | local_file Full path filename on the local host |
| 145 | local filename = date-time_remote filename |
| 146 | |
| 147 | """ |
| 148 | |
| 149 | try: |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 150 | self.scpclient.get(remote_file, local_file, recursive=True) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 151 | except (SCPException, SocketTimeout, PipeTimeout) as e: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 152 | # Log command with error. Return to caller for next file, if any. |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 153 | logging.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 154 | "\n\tERROR: Fail scp %s from remotehost %s %s\n\n" % (remote_file, e.__class__, e)) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 155 | return False |
| 156 | |
| 157 | # Return True for file accounting |
| 158 | return True |