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 | 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 | |
| 45 | try: |
| 46 | # SSHClient to make connections to the remote server |
| 47 | self.sshclient = paramiko.SSHClient() |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 48 | # setting set_missing_host_key_policy() to allow any host |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 49 | self.sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 50 | # Connect to the server |
| 51 | self.sshclient.connect(hostname=self.hostname, |
| 52 | username=self.username, |
George Keishing | 1c3d86b | 2021-06-16 22:29:30 -0500 | [diff] [blame] | 53 | password=self.password, |
| 54 | look_for_keys=False) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 55 | |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 56 | except (BadHostKeyException, AuthenticationException, |
| 57 | SSHException, NoValidConnectionsError, socket.error) as e: |
| 58 | print("\n>>>>>\tERROR: Unable to SSH to %s %s %s\n\n" % (self.hostname, e.__class__, e)) |
| 59 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 60 | |
| 61 | def ssh_remoteclient_disconnect(self): |
| 62 | |
| 63 | r""" |
| 64 | Clean up. |
| 65 | """ |
| 66 | |
| 67 | if self.sshclient: |
| 68 | self.sshclient.close() |
| 69 | |
| 70 | if self.scpclient: |
| 71 | self.scpclient.close() |
| 72 | |
| 73 | def execute_command(self, command): |
| 74 | """ |
| 75 | Execute command on the remote host. |
| 76 | |
| 77 | Description of argument(s): |
| 78 | command Command string sent to remote host |
| 79 | |
| 80 | """ |
| 81 | |
| 82 | try: |
| 83 | stdin, stdout, stderr = self.sshclient.exec_command(command) |
| 84 | stdout.channel.recv_exit_status() |
| 85 | response = stdout.readlines() |
| 86 | return response |
| 87 | except (paramiko.AuthenticationException, paramiko.SSHException, |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 88 | paramiko.ChannelException) as e: |
| 89 | # Log command with error. Return to caller for next command, if any. |
| 90 | print("\n>>>>>\tERROR: Fail remote command %s %s %s\n\n" % (command, e.__class__, e)) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 91 | |
| 92 | def scp_connection(self): |
| 93 | |
| 94 | r""" |
| 95 | Create a scp connection for file transfer. |
| 96 | """ |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 97 | try: |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 98 | self.scpclient = SCPClient(self.sshclient.get_transport(), sanitize=lambda x: x) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 99 | print("\n\t[Check] %s SCP transport established.\t [OK]" % self.hostname) |
| 100 | except (SCPException, SocketTimeout, PipeTimeout) as e: |
| 101 | self.scpclient = None |
| 102 | print("\n>>>>>\tERROR: SCP get_transport has failed. %s %s" % (e.__class__, e)) |
| 103 | print(">>>>>\tScript continues generating FFDC on %s." % self.hostname) |
| 104 | print(">>>>>\tCollected data will need to be manually offloaded.") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 105 | |
| 106 | def scp_file_from_remote(self, remote_file, local_file): |
| 107 | |
| 108 | r""" |
| 109 | scp file in remote system to local with date-prefixed filename. |
| 110 | |
| 111 | Description of argument(s): |
| 112 | remote_file Full path filename on the remote host |
| 113 | |
| 114 | local_file Full path filename on the local host |
| 115 | local filename = date-time_remote filename |
| 116 | |
| 117 | """ |
| 118 | |
| 119 | try: |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 120 | self.scpclient.get(remote_file, local_file, recursive=True) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 121 | except (SCPException, SocketTimeout, PipeTimeout) as e: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 122 | # Log command with error. Return to caller for next file, if any. |
| 123 | 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] | 124 | return False |
| 125 | |
| 126 | # Return True for file accounting |
| 127 | return True |