import paramiko

# Configuration
hostname = 'ec2-35-171-155-253.compute-1.amazonaws.com'  # Replace with your EC2 instance public DNS
port = 22
username = 'ubuntu'  # Typically 'ec2-user', 'ubuntu', or 'admin' depending on your AMI
key_file_path = '/Users/prashantsavadi/aws-keys/notcompany-prashant-ec2-a9Aug2024-kp.pem'  # Path to your PEM key file

# Create an SSH client
client = paramiko.SSHClient()

# Automatically add the EC2 instance host key (only for the first time)
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
    # Load the private key
    private_key = paramiko.RSAKey.from_private_key_file(key_file_path)

    # Connect to the EC2 instance
    client.connect(hostname, port=port, username=username, pkey=private_key)

    # Execute a command (e.g., 'uname -a')
    stdin, stdout, stderr = client.exec_command('pwd')

    # Read and print the output
    print(stdout.read().decode())
    print(stderr.read().decode())

finally:
    # Close the connection
    client.close()
