This repository was archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path__init__.py
More file actions
executable file
·305 lines (263 loc) · 9.38 KB
/
__init__.py
File metadata and controls
executable file
·305 lines (263 loc) · 9.38 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
import os
import re
import sys
import csv
import time
import requests
from getpass import getpass
from optparse import OptionParser
from prettytable import PrettyTable
def main():
class QueryException(Exception):
pass
try:
version = 'phpmyadmin-cli Ver 1.0 Released 1 April 2014'
python2 = sys.version_info[0] < 3
def usage():
return version + """
Copyright (c) 2014, fdev.nl. All rights reserved.
This application is not affiliated with or endorsed
by the phpMyAdmin Project or its trademark owners.
Usage: phpmyadmin-cli [OPTIONS] database
-e, --execute=name Execute command and quit.
-E, --export=table Export specified tables, can be used multiple times.
-A, --export-all Export all tables.
-h, --help Display this help and exit.
-l, --location=url Location of phpMyAdmin (http://localhost/phpmyadmin/).
-p Prompt for password to use.
--password=name Password to use.
-s, --ssl-ignore Ignore bad SSL certificates.
-t, --timeout=n Http request timeout in seconds.
-u, --user=name User for login if not current user.
-V, --version Output version information and exit.
"""
parser = OptionParser(usage='%prog [OPTIONS] database')
parser.format_help = usage
parser.add_option('-e', '--execute', action='store', dest='execute', default='')
parser.add_option('-E', '--export', action='append', dest='export', default=[])
parser.add_option('-A', '--export-all', action='store_true', dest='export_all', default=False)
parser.add_option('-l', '--location', action='store', dest='location', default='http://localhost/phpmyadmin/')
parser.add_option('-p', action='store_true', dest='askpass', default=False)
parser.add_option('--password', action='store', dest='password', default='')
parser.add_option('-s', '--ssl-ignore', action='store_false', dest='verify', default=True)
parser.add_option('-t', '--timeout', action='store', type='int', dest='timeout', default=None)
parser.add_option('-u', '--user', action='store', dest='user', default=False)
parser.add_option('-V', '--version', action='store_true', dest='version', default=False)
options, args = parser.parse_args()
kwargs = dict([(k, v) for k, v in options.__dict__.items()])
if kwargs.get('version'):
print(version)
sys.exit()
if len(args) != 1:
parser.print_usage()
sys.exit()
database = args[0]
phpmyadmin = kwargs.get('location').rstrip('/') + '/'
user = kwargs.get('user')
execute = kwargs.get('execute')
export = kwargs.get('export')
export_all = kwargs.get('export_all')
askpass = kwargs.get('askpass')
password = kwargs.get('password')
verify = kwargs.get('verify')
timeout = kwargs.get('timeout')
if user is False:
user = os.environ.get('USER', 'root')
if askpass:
password = getpass('Enter password: ')
# Open phpMyAdmin
session = requests.Session()
result = session.get(phpmyadmin, verify=verify, timeout=timeout)
if result.status_code not in (200, 401):
sys.exit('Could not connect to phpMyAdmin.')
# Auth type is http
if result.status_code == 401:
session.auth = (user, password)
result = session.get(phpmyadmin, verify=verify, timeout=timeout)
if result.status_code == 401:
sys.exit("ERROR #0401: Access denied for user '%s'@'%s' (using password: %s)" % (user, phpmyadmin, 'YES' if password else 'NO'))
# Auth type is cookie or config
else:
# Auth type is cookie
if re.search(r'name="login_form"', result.text):
# Look for token
match = re.search(r'<input type="hidden" name="token" value="([a-f0-9]{32})"', result.text)
if not match:
sys.exit('Unsupported version of phpMyAdmin.')
token = match.group(1)
# Perform login
data = {
'pma_username' : user,
'pma_password' : password,
'server' : 1,
'token' : token,
}
result = session.post(phpmyadmin, data=data, verify=verify, timeout=timeout)
# Still at the login screen
match = re.search(r'name="login_form"', result.text)
if match:
sys.exit("ERROR #0401: Access denied for user '%s'@'%s' (using password: %s)" % (user, phpmyadmin, 'YES' if password else 'NO'))
# Search for token
match_3x = re.search(r"var token = '([a-f0-9]{32})';", result.text)
match_4x = re.search(r'token:"([a-f0-9]{32})"', result.text)
if match_3x:
token = match_3x.group(1)
elif match_4x:
token = match_4x.group(1)
else:
sys.exit('Unsupported version of phpMyAdmin.')
def query_import(q):
"""Perform one or more queries, separated by semi-colon."""
data = {
'db' : database,
'table' : 'something',
'token' : token,
'sql_query' : q,
}
result = session.post(phpmyadmin + 'import.php', data=data, verify=verify, timeout=timeout)
match = re.search(r'<div class="(notice|error)">(#\d+.*?)</div>', result.text, re.DOTALL)
if match:
raise QueryException(match.group(2))
def query_export(tables):
"""Perform one or more queries, separated by semi-colon."""
data = {
'db' : database,
'token' : token,
'export_type' : 'database',
'export_method' : 'quick',
'quick_or_custom' : 'quick',
'table_select[]' : tables,
'table_structure[]' : tables,
'table_data[]' : tables,
'output_format' : 'sendit',
'filename_template' : '@DATABASE@',
'remember_template' : 'on',
'charset_of_file' : 'utf-8',
'compression' : 'none',
'maxsize' : '',
'what' : 'sql',
'sql_include_comments' : 'something',
'sql_header_comment' : '',
'sql_compatibility' : 'NONE',
'sql_structure_or_data' : 'structure_and_data',
'sql_create_table' : 'something',
'sql_create_view' : 'something',
'sql_procedure_function' : 'something',
'sql_create_trigger' : 'something',
'sql_create_table_statements' : 'something',
'sql_if_not_exists' : 'something',
'sql_auto_increment' : 'something',
'sql_backquotes' : 'something',
'sql_type' : 'INSERT',
'sql_insert_syntax' : 'both',
'sql_max_query_size' : '50000',
'sql_hex_for_binary' : 'something',
'sql_utc_time' : 'something'
}
result = session.post(phpmyadmin + 'export.php', data=data, verify=verify, timeout=timeout)
result.encoding = 'utf-8'
if not result:
raise QueryException("Unable to export database.")
return result.text.encode('utf8') if python2 else result.text
def query(q):
"""Perform a single query and return the returned rows."""
data = {
'db' : database,
'table' : 'article',
'token' : token,
'sql_query' : q,
'single_table' : 'TRUE',
'export_type' : 'table',
'allrows' : '1',
'charset_of_file' : 'utf-8',
'compression' : 'none',
'what' : 'csv',
'csv_separator' : ',',
'csv_enclosed' : '"',
'csv_escaped' : '"',
'csv_terminated' : 'AUTO',
'csv_null' : 'NULL',
'csv_columns' : 'something',
'csv_structure_or_data' : 'data',
'csv_data' : '',
# 3.x specific
'asfile' : 'sendit',
# 4.x specific
'output_format' : 'sendit',
}
result = session.post(phpmyadmin + 'export.php', data=data, verify=verify, timeout=timeout)
result.encoding = 'utf-8'
if result and 'text/comma-separated-values' in result.headers['content-type']:
# Detect invalid query
if not result.text.strip().startswith('<!-- PMA-SQL-ERROR -->') and not result.text.startswith('<div class="error">'):
text = result.text.strip()
return text.encode('utf8') if python2 else text
if re.search(r'name="login_form"', result.text):
sys.exit('ERROR #0104: Session with phpMyAdmin expired.')
match = re.search(r'<code>\s*(.*?)\s*</code>', result.text, re.DOTALL)
raise QueryException(match.group(1) if match else 'Could not execute query.')
# Perform export
if export or export_all:
try:
if export_all:
# Get list of tables to export
output = query('SHOW TABLES')
reader = csv.reader(output.split('\n'))
export = [row[0] for row in reader][1:]
print(query_export(export))
except QueryException as e:
sys.exit('ERROR %s' % e)
sys.exit()
# Handle stdin
if not sys.stdin.isatty():
q = sys.stdin.read()
try:
query_import(q)
except QueryException as e:
sys.exit('ERROR %s' % e)
sys.exit()
# Execute command
if execute:
try:
print(query(execute))
except QueryException as e:
sys.exit('ERROR %s' % e)
sys.exit()
# Start interactive shell
print("""Welcome to the phpMyAdmin command-line interface.
Copyright (c) 2014, fdev.nl. All rights reserved.
This application is not affiliated with or endorsed
by the phpMyAdmin Project or its trademark owners.
""")
try:
while True:
if python2:
q = raw_input('phpmyadmin> ')
else:
q = input('phpmyadmin> ')
t0 = time.time()
try:
output = query(q)
if output:
reader = csv.reader(output.split('\n'))
table = PrettyTable()
if python2:
table.field_names = [x.strip() for x in reader.next()]
else:
table.field_names = [x.strip() for x in next(reader)]
table.align = 'l'
for row in reader:
data = [x.strip() for x in row]
data.extend([''] * (len(table.field_names) - len(data)))
table.add_row(data)
print(table)
print('Query OK, %d rows (%.2f sec)\n' % (table.rowcount if output else 0, time.time() - t0))
except QueryException as e:
print('ERROR %s' % e)
except (EOFError, KeyboardInterrupt):
print('Bye')
except Exception as e:
sys.exit('FATAL ERROR: %s' % e)
if __name__ == '__main__':
main()