how to connect linux aws ec2 instance to windows local machine at my home using paramiko

Kashish Naqvi just.kashishhn at gmail.com
Sun Nov 26 07:18:45 EST 2023


I have a north viriginia ec2 linux instance and a windows machine at my home, how do I connec tthem? 


import paramiko
import time

def run_scripts():
    # Set your local machine's SSH details
    local_machine_ip = ' '
    username = 'justk'
    private_key_path = 'C:/Users/justk/.ssh/kashish'
    print("Connected 1", private_key_path)

    # Create an SSH client
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print("Connected 2.2")

    try:
        # Connect to the local machine
        ssh.connect(local_machine_ip, username=username, key_filename=private_key_path,password='abc')
        print("Connected 2")

        # Stop the first script: check_messages.py
        stop_check_messages_command = 'pkill -f python C:/Project/pipeline-deployment/check_messages.py'
        ssh.exec_command(stop_check_messages_command)
        print("Connected 3")

        # Stop the second script: manage.py runserver
        stop_runserver_command = 'pkill -f "python C:/Project/pipeline-deployment/manage.py runserver'
        ssh.exec_command(stop_runserver_command)

        print("Waiting for 5 seconds before starting scripts...")
        time.sleep(60)

        # Run the first script: check_messages.py
        check_messages_command = 'python C:/Project/pipeline-deployment/check_messages.py'
        stdin, stdout, stderr = ssh.exec_command(check_messages_command)
        print(f"Output of check_messages.py:\n{stdout.read().decode('utf-8')}")

        # Run the second script: manage.py runserver
        runserver_command = 'python C:/Project/pipeline-deployment/manage.py runserver'
        stdin, stdout, stderr = ssh.exec_command(runserver_command)
        print(f"Output of manage.py runserver:\n{stdout.read().decode('utf-8')}")

        # Wait for 60 seconds
        print("Waiting for 60 seconds...")
        time.sleep(60)

        # Run the third script: restart.py
        restart_command = 'python C:/Project/pipeline-deployment/restartworkersbutton.py'
        stdin, stdout, stderr = ssh.exec_command(restart_command)
        print(f"Output of restart.py:\n{stdout.read().decode('utf-8')}")

    except Exception as e:
        print(f"Error: {e}")

    finally:
        # Close the SSH connection
        ssh.close()

if __name__ == "__main__":
    run_scripts()


i used this but i am unable to know what ip address to use?


More information about the Python-list mailing list