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""" |
| 27 | Description of argument(s): |
| 28 | |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 29 | hostname Name/IP of the remote (targeting) host |
| 30 | username User on the remote host with access to FFCD files |
| 31 | password Password for user on remote host |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 32 | """ |
| 33 | |
| 34 | self.ssh_output = None |
| 35 | self.ssh_error = None |
| 36 | self.sshclient = None |
| 37 | self.scpclient = None |
| 38 | self.hostname = hostname |
| 39 | self.username = username |
| 40 | self.password = password |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 41 | self.port_ssh = port_ssh |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 42 | |
| 43 | def ssh_remoteclient_login(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 44 | r""" |
| 45 | Method to create a ssh connection to remote host. |
| 46 | """ |
| 47 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 48 | is_ssh_login = True |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 49 | try: |
| 50 | # SSHClient to make connections to the remote server |
| 51 | self.sshclient = paramiko.SSHClient() |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 52 | # setting set_missing_host_key_policy() to allow any host |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 53 | self.sshclient.set_missing_host_key_policy( |
| 54 | paramiko.AutoAddPolicy() |
| 55 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 56 | # Connect to the server |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 57 | self.sshclient.connect( |
| 58 | hostname=self.hostname, |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 59 | port=self.port_ssh, |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 60 | username=self.username, |
| 61 | password=self.password, |
| 62 | banner_timeout=120, |
| 63 | timeout=60, |
| 64 | look_for_keys=False, |
| 65 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 66 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 67 | except ( |
| 68 | BadHostKeyException, |
| 69 | AuthenticationException, |
| 70 | SSHException, |
| 71 | NoValidConnectionsError, |
| 72 | socket.error, |
| 73 | ) as e: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 74 | is_ssh_login = False |
| 75 | |
| 76 | return is_ssh_login |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 77 | |
| 78 | def ssh_remoteclient_disconnect(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 79 | r""" |
| 80 | Clean up. |
| 81 | """ |
| 82 | |
| 83 | if self.sshclient: |
| 84 | self.sshclient.close() |
| 85 | |
| 86 | if self.scpclient: |
| 87 | self.scpclient.close() |
| 88 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 89 | def execute_command(self, command, default_timeout=60): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 90 | """ |
| 91 | Execute command on the remote host. |
| 92 | |
| 93 | Description of argument(s): |
| 94 | command Command string sent to remote host |
| 95 | |
| 96 | """ |
| 97 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 98 | empty = "" |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 99 | cmd_start = time.time() |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 100 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 101 | stdin, stdout, stderr = self.sshclient.exec_command( |
| 102 | command, timeout=default_timeout |
| 103 | ) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 104 | start = time.time() |
| 105 | while time.time() < start + default_timeout: |
Peter D Phan | ba48e9b | 2021-08-12 11:35:50 -0500 | [diff] [blame] | 106 | # Need to do read/write operation to trigger |
| 107 | # paramiko exec_command timeout mechanism. |
Peter D Phan | fd631a1 | 2021-08-12 12:58:08 -0500 | [diff] [blame] | 108 | xresults = stderr.readlines() |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 109 | results = "".join(xresults) |
Peter D Phan | fd631a1 | 2021-08-12 12:58:08 -0500 | [diff] [blame] | 110 | time.sleep(1) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 111 | if stdout.channel.exit_status_ready(): |
| 112 | break |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 113 | cmd_exit_code = stdout.channel.recv_exit_status() |
Peter D Phan | 8a7ec17 | 2021-08-03 13:20:26 -0500 | [diff] [blame] | 114 | |
| 115 | # Convert list of string to one string |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 116 | err = "" |
| 117 | out = "" |
Peter D Phan | ba48e9b | 2021-08-12 11:35:50 -0500 | [diff] [blame] | 118 | for item in results: |
Peter D Phan | fd631a1 | 2021-08-12 12:58:08 -0500 | [diff] [blame] | 119 | err += item |
| 120 | for item in stdout.readlines(): |
Peter D Phan | 8a7ec17 | 2021-08-03 13:20:26 -0500 | [diff] [blame] | 121 | out += item |
| 122 | |
| 123 | return cmd_exit_code, err, out |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 124 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 125 | except ( |
| 126 | paramiko.AuthenticationException, |
| 127 | paramiko.SSHException, |
| 128 | paramiko.ChannelException, |
| 129 | SocketTimeout, |
| 130 | ) as e: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 131 | # Log command with error. Return to caller for next command, if any. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 132 | logging.error( |
| 133 | "\n\tERROR: Fail remote command %s %s" % (e.__class__, e) |
| 134 | ) |
| 135 | logging.error( |
| 136 | "\tCommand '%s' Elapsed Time %s" |
| 137 | % ( |
| 138 | command, |
| 139 | time.strftime( |
| 140 | "%H:%M:%S", time.gmtime(time.time() - cmd_start) |
| 141 | ), |
| 142 | ) |
| 143 | ) |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 144 | return 0, empty, empty |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 145 | |
| 146 | def scp_connection(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 147 | r""" |
| 148 | Create a scp connection for file transfer. |
| 149 | """ |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 150 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 151 | self.scpclient = SCPClient( |
| 152 | self.sshclient.get_transport(), sanitize=lambda x: x |
| 153 | ) |
| 154 | logging.info( |
| 155 | "\n\t[Check] %s SCP transport established.\t [OK]" |
| 156 | % self.hostname |
| 157 | ) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 158 | except (SCPException, SocketTimeout, PipeTimeout) as e: |
| 159 | self.scpclient = None |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 160 | logging.error( |
| 161 | "\n\tERROR: SCP get_transport has failed. %s %s" |
| 162 | % (e.__class__, e) |
| 163 | ) |
| 164 | logging.info( |
| 165 | "\tScript continues generating FFDC on %s." % self.hostname |
| 166 | ) |
| 167 | logging.info( |
| 168 | "\tCollected data will need to be manually offloaded." |
| 169 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 170 | |
| 171 | def scp_file_from_remote(self, remote_file, local_file): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 172 | r""" |
| 173 | scp file in remote system to local with date-prefixed filename. |
| 174 | |
| 175 | Description of argument(s): |
| 176 | remote_file Full path filename on the remote host |
| 177 | |
| 178 | local_file Full path filename on the local host |
| 179 | local filename = date-time_remote filename |
| 180 | |
| 181 | """ |
| 182 | |
| 183 | try: |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 184 | self.scpclient.get(remote_file, local_file, recursive=True) |
Peter D Phan | cf352e5 | 2022-02-14 13:18:01 -0600 | [diff] [blame] | 185 | except (SCPException, SocketTimeout, PipeTimeout, SSHException) as e: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 186 | # 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] | 187 | logging.error( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 188 | "\n\tERROR: Fail scp %s from remotehost %s %s\n\n" |
| 189 | % (remote_file, e.__class__, e) |
| 190 | ) |
Peter D Phan | cf352e5 | 2022-02-14 13:18:01 -0600 | [diff] [blame] | 191 | # Pause for 2 seconds allowing Paramiko to finish error processing before next fetch. |
| 192 | # Without the delay after SCPException, |
| 193 | # next fetch will get 'paramiko.ssh_exception.SSHException'> Channel closed Error. |
| 194 | time.sleep(2) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 195 | return False |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 196 | # Return True for file accounting |
| 197 | return True |