-
Notifications
You must be signed in to change notification settings - Fork 0
/
getInfo.py
executable file
·153 lines (126 loc) · 3.79 KB
/
getInfo.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
from libcloud.compute.base import Node,NodeImage, NodeSize, NodeLocation
import libcloud.security
import threading
import Queue
import time
import os
import logging
from CLIParser.OptionParser import OptionParser
libcloud.security.VERIFY_SSL_CERT = True
class instanceRunner(threading.Thread):
def __init__(self, **kwargs):
threading.Thread.__init__(self)
self.queue = kwargs['queue']
self.out = kwargs['out']
self.id = kwargs['id']
self.key = kwargs['key']
def run(self):
while True:
node = self.queue.get()
if node != None:
EC2 = get_driver(Provider.EC2_US_EAST)
driver = EC2(self.id,self.key)
try:
tag = driver.ex_describe_tags(node)
Inst = {}
if 'Name' in tag:
Inst["name"] = ['Name']
Inst["pubIP"] = node.public_ip[0]
Inst["privIP"] = node.private_ip[0]
self.out.put(Inst)
except Exception as err:
logging.error(err)
logging.error("Pushing fail back to queue: "+node.name)
self.queue.put(node)
self.queue.task_done()
class instanceOut(threading.Thread):
def __init__(self, **kwargs):
threading.Thread.__init__(self)
self.queue = kwargs['queue']
self.options = kwargs['options']
def resolve_ip(ip):
ip = socket.gethostbyname(ip)
host = socket.gethostbyaddr(ip)
return host[0]
def print_name(self,addr):
print(addr["name"])
def print_pubHost(self,addr):
if "host" in addr:
print(addr["host"])
def print_pubIP(self,addr):
print(addr["pubIP"])
def print_privIP(self,addr):
print(addr["privIP"])
def print_mapping(self,addr):
print(addr["privIP"]+" -> "+addr["pubIP"])
def run(self):
while True:
node = self.queue.get()
if node != None:
if self.options.public:
self.print_pubIP(node)
elif self.options.private:
self.print_privIP(node)
elif self.options.names:
self.print_name(node)
elif self.options.mapping:
self.print_mapping(node)
else:
self.print_privIP(node)
self.queue.task_done()
start = time.time()
def main():
if (os.path.isfile("config")):
opt.parse_config_file("config")
opt.parse_command_line()
if not opt.options.logging:
opt.options.logging="debug"
if opt.options.threads:
tCT = opt.options.threads
else:
tCT = 1
if opt.options.amazon_id:
id = opt.options.amazon_id
else:
print("You must set your id.")
sys.exit(1)
if opt.options.amazon_key:
key = opt.options.amazon_key
else:
print("You must set your key.")
sys.exit(1)
EC2 = get_driver(Provider.EC2_US_EAST)
driver = EC2(id,key)
nodes = driver.list_nodes()
runnerPool = Queue.Queue()
getterPool = Queue.Queue()
logging.debug("Creating "+str(tCT)+" threads.")
for x in xrange(tCT):
iR = instanceRunner(queue=runnerPool, out=getterPool, id=id, key=key)
iR.setDaemon(True)
iR.start()
for x in nodes:
if x.state == 0:
runnerPool.put(x)
iO = instanceOut(queue=getterPool, options=opt.options)
iO.setDaemon(True)
iO.start()
runnerPool.join()
getterPool.join()
if __name__ == '__main__':
opt = OptionParser.instance("[command]")
opt.option("help", type=bool, help="Show this help information.")
opt.option("public", type=bool, help="print Public IPs")
opt.option("private", type=bool, help="print Private IPs")
opt.option("names", type=bool, help="print names")
opt.option("mapping", type=bool, help="print priv -> public mapping")
opt.option("threads", type=int, metavar="THREADS", help="Threads to use.")
opt.option("logging", type=str, metavar="LEVEL", help="Logging Level")
opt.option("amazon_id", type=str, metavar="ID", help="Amazon User ID")
opt.option("amazon_key", type=str, metavar="Key", help="Amazon User Key")
main()
logging.debug("Elapsed Time: %s" % (time.time() - start))