forked from frommelmak/aws-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2-instances.py
More file actions
executable file
·114 lines (99 loc) · 4.2 KB
/
ec2-instances.py
File metadata and controls
executable file
·114 lines (99 loc) · 4.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
import boto3
import sys
import argparse
import paramiko
def list_instances(Filter, RegionName, InstanceIds):
ec2 = boto3.resource('ec2', region_name=RegionName)
instances = ec2.instances.filter(Filters=Filter, InstanceIds=InstanceIds)
columns_format="%-3s %-26s %-15s %-15s %-20s %-10s %-11s %-12s %-16s"
print columns_format % ("num", "Name", "Public IP", "Private IP", "ID", "Type", "Zone","VPC", "Status")
num = 1
hosts = []
name = {}
for i in instances:
try:
name = (item for item in i.tags if item["Key"] == "Name" ).next()
except StopIteration:
name['Value'] = ''
print columns_format % (
num,
name['Value'],
i.public_ip_address,
i.private_ip_address,
i.id,
i.instance_type,
i.placement['AvailabilityZone'],
i.vpc_id,
i.state['Name']
)
num = num + 1
item={'id': i.id, 'ip': i.public_ip_address, 'hostname': name['Value'], 'status': i.state['Name'],}
hosts.append(item)
return hosts
def execute_cmd(host,user,cmd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, username=user)
stdin, stdout, stderr = ssh.exec_command(cmd)
stdout=stdout.read()
stderr=stderr.read()
ssh.close()
return stdout,stderr
except paramiko.AuthenticationException, e:
return "Authentication Error trying to connect into the host %s with the user %s. Plese review your keys" % (host, user), e
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--name',
help="Filter result by name.")
parser.add_argument('-t', '--type',
help="Filer result by type.")
parser.add_argument('-s', '--status',
help="Filter result by status." )
parser.add_argument('-l', '--id_list',
nargs='+', type=str,
help="Provide a list of InstanceIds." )
parser.add_argument('-e', '--execute',
help="Execute a command on instances")
parser.add_argument('-r', '--region',
help="Specify an alternate region to override \
the one defined in the .aws/credentials file")
parser.add_argument('-u', '--user', default="ubuntu",
help="User to run commands if -e option is used.\
Ubuntu user is used by default")
arg = parser.parse_args()
# Default filter if no options are specified
filter=[]
InstanceIds=[]
if arg.name:
filter.append({'Name': 'tag-value', 'Values': ["*" + arg.name + "*"]})
if arg.type:
filter.append({'Name': 'instance-type', 'Values': ["*" + arg.type + "*"]})
if arg.status:
filter.append({'Name': 'instance-state-name', 'Values': ["*" + arg.status + "*"]})
if arg.id_list:
InstanceIds=arg.id_list
if arg.region:
client = boto3.client('ec2')
regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
if arg.region not in regions:
sys.exit("ERROR: Please, choose a valid region.")
hosts=list_instances(filter,arg.region,InstanceIds)
names = ""
if arg.execute:
for item in hosts:
names = names + " " + item["hostname"] + "(" + item["id"] + ")"
print "\nCommand to execute: %s" % arg.execute
print "Executed by: %s" % arg.user
print "Hosts list: %s\n" % names
for item in hosts:
if item["status"] == 'running':
print "::: %s (%s)" % (item["hostname"], item["id"])
stdout,stderr = execute_cmd(item["ip"], arg.user, arg.execute)
print stdout
print stderr
else:
print "::: %s (%s) is not running (command execution skiped)" % (item["hostname"], item["id"])
if __name__ == '__main__':
sys.exit(main())