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