blob: 2110b74dd1c3fa5021137e5fc5b741441f62e823 [file] [log] [blame]
Peter D Phan72ce6b82021-06-03 06:18:26 -05001#!/usr/bin/env python
2
3import paramiko
Peter D Phan8462faf2021-06-16 12:24:15 -05004from paramiko.ssh_exception import AuthenticationException
5from paramiko.ssh_exception import NoValidConnectionsError
6from paramiko.ssh_exception import SSHException
7from paramiko.ssh_exception import BadHostKeyException
Peter D Phan72ce6b82021-06-03 06:18:26 -05008from paramiko.buffered_pipe import PipeTimeout as PipeTimeout
Peter D Phan733df632021-06-17 13:13:36 -05009from scp import SCPClient, SCPException
Peter D Phan8462faf2021-06-16 12:24:15 -050010import sys
Peter D Phan72ce6b82021-06-03 06:18:26 -050011import socket
12from socket import timeout as SocketTimeout
13
14
15class 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 Phan8462faf2021-06-16 12:24:15 -050026 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 Phan72ce6b82021-06-03 06:18:26 -050029 """
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 Phan8462faf2021-06-16 12:24:15 -050048 # setting set_missing_host_key_policy() to allow any host
Peter D Phan72ce6b82021-06-03 06:18:26 -050049 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 Keishing1c3d86b2021-06-16 22:29:30 -050053 password=self.password,
54 look_for_keys=False)
Peter D Phan72ce6b82021-06-03 06:18:26 -050055
Peter D Phan8462faf2021-06-16 12:24:15 -050056 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 Phan72ce6b82021-06-03 06:18:26 -050060
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 Phan8462faf2021-06-16 12:24:15 -050088 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 Phan72ce6b82021-06-03 06:18:26 -050091
92 def scp_connection(self):
93
94 r"""
95 Create a scp connection for file transfer.
96 """
Peter D Phan733df632021-06-17 13:13:36 -050097 try:
Peter D Phan56429a62021-06-23 08:38:29 -050098 self.scpclient = SCPClient(self.sshclient.get_transport(), sanitize=lambda x: x)
Peter D Phan733df632021-06-17 13:13:36 -050099 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 Phan72ce6b82021-06-03 06:18:26 -0500105
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 Phan56429a62021-06-23 08:38:29 -0500120 self.scpclient.get(remote_file, local_file, recursive=True)
Peter D Phan733df632021-06-17 13:13:36 -0500121 except (SCPException, SocketTimeout, PipeTimeout) as e:
Peter D Phan8462faf2021-06-16 12:24:15 -0500122 # 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 Phan72ce6b82021-06-03 06:18:26 -0500124 return False
125
126 # Return True for file accounting
127 return True