-
Notifications
You must be signed in to change notification settings - Fork 9
/
ssh-matic.py
executable file
·68 lines (53 loc) · 2.08 KB
/
ssh-matic.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
#!/usr/bin/env python
'''
SSH-Matic 0.1.2 alpha
Simple update script aimed connecting with SSH to each server on the list and
invoking specific command, then displaying results. Originally created to make
pulls from Git repositories, extended to support more usage scenarios.
Needs some polishing.
Author: https://github.com/tadeck/
HOW TO USE IT
To use it, just provide appropriate settings in settings.dat file. If some setting
will not be found for specific environment, default will be used for that setting.
Feel free to fork it on https://github.com/tadeck/ssh-matic and make pull requests
READ MORE
More details, updated version, howto and bug tracker accessible on:
https://github.com/tadeck/ssh-matic
'''
# location of this script
import sys
current_dir = sys.path[0]
data = eval(open(current_dir+'/settings.dat', 'r').read())
environments = data.get('environments')
defaults = data.get('defaults')
# some helpers used only for displaing colored output in Linux console
def success(text):
return '\033[0;32m' + text + '\033[0m'
def failure(text):
return '\033[0;31m' + text + '\033[0m'
def warning(text):
return '\033[0;33m' + text + '\033[0m'
def bold(text):
return '\033[1;37m' + text + '\033[0m'
import ssh
for environment in environments:
# Determining variables for current environment:
curr = {}
for option_name in ['name','host','username','private_key','command','indent']:
curr[option_name] = environment.get(option_name, defaults.get(option_name))
print '[Connecting to ' + bold(curr['name']) + ']'
print
server = ssh.Connection(host=curr['host'], username=curr['username'], private_key=curr['private_key'])
result = server.execute(curr['command'])
# Cleanup lines
result = [line.strip(' \n\t') for line in result]
# Display results
if result.count('Already up-to-date.') > 0 and len(result) == 1:
print curr['indent'] + success('(up-to-date)')
print
else:
for line in result:
print curr['indent'] + warning(line)
print
server.close()
# ended updating environments