blob: 37b9314df7ddb5569ae027fc2c89464249fcdbda [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
9import scp
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())
Peter D Phan8462faf2021-06-16 12:24:15 -050050 # pk=paramiko.RSAKey.from_private_key(open('~/.ssh_pub/id_rsa.pub'))
Peter D Phan72ce6b82021-06-03 06:18:26 -050051 # Connect to the server
52 self.sshclient.connect(hostname=self.hostname,
53 username=self.username,
54 password=self.password)
55
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 """
97 self.scpclient = scp.SCPClient(self.sshclient.get_transport())
98
99 def scp_file_from_remote(self, remote_file, local_file):
100
101 r"""
102 scp file in remote system to local with date-prefixed filename.
103
104 Description of argument(s):
105 remote_file Full path filename on the remote host
106
107 local_file Full path filename on the local host
108 local filename = date-time_remote filename
109
110 """
111
112 try:
113 self.scpclient.get(remote_file, local_file)
Peter D Phan8462faf2021-06-16 12:24:15 -0500114 except (scp.SCPException, SocketTimeout, PipeTimeout) as e:
115 # Log command with error. Return to caller for next file, if any.
116 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 -0500117 return False
118
119 # Return True for file accounting
120 return True