File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ import boto3
2
+ import paramiko
3
+ def code_lambda (event , context ):
4
+ host = 0
5
+ s3_client = boto3 .client ('s3' )
6
+ ec2 = boto3 .resource ('ec2' )
7
+ running_instances = ec2 .instances .filter (Filters = [{'Name' : 'instance-id' ,'Values' : ['Add your instance id' ]}])
8
+ for instance in running_instances :
9
+ host = instance .public_ip_address
10
+
11
+ s3_client .download_file ('Add your bucket name' ,'key/Add your key name.pem' , '/tmp/Add your key name.pem' )
12
+
13
+ k = paramiko .RSAKey .from_private_key_file ("/tmp/Add your key name.pem" )
14
+ c = paramiko .SSHClient ()
15
+ c .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
16
+
17
+ print ("Connecting to " + host )
18
+ c .connect ( hostname = host , username = "ubuntu" , pkey = k )
19
+ print ("Connected to " + host )
20
+ commands = ["/home/ubuntu/demo.sh" ]
21
+ for command in commands :
22
+ print ("Executing {}" .format (command ))
23
+ stdin , stdout , stderr = c .exec_command (command )
24
+ print (stdout .read ())
25
+ print (stderr .read ())
26
+ return
27
+ {
28
+ 'message' : "Done"
29
+ }
30
+
You can’t perform that action at this time.
0 commit comments