blob: 9715948772c088c928c78b77c38915fac88a3a72 [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 Phanbabf2962021-07-07 11:24:40 -050010import time
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
Peter D Phan5963d632021-07-12 09:58:55 -050045 is_ssh_login = True
Peter D Phan72ce6b82021-06-03 06:18:26 -050046 try:
47 # SSHClient to make connections to the remote server
48 self.sshclient = paramiko.SSHClient()
Peter D Phan8462faf2021-06-16 12:24:15 -050049 # setting set_missing_host_key_policy() to allow any host
Peter D Phan72ce6b82021-06-03 06:18:26 -050050 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 Keishing1c3d86b2021-06-16 22:29:30 -050054 password=self.password,
55 look_for_keys=False)
Peter D Phan72ce6b82021-06-03 06:18:26 -050056
Peter D Phan8462faf2021-06-16 12:24:15 -050057 except (BadHostKeyException, AuthenticationException,
58 SSHException, NoValidConnectionsError, socket.error) as e:
Peter D Phan5963d632021-07-12 09:58:55 -050059 is_ssh_login = False
60
61 return is_ssh_login
Peter D Phan72ce6b82021-06-03 06:18:26 -050062
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 Phan3beb02e2021-07-06 13:25:17 -050075 def execute_command(self, command, default_timeout=60):
Peter D Phan72ce6b82021-06-03 06:18:26 -050076 """
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 Phanbabf2962021-07-07 11:24:40 -050084 empty = ''
85 cmd_start = time.time()
Peter D Phan72ce6b82021-06-03 06:18:26 -050086 try:
Peter D Phanbabf2962021-07-07 11:24:40 -050087 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 Phan72ce6b82021-06-03 06:18:26 -050097 except (paramiko.AuthenticationException, paramiko.SSHException,
Peter D Phanbabf2962021-07-07 11:24:40 -050098 paramiko.ChannelException, SocketTimeout) as e:
Peter D Phan8462faf2021-06-16 12:24:15 -050099 # Log command with error. Return to caller for next command, if any.
Peter D Phanbabf2962021-07-07 11:24:40 -0500100 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 Phan72ce6b82021-06-03 06:18:26 -0500104
105 def scp_connection(self):
106
107 r"""
108 Create a scp connection for file transfer.
109 """
Peter D Phan733df632021-06-17 13:13:36 -0500110 try:
Peter D Phan56429a62021-06-23 08:38:29 -0500111 self.scpclient = SCPClient(self.sshclient.get_transport(), sanitize=lambda x: x)
Peter D Phan733df632021-06-17 13:13:36 -0500112 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 Phan72ce6b82021-06-03 06:18:26 -0500118
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 Phan56429a62021-06-23 08:38:29 -0500133 self.scpclient.get(remote_file, local_file, recursive=True)
Peter D Phan733df632021-06-17 13:13:36 -0500134 except (SCPException, SocketTimeout, PipeTimeout) as e:
Peter D Phan8462faf2021-06-16 12:24:15 -0500135 # 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 Phan72ce6b82021-06-03 06:18:26 -0500137 return False
138
139 # Return True for file accounting
140 return True