-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmssql-executor.py
63 lines (53 loc) · 1.93 KB
/
mssql-executor.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
import os
import pymssql
import threading
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def execute_sql(sql, conn, sql_file_path):
try:
with conn.cursor() as cursor:
cursor.execute(sql)
conn.commit()
print(f"{OKGREEN}[+] Executed {ENDC} {sql_file_path}")
except pymssql.Error as e:
print(f"{FAIL}[-] {ENDC}Error executing {sql_file_path}: {e}")
def execute_sql_files_in_directory(directory_path, conn):
threads = []
for root, dirs, files in os.walk(directory_path):
for file in files:
if file.endswith(".sql"):
sql_file_path = os.path.join(root, file)
with open(sql_file_path, "r") as f:
sql = f.read()
thread = threading.Thread(target=execute_sql, args=(sql, conn, sql_file_path))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
if __name__ == "__main__":
server = "your_server_name"
username = "your_username"
password = "your_password"
database = "your_database_name"
try:
conn = pymssql.connect(server=server, user=username, password=password, database=database)
with conn.cursor() as cursor:
cursor.execute("SELECT 1")
result = cursor.fetchone()
if result[0] == 1:
print(f"{OKGREEN}[+]{ENDC} Database connection test succeeded.")
else:
print(f"{FAIL}[-] {ENDC} Database connection test failed.")
exit(1)
except pymssql.Error as e:
print(f"{FAIL}[-] {ENDC} Database connection test failed: {e}")
exit(1)
execute_sql_files_in_directory(f"path/to/directory", conn)
conn.close()