-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorashell.py
More file actions
73 lines (63 loc) · 2.6 KB
/
orashell.py
File metadata and controls
73 lines (63 loc) · 2.6 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
import cx_Oracle as Database
def display_banner():
print("="*80)
print("OraShell: Oracle Database Interactive Shell")
print("Type 'exit' to quit, 'tables' to list tables, or any SQL query")
print("="*80)
# Prompt for credentials and DSN information
user = input("[?] Enter username: ").strip()
password = input("[?] Enter password: ").strip()
host = input("[?] Enter host (e.g., 10.129.205.19): ").strip()
port = input("[?] Enter port (e.g., 1521): ").strip()
service_name = input("[?] Enter service name (e.g., XE): ").strip()
# Ask the user whether to connect as SYSDBA
connect_mode = 0
sysdba_choice = input("[?] Connect as SYSDBA? (Y/n): ").strip().lower()
if sysdba_choice == 'y' or sysdba_choice == '':
connect_mode = Database.SYSDBA
try:
# Create DSN with provided information
dsn = Database.makedsn(host, port, service_name=service_name)
# Attempt to connect using the selected mode
conn = Database.connect(user=user, password=password, dsn=dsn, mode=connect_mode, encoding='UTF-8')
if connect_mode == Database.SYSDBA:
print(f"[✔] Connection successful! Connected to {host}:{port}/{service_name} as SYSDBA")
else:
print(f"[✔] Connection successful! Connected to {host}:{port}/{service_name}")
# Initialize cursor
cursor = conn.cursor()
display_banner()
# Interactive shell loop
while True:
# Read user input and remove the trailing semicolon if it exists
query = input("SQL> ").strip()
if query.endswith(';'):
query = query[:-1]
if query.lower() == 'exit':
break
elif query.lower() == 'tables':
cursor.execute("SELECT table_name FROM user_tables")
print("[+] Available tables:")
for table in cursor:
print(table[0])
else:
try:
cursor.execute(query)
# Check if query returns results
if cursor.description: # SELECT queries
columns = [col[0] for col in cursor.description]
print("[+] Results:")
print(columns)
for row in cursor:
print(row)
else: # INSERT, UPDATE, etc.
conn.commit()
print(f"[+] {cursor.rowcount} rows affected.")
except Database.Error as e:
print(f"[✖] Query error: {e}")
# Close cursor and connection
cursor.close()
conn.close()
print("[✔] Connection closed.")
except Database.Error as e:
print(f"[✖] Connection error: {e}")