-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcherokee-remote.py
executable file
·220 lines (174 loc) · 6.87 KB
/
cherokee-remote.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/python
#title :cherokee-remote.py
#description :A remote SSH cherokee admin helper,
# it will run cherokee-admin remotely and stablish a secure ssh tunnel to the admin port.
# Implemented with python fabric.
#author :Sergio Aguilar <[email protected]>
#date :2013-04-02
#version :0.0.1
#license :GPL v2
#============================================================================
import os # To suppress webbroser message
import sys # To count arguments
import argparse # For arguments
import webbrowser # To open the web browser
import socket # To check for the host
import fabric # For ssh connectivity
from fabric import api
from fabric.api import *
# Variables:
USER = "username"
HOST="hostname"
RPORT="9090"
LPORT="9000"
SSH_CONNECT="ssh -f -N -L %s:localhost:%s " % (LPORT, RPORT)
# Screen settings:
# -d Detach mode, This creates a new session but doesn't attach to it.
# -S Session name
# sleep 1 required, otherwise it will fail.
CHEROKEE_ADMIN="screen -dmS CHEROKEE cherokee-admin -u -t; sleep 1"
# Env
env.host_string = "%s@%s" % (USER, HOST)
#env.key_filename = ["/path/to/ssh/key"]
#env.colors = True
env.warn_only = True # Wont abort if remote command execution fails
#env.show = ['debug']
#fabric.state.output['running'] = True
#env.output_prefix = False
# Parser:
cherokee_remote_desc="""A remote SSH cherokee admin helper,
it will run cherokee-admin remotely and stablish a secure ssh tunnel to the admin port.
Implemented with python fabric."""
parser = argparse.ArgumentParser(description=cherokee_remote_desc)
parser.add_argument('-a', '--admin', action='store_true', help='Start the remote cherokee-admin', default=False)
parser.add_argument('-u', '--user', help='Username for ssh connection', default=USER)
parser.add_argument('-H', '--host', help='Hostname for ssh connection', default=HOST)
parser.add_argument('-t', '--tunnel', action='store_true', help="Just connect to remote cherokee-admin. Using ssh port forwarding.", default=False)
parser.add_argument('--start', action='store_true', help="Will start the the full cherokee-remote process", default=False)
parser.add_argument('--stop', action='store_true', help="Will stop the remote cherokee-admin and tunnel", default=False)
parser.add_argument('--status', action='store_true', help="Display status", default=False)
parser.add_argument('-v', '--verbose', action='store_true', help="Verbose On/Off; with fabric debug lever Off", default=False)
parser.add_argument('-vv', '--debug', action='store_true', help="Debug On/Off; with fabric debug level On", default=False)
parser.add_argument('-w', '--webpage', action='store_true', help='Opens the admin webpage in the web browser', default=False)
# nargs="?" wont make error if missing argument
args = parser.parse_args()
def setup():
env.host_string = "%s@%s" % (USER, HOST)
env.key_filename = ["/path/to/ssh/key"]
def get_cherokee_admin_pid():
admin_pid = run("pidof cherokee-admin")
return admin_pid
def run_cheroke_admin():
sudo(CHEROKEE_ADMIN)
def kill_cherokee_admin():
pid = get_cherokee_admin_pid()
sudo("kill -9 %s" % pid)
def cherokee_admin_tunnel():
if get_cherokee_admin_tunnel_pid():
print "Tunnel already exists!"
elif int(get_cherokee_admin_pid()):
global SSH_CONNECT
SSH_CONNECT += env.host_string
local(SSH_CONNECT)
else:
print "No cherokee-admin running remotely"
def get_cherokee_admin_tunnel_pid():
# Capture = True required to enable return on local fabric function:
return local("ps aux | grep [s]sh | grep 9000 | awk '{print $2}'", capture=True)
def test_hostname(host):
try:
socket.gethostbyname(host)
return True
except:
return False
def cherokee_admin_tunnel_end():
local_pid = get_cherokee_admin_tunnel_pid()
command = "kill -9 %s" % local_pid
local(command)
def browser_launch():
# Suppresses the chrome error message:
# "Created new window in existing browser session."
# There is still a bug when launching chrome for the first time.
# those are chrome related bugs. No errors on Firefox.
# Suggestion from:
# http://stackoverflow.com/questions/2323080/how-can-i-disable-the-webbrowser-message-in-python
savout = os.dup(1)
os.close(1)
os.open(os.devnull, os.O_RDWR)
try:
webbrowser.open_new_tab('http://localhost:' + LPORT)
finally:
os.dup2(savout, 1)
def start():
# Start remote cherokee-admin
run_cheroke_admin()
# Create tunnel to remote cherokee-admin:
cherokee_admin_tunnel()
# Open remote cherokee admin in local port:
browser_launch()
def stop():
# Stop tunnel:
cherokee_admin_tunnel_end()
# Stop remote cheroke-admin:
kill_cherokee_admin()
if __name__ == '__main__':
# Define verbose_level
show_var= 'output'
hide_var = 'everything'
if args.verbose:
show_var = 'everything'
hide_var = 'warnings'
if args.debug:
show_var = 'everything'
hide_var = 'aborts'
fabric.state.output['debug'] = True
# Not verbose or debug:
# Debug hide settings:
#with hide():
with settings(show(show_var), hide(hide_var)) :
if args.debug:
print "Arguments to command= %s" % args
print "Fabric.state.output Variable:"
print fabric.state.output
# Check for arguments:
if len(sys.argv) == 1:
parser.print_help()
else:
# Test if we have a valid host:
if not test_hostname(args.host):
print "DNS error, host can't be resolve. Host: %s" % args.host
exit()
# Start cherokee-admin:
if args.admin:
run_cheroke_admin()
# Open Webpage
if args.webpage:
browser_launch()
# Set the host_string variable with args variable:
# there will always be these 2 variables because of the defaults:
if args.user and args.host:
env.host_string = "%s@%s" % (args.user, args.host)
if args.tunnel:
print "Creating tunnel"
cherokee_admin_tunnel()
if args.start:
print "Starting Cherokee-remote"
start()
if args.stop:
print "Stoping Cherokee-remote"
stop()
if args.status:
print "***Current cherokee-remote status***"
admin_pid = get_cherokee_admin_pid()
tunnel_pid = get_cherokee_admin_tunnel_pid()
status = "BAD!!!"
if admin_pid and tunnel_pid:
status = "OK!"
print "Status: %s" % status
print "Remote cherokee-admin pid: %s" % admin_pid
print "Tunnel pid: %s" % tunnel_pid
# No action message:
if not ( args.tunnel or args.start or args.stop or args.status or args.admin or args.webpage ):
parser.print_help()
print ""
print "**** No action specified****"