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