Repository navigation
Expand file tree
/
Copy pathec2-list-instances.py
More file actions
71 lines (52 loc) · 1.47 KB
/
Copy pathec2-list-instances.py
File metadata and controls
71 lines (52 loc) · 1.47 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
import os, sys, getopt, pprint
from boto import ec2
def main(argv):
region = "us-east-1"
public_key = os.environ.get('AWS_ACCESS_KEY_ID')
secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
try:
opts, args = getopt.getopt(argv,"r:",["pk=","sk="])
except getopt.GetoptError:
print 'ec2-list-instances.py -r <region> -pk <public key> -sk <secret key>'
sys.exit(2)
for opt, arg in opts:
if opt == '-r':
region = arg
elif opt == '-pk':
public_key = arg
elif opt == '-sk':
secret_key = arg
try:
client = ec2.connect_to_region(region, aws_access_key_id=public_key, aws_secret_access_key=secret_key)
reservations = client.get_all_instances()
for r in reservations:
for i in r.instances:
print_instance(i)
except Exception as ex:
print ex
sys.exit(1)
def name(i):
if 'Name' in i.tags:
n = i.tags['Name']
else:
n = ''
return n
def private_ip(i):
ip = str(i.private_ip_address)
if ip == 'None':
ip = ''
return ip
def public_ip(i):
ip = str(i.ip_address)
if ip == 'None':
ip = ''
return ip
def print_instance(i):
print i.id + '\t' \
+ i.state + '\t' \
+ name(i) + '\t' \
+ i.instance_type + '\t' \
+ private_ip(i) + '\t' \
+ public_ip(i)
if __name__ == "__main__":
main(sys.argv[1:])