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 | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 10 | import sys |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 11 | import time |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 12 | import socket |
| 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 | |
| 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: |
| 59 | print("\n>>>>>\tERROR: Unable to SSH to %s %s %s\n\n" % (self.hostname, e.__class__, e)) |
| 60 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 61 | |
| 62 | def ssh_remoteclient_disconnect(self): |
| 63 | |
| 64 | r""" |
| 65 | Clean up. |
| 66 | """ |
| 67 | |
| 68 | if self.sshclient: |
| 69 | self.sshclient.close() |
| 70 | |
| 71 | if self.scpclient: |
| 72 | self.scpclient.close() |
| 73 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 74 | def execute_command(self, command, default_timeout=60): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 75 | """ |
| 76 | Execute command on the remote host. |
| 77 | |
| 78 | Description of argument(s): |
| 79 | command Command string sent to remote host |
| 80 | |
| 81 | """ |
| 82 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 83 | empty = '' |
| 84 | cmd_start = time.time() |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 85 | try: |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 86 | stdin, stdout, stderr = \ |
| 87 | self.sshclient.exec_command(command, timeout=default_timeout) |
| 88 | start = time.time() |
| 89 | while time.time() < start + default_timeout: |
| 90 | if stdout.channel.exit_status_ready(): |
| 91 | break |
| 92 | time.sleep(1) |
| 93 | |
| 94 | return stderr.readlines(), stdout.readlines() |
| 95 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 96 | except (paramiko.AuthenticationException, paramiko.SSHException, |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 97 | paramiko.ChannelException, SocketTimeout) as e: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 98 | # 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] | 99 | print("\n>>>>>\tERROR: Fail remote command %s %s" % (e.__class__, e)) |
| 100 | print(">>>>>\tCommand '%s' Elapsed Time %s" % |
| 101 | (command, time.strftime("%H:%M:%S", time.gmtime(time.time() - cmd_start)))) |
| 102 | return empty, empty |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 103 | |
| 104 | def scp_connection(self): |
| 105 | |
| 106 | r""" |
| 107 | Create a scp connection for file transfer. |
| 108 | """ |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 109 | try: |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 110 | self.scpclient = SCPClient(self.sshclient.get_transport(), sanitize=lambda x: x) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 111 | print("\n\t[Check] %s SCP transport established.\t [OK]" % self.hostname) |
| 112 | except (SCPException, SocketTimeout, PipeTimeout) as e: |
| 113 | self.scpclient = None |
| 114 | print("\n>>>>>\tERROR: SCP get_transport has failed. %s %s" % (e.__class__, e)) |
| 115 | print(">>>>>\tScript continues generating FFDC on %s." % self.hostname) |
| 116 | print(">>>>>\tCollected data will need to be manually offloaded.") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 117 | |
| 118 | def scp_file_from_remote(self, remote_file, local_file): |
| 119 | |
| 120 | r""" |
| 121 | scp file in remote system to local with date-prefixed filename. |
| 122 | |
| 123 | Description of argument(s): |
| 124 | remote_file Full path filename on the remote host |
| 125 | |
| 126 | local_file Full path filename on the local host |
| 127 | local filename = date-time_remote filename |
| 128 | |
| 129 | """ |
| 130 | |
| 131 | try: |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 132 | self.scpclient.get(remote_file, local_file, recursive=True) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 133 | except (SCPException, SocketTimeout, PipeTimeout) as e: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 134 | # Log command with error. Return to caller for next file, if any. |
| 135 | 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] | 136 | return False |
| 137 | |
| 138 | # Return True for file accounting |
| 139 | return True |