-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathlambda_ssh_ec2.py
More file actions
58 lines (46 loc) · 1.64 KB
/
lambda_ssh_ec2.py
File metadata and controls
58 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
-*- coding: utf-8 -*-
========================
AWS Lambda
========================
Contributor: Chirag Rathod (Srce Cde)
========================
"""
import json
import boto3
import paramiko
def lambda_handler(event, context):
# boto3 client
client = boto3.client("ec2")
s3_client = boto3.client("s3")
# getting instance information
describeInstance = client.describe_instances()
hostPublicIP = []
# fetchin public IP address of the running instances
for i in describeInstance["Reservations"]:
for instance in i["Instances"]:
if instance["State"]["Name"] == "running":
hostPublicIP.append(instance["PublicIpAddress"])
print(hostPublicIP)
# downloading pem filr from S3
s3_client.download_file("bucket-name", "file.pem", "/tmp/file.pem")
# reading pem file and creating key object
key = paramiko.RSAKey.from_private_key_file("/tmp/file.pem")
# an instance of the Paramiko.SSHClient
ssh_client = paramiko.SSHClient()
# setting policy to connect to unknown host
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host = hostPublicIP[0]
print("Connecting to : " + host)
# connecting to server
ssh_client.connect(hostname=host, username="ubuntu", pkey=key)
print("Connected to :" + host)
# command list
commands = ["ls"]
# executing list of commands within server
for command in commands:
print("Executing {command}")
stdin, stdout, stderr = ssh_client.exec_command(command)
print(stdout.read())
print(stderr.read())
return {"statusCode": 200, "body": json.dumps("Thanks!")}