-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiQuery.py
executable file
·63 lines (48 loc) · 1.35 KB
/
MultiQuery.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
#!/usr/bin/env python
import sys
import paramiko
import traceback
from multiprocessing import Pool, Lock
# Global stdout write lock to synchronize output
print_lock = Lock()
def query(target):
username, hostname = target.split('@')
port = 22
# Check if non-standard port given
if hostname.find(':') >= 0:
hostname, portstr = hostname.split(':')
port = int(portstr)
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
# Connect using system SSH agent for authentication
client.connect(hostname, port, username)
# Execute uptime on node
stdin, stdout, stderr = client.exec_command('uptime')
# Print results on stdout
print_lock.acquire()
for line in stdout:
print line.strip('\n')
print_lock.release()
client.close()
except Exception, e:
print 'Caught exception: %s: %s' % (e.__class__, e)
traceback.print_exc()
try:
client.close()
except:
pass
def main():
# Read destination nodes to be queried
with open(sys.argv[1], 'r') as f:
node_list = [ line.strip() for line in f ]
# Create worker pool with process count equal to cpu_count (default)
pool = Pool()
# Query each node
pool.map(query, node_list)
# Wrap up
pool.close()
pool.join()
if __name__ == '__main__':
main()