George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 2 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 3 | import logging |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 4 | import socket |
| 5 | import time |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 6 | from socket import timeout as SocketTimeout |
Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 7 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 8 | import paramiko |
| 9 | from paramiko.buffered_pipe import PipeTimeout as PipeTimeout |
| 10 | from paramiko.ssh_exception import ( |
| 11 | AuthenticationException, |
| 12 | BadHostKeyException, |
| 13 | NoValidConnectionsError, |
| 14 | SSHException, |
| 15 | ) |
| 16 | from scp import SCPClient, SCPException |
| 17 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 18 | |
| 19 | class SSHRemoteclient: |
| 20 | r""" |
| 21 | Class to create ssh connection to remote host |
| 22 | for remote host command execution and scp. |
| 23 | """ |
| 24 | |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 25 | def __init__(self, hostname, username, password, port_ssh): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 26 | r""" |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 27 | Initialize the FFDCCollector object with the provided remote host |
| 28 | details. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 29 | |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 30 | This method initializes an FFDCCollector object with the given |
| 31 | attributes, which represent the details of the remote (targeting) |
| 32 | host. The attributes include the hostname, username, password, and |
| 33 | SSH port. |
| 34 | |
| 35 | Parameters: |
| 36 | hostname (str): Name or IP address of the remote (targeting) host. |
| 37 | username (str): User on the remote host with access to FFDC files. |
| 38 | password (str): Password for the user on the remote host. |
| 39 | port_ssh (int): SSH port value. By default, 22. |
| 40 | |
| 41 | Returns: |
| 42 | None |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 43 | """ |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 44 | self.ssh_output = None |
| 45 | self.ssh_error = None |
| 46 | self.sshclient = None |
| 47 | self.scpclient = None |
| 48 | self.hostname = hostname |
| 49 | self.username = username |
| 50 | self.password = password |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 51 | self.port_ssh = port_ssh |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 52 | |
| 53 | def ssh_remoteclient_login(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 54 | r""" |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 55 | Connect to remote host using the SSH client. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 56 | |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 57 | Returns: |
| 58 | bool: The method return True on success and False in failure. |
| 59 | """ |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 60 | is_ssh_login = True |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 61 | try: |
| 62 | # SSHClient to make connections to the remote server |
| 63 | self.sshclient = paramiko.SSHClient() |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 64 | # setting set_missing_host_key_policy() to allow any host |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 65 | self.sshclient.set_missing_host_key_policy( |
| 66 | paramiko.AutoAddPolicy() |
| 67 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 68 | # Connect to the server |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 69 | self.sshclient.connect( |
| 70 | hostname=self.hostname, |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 71 | port=self.port_ssh, |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 72 | username=self.username, |
| 73 | password=self.password, |
| 74 | banner_timeout=120, |
| 75 | timeout=60, |
| 76 | look_for_keys=False, |
| 77 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 78 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 79 | except ( |
| 80 | BadHostKeyException, |
| 81 | AuthenticationException, |
| 82 | SSHException, |
| 83 | NoValidConnectionsError, |
| 84 | socket.error, |
| 85 | ) as e: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 86 | is_ssh_login = False |
George Keishing | 1535205 | 2025-04-24 18:55:47 +0530 | [diff] [blame] | 87 | print("SSH Login: Exception: %s" % e) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 88 | |
| 89 | return is_ssh_login |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 90 | |
| 91 | def ssh_remoteclient_disconnect(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 92 | r""" |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 93 | Disconnect from the remote host using the SSH client. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 94 | |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 95 | This method disconnects from the remote host using the SSH client |
| 96 | established during the FFDC collection process. The method does not |
| 97 | return any value. |
| 98 | |
| 99 | Returns: |
| 100 | None |
| 101 | """ |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 102 | if self.sshclient: |
| 103 | self.sshclient.close() |
| 104 | |
| 105 | if self.scpclient: |
| 106 | self.scpclient.close() |
| 107 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 108 | def execute_command(self, command, default_timeout=60): |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 109 | r""" |
| 110 | Execute a command on the remote host using the SSH client. |
| 111 | |
| 112 | This method executes a provided command on the remote host using the |
| 113 | SSH client. The method takes the command string as an argument and an |
| 114 | optional default_timeout parameter of 60 seconds, which specifies the |
| 115 | timeout for the command execution. |
| 116 | |
| 117 | The method returns the output of the executed command as a string. |
| 118 | |
| 119 | Parameters: |
| 120 | command (str): The command string to be executed |
| 121 | on the remote host. |
| 122 | default_timeout (int, optional): The timeout for the command |
| 123 | execution. Defaults to 60 seconds. |
| 124 | |
| 125 | Returns: |
| 126 | str: The output of the executed command as a string. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 127 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 128 | empty = "" |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 129 | cmd_start = time.time() |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 130 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 131 | stdin, stdout, stderr = self.sshclient.exec_command( |
| 132 | command, timeout=default_timeout |
| 133 | ) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 134 | start = time.time() |
| 135 | while time.time() < start + default_timeout: |
Peter D Phan | ba48e9b | 2021-08-12 11:35:50 -0500 | [diff] [blame] | 136 | # Need to do read/write operation to trigger |
| 137 | # paramiko exec_command timeout mechanism. |
Peter D Phan | fd631a1 | 2021-08-12 12:58:08 -0500 | [diff] [blame] | 138 | xresults = stderr.readlines() |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 139 | results = "".join(xresults) |
Peter D Phan | fd631a1 | 2021-08-12 12:58:08 -0500 | [diff] [blame] | 140 | time.sleep(1) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 141 | if stdout.channel.exit_status_ready(): |
| 142 | break |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 143 | cmd_exit_code = stdout.channel.recv_exit_status() |
Peter D Phan | 8a7ec17 | 2021-08-03 13:20:26 -0500 | [diff] [blame] | 144 | |
| 145 | # Convert list of string to one string |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 146 | err = "" |
| 147 | out = "" |
Peter D Phan | ba48e9b | 2021-08-12 11:35:50 -0500 | [diff] [blame] | 148 | for item in results: |
Peter D Phan | fd631a1 | 2021-08-12 12:58:08 -0500 | [diff] [blame] | 149 | err += item |
| 150 | for item in stdout.readlines(): |
Peter D Phan | 8a7ec17 | 2021-08-03 13:20:26 -0500 | [diff] [blame] | 151 | out += item |
| 152 | |
| 153 | return cmd_exit_code, err, out |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 154 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 155 | except ( |
| 156 | paramiko.AuthenticationException, |
| 157 | paramiko.SSHException, |
| 158 | paramiko.ChannelException, |
| 159 | SocketTimeout, |
| 160 | ) as e: |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 161 | # Log command with error. |
| 162 | # Return to caller for next command, if any. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 163 | logging.error( |
| 164 | "\n\tERROR: Fail remote command %s %s" % (e.__class__, e) |
| 165 | ) |
| 166 | logging.error( |
| 167 | "\tCommand '%s' Elapsed Time %s" |
| 168 | % ( |
| 169 | command, |
| 170 | time.strftime( |
| 171 | "%H:%M:%S", time.gmtime(time.time() - cmd_start) |
| 172 | ), |
| 173 | ) |
| 174 | ) |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 175 | return 0, empty, empty |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 176 | |
| 177 | def scp_connection(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 178 | r""" |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 179 | Establish an SCP connection for file transfer. |
| 180 | |
| 181 | This method creates an SCP connection for file transfer using the SSH |
| 182 | client established during the FFDC collection process. |
| 183 | |
| 184 | Returns: |
| 185 | None |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 186 | """ |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 187 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 188 | self.scpclient = SCPClient( |
| 189 | self.sshclient.get_transport(), sanitize=lambda x: x |
| 190 | ) |
| 191 | logging.info( |
| 192 | "\n\t[Check] %s SCP transport established.\t [OK]" |
| 193 | % self.hostname |
| 194 | ) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 195 | except (SCPException, SocketTimeout, PipeTimeout) as e: |
| 196 | self.scpclient = None |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 197 | logging.error( |
| 198 | "\n\tERROR: SCP get_transport has failed. %s %s" |
| 199 | % (e.__class__, e) |
| 200 | ) |
| 201 | logging.info( |
| 202 | "\tScript continues generating FFDC on %s." % self.hostname |
| 203 | ) |
| 204 | logging.info( |
| 205 | "\tCollected data will need to be manually offloaded." |
| 206 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 207 | |
| 208 | def scp_file_from_remote(self, remote_file, local_file): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 209 | r""" |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 210 | SCP a file from the remote host to the local host with a filename. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 211 | |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 212 | This method copies a file from the remote host to the local host using |
| 213 | the SCP protocol. The method takes the remote_file and local_file as |
| 214 | arguments, which represent the full paths of the files on the remote |
| 215 | and local hosts, respectively. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 216 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 217 | |
George Keishing | e5c4439 | 2025-05-17 21:14:09 +0530 | [diff] [blame^] | 218 | Parameters: |
| 219 | remote_file (str): The full path filename on the remote host. |
| 220 | local_file (str): The full path filename on the local host. |
| 221 | |
| 222 | Returns: |
| 223 | bool: The method return True on success and False in failure. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 224 | """ |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 225 | try: |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 226 | self.scpclient.get(remote_file, local_file, recursive=True) |
Peter D Phan | cf352e5 | 2022-02-14 13:18:01 -0600 | [diff] [blame] | 227 | except (SCPException, SocketTimeout, PipeTimeout, SSHException) as e: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 228 | # 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] | 229 | logging.error( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 230 | "\n\tERROR: Fail scp %s from remotehost %s %s\n\n" |
| 231 | % (remote_file, e.__class__, e) |
| 232 | ) |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 233 | # Pause for 2 seconds allowing Paramiko to finish error processing |
| 234 | # before next fetch. Without the delay after SCPException, next |
| 235 | # fetch will get 'paramiko.ssh_exception.SSHException'> Channel |
| 236 | # closed Error. |
Peter D Phan | cf352e5 | 2022-02-14 13:18:01 -0600 | [diff] [blame] | 237 | time.sleep(2) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 238 | return False |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 239 | # Return True for file accounting |
| 240 | return True |