Spaces:
Sleeping
Sleeping
import boto3 | |
import paramiko | |
import time | |
#ssh -i "my_kayname_01.pem" root@ec2-54-90-175-47.compute-1.amazonaws.com | |
# Replace these values with your actual values | |
access_key = "AKIAYIBNUHKTYGACURPX" | |
secret_key = "q8uXKrUQZx50zyIrPCUrnH/zvXNqv6QaR20O3Gaz" | |
region = "us-east-1" | |
image_id = "ami-088f3457fef51ee50" | |
instance_type = "r5.large" # Instance type with 32GB RAM | |
key_name = "my_kayname_01" # Replace with your key pair name | |
security_group_ids = ["sg-0452fd39f8266308d"] | |
def create_instance(): | |
global access_key | |
global secret_key | |
global region | |
global image_id | |
global key_name | |
global security_group_ids | |
# Create a Boto3 EC2 client | |
ec2 = boto3.client("ec2", aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region) | |
# Launch an EC2 instance | |
response = ec2.run_instances( | |
ImageId=image_id, | |
InstanceType=instance_type, | |
KeyName=key_name, | |
MinCount=1, | |
MaxCount=1, | |
SecurityGroupIds=security_group_ids # Set the security group IDs here | |
) | |
instance = response["Instances"][0] | |
instance_id = response["Instances"][0]["InstanceId"] | |
print("Instance created:", instance_id) | |
response = ec2.describe_instances(InstanceIds=[instance_id]) | |
# Extract the public IP address from the response | |
instance = response["Reservations"][0]["Instances"][0] | |
public_ip = instance.get("PublicIpAddress") | |
print(f"Instance created with IP: {public_ip}") | |
while True: | |
# Describe the status of the EC2 instance | |
response = ec2.describe_instance_status(InstanceIds=[instance_id]) | |
# Extract system status and instance status details if available | |
if "InstanceStatuses" in response and len(response["InstanceStatuses"]) > 0: | |
instance_status = response["InstanceStatuses"][0] | |
system_status = response["InstanceStatuses"][0].get("SystemStatus", {}) | |
instance_status_value = instance_status['InstanceStatus']['Status'] | |
system_status_value = system_status.get('Status', 'N/A') | |
print(f"Instance Status: {instance_status_value}") | |
print(f"System Status: {system_status_value}") | |
if instance_status_value == "ok" and system_status_value == "ok": | |
break | |
time.sleep(10) | |
else: | |
print("Status checks not available for the instance.") | |
return instance_id, public_ip | |
def delete_instance(instance_id): | |
global access_key | |
global secret_key | |
global region | |
# Create a Boto3 EC2 client | |
ec2 = boto3.client("ec2", aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region) | |
# Terminate the EC2 instance | |
response = ec2.terminate_instances(InstanceIds=[instance_id]) | |
# Print the response | |
print("Instance termination response:", response) | |
def ssh_connect(hostname): | |
#paramiko.util.log_to_file('paramiko.log') | |
#paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG) | |
# Replace these values with your actual ones | |
port = 22 | |
username = "ec2-user" | |
#username = "ubuntu" | |
#username = "root" | |
key_filename = "my_kayname_01.pem" # Path to your private key file | |
# Create an SSH client instance | |
ssh_client = paramiko.SSHClient() | |
# Automatically add the server's host key (this is insecure and not recommended for production) | |
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
try: | |
# Connect to the EC2 instance | |
ssh_client.connect(hostname, port, username, key_filename=key_filename) | |
# Execute a command on the remote instance (e.g., list directory contents) | |
stdin, stdout, stderr = ssh_client.exec_command("ls -l") | |
# Print the output of the command | |
print(stdout.read().decode("utf-8")) | |
# Install Jupyter Notebook using pip | |
install_command = "pip install jupyter" | |
stdin, stdout, stderr = ssh_client.exec_command(install_command) | |
print(stdout.read().decode("utf-8")) | |
# Start the Jupyter Notebook server (you can specify your desired options) | |
jupyter_command = "jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser" | |
ssh_client.exec_command(jupyter_command) | |
print("Jupyter Notebook installed and server started.") | |
# Start the Jupyter Notebook server (you can specify your desired options) | |
jupyter_command = "jupyter server list" | |
while True: | |
stdin, stdout, stderr = ssh_client.exec_command(jupyter_command) | |
stdout_value = stdout.read().decode("utf-8") | |
if stdout_value.find("http:") != -1: | |
break | |
time.sleep(3) | |
print(stdout_value) | |
p = stdout_value.find("http:") | |
if p != -1: | |
temp = stdout_value[p:] | |
p = temp.find("?token") | |
if p != -1: | |
temp = temp[p + 7:] | |
p = temp.find(" :: ") | |
token_value = temp[: p] | |
print("token=", token_value) | |
# Close the SSH connection | |
ssh_client.close() | |
except Exception as e: | |
print(f"Error: {e}") | |
if __name__ == "__main__": | |
instance_id, public_ip = create_instance() | |
ssh_connect(public_ip) | |
#ssh_connect("44.210.108.226") | |
#delete_instance(instance_id) | |