You are on page 1of 2

import paramiko

class RemoteCommandExecutor:
def __init__(self, host, port, username, password):
self.host = host
self.port = port
self.username = username
self.password = password
self.client = None

def connect(self):
try:
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(self.host, port=self.port, username=self.username,
password=self.password)
except paramiko.AuthenticationException as auth_error:
raise Exception(f"Authentication failed for {self.host}: {auth_error}")
except paramiko.SSHException as ssh_error:
raise Exception(f"SSH connection error for {self.host}: {ssh_error}")
except Exception as e:
raise Exception(f"Connection error for {self.host}: {e}")

def execute_command(self, command):


if self.client is None:
raise Exception("Not connected to the remote host. Call connect()
first.")

try:
stdin, stdout, stderr = self.client.exec_command(command)
output = stdout.read().decode('utf-8')
error = stderr.read().decode('utf-8')
return output, error
except Exception as e:
raise Exception(f"Error executing command on {self.host}: {e}")

def disconnect(self):
if self.client is not None:
self.client.close()
self.client = None

if __name__ == "__main__":
remote_executor = RemoteCommandExecutor('your_remote_host', 22,
'your_username', 'your_password')
remote_executor.connect()
output, error = remote_executor.execute_command('ls -l')
print("Command Output:", output)
print("Command Error:", error)
remote_executor.disconnect()

***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************
***********************************************************************************
***********************************************

You might also like