-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtermux-scripts.py
192 lines (144 loc) · 5.43 KB
/
termux-scripts.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
#!/data/data/com.termux/files/usr/bin/python
from os import chdir, listdir, name, getcwd
from os.path import isfile, isdir, join, dirname
from prettytable import PrettyTable
from subprocess import call, check_output
from sys import exit
from textwrap import dedent
# global variables
ROOT_DIR = dirname(__file__)
def banner():
print(dedent(r'''
===============================
___ ___ __
| |__ |__) |\/| | | \_/
| |___ | \ | | \__/ / \
__ __ __ __ ___ __
/__` / ` |__) | |__) | /__`
.__/ \__, | \ | | | .__/
______________________________
Project by
dmdhrumilmistry
______________________________
'''))
def execute_cmd(command: str, chk_output: bool = False) -> (str | int):
'''executes command'''
command = command.split()
if chk_output:
return check_output(command, shell=True).decode('utf-8')
return call(command, shell=True)
def get_sh_files(dir_path: str) -> list[str]:
'''returns list of shell files in a directory'''
return [f for f in listdir(dir_path) if isfile(join(dir_path, f)) and f.split('.')[-1] == 'sh']
def get_dirs(dir_path: str) -> list[str]:
'''returns list of directories in a directory'''
return [f for f in listdir(dir_path) if isdir(join(dir_path, f))]
def map_files(dir_path: str) -> dict:
'''maps all files and folders in a subdirectories of a directory with depth 1'''
# get and sanitize directories
dirs = get_dirs(dir_path)
dirs = [dir for dir in dirs if '.' not in dir]
# add current directory to dirs list
# it'll be used to print curr dir scripts
# in get_table function
dirs.insert(0, '.')
# create and return map
map = {}
for dir in dirs:
map[dir] = {
'files': get_sh_files(dir),
'dirs': get_dirs(dir)
}
return map
def get_table(dir_path: str):
# map data
mapped_data = map_files(dir_path)
# create and return table
table = PrettyTable(['Dir', 'Files', 'Dirs'])
# set column alignment
table.align['Dirs'] = 'c'
table.align['Files'] = 'c'
table.align['Dirs'] = 'l'
# add rows
for dir in mapped_data:
table.add_row([
dir,
'\n'.join(mapped_data[dir]['files']),
'\n'.join(mapped_data[dir]['dirs'])
])
return table
def print_table(dir_name: str) -> None:
'''prints table for the specified directory'''
# get and print table
print(get_table(dir_name))
def print_help():
# create help menu table and align cols
help = PrettyTable(['Command', 'Description'])
help.align['Command'] = 'c'
help.align['Description'] = 'l'
# add commands
help.add_row(['help', 'prints commands along with description'])
help.add_row(['exit', 'exits TermuxScripts console'])
help.add_row(['clear', 'clears console'])
help.add_row(['show', 'print options in current directory'])
help.add_row(['select', 'selects a directory, eg. select Installation'])
help.add_row(['back', 'move one directory back'])
help.add_row(['run', 'runs a script eg. run GooglePhish.sh'])
# help.add_row(['',''])
# print help menu
print(help)
def start():
'''starts Command Line User Interface'''
try:
# print banner
banner()
while True:
command = input('>> ').strip().split()
cmd_len = len(command)
match command[0].lower():
case 'help':
print_help()
case 'clear':
cmd = 'cls' if name == 'nt' else 'clear'
execute_cmd(cmd)
case 'exit':
exit(0)
case 'show':
print_table(getcwd())
case 'back':
if getcwd() != ROOT_DIR:
chdir('..')
else:
print("[!] Cannot move out of the Project folder")
case 'select':
if cmd_len >= 2:
dirname = command[1]
new_dir = join(getcwd(), dirname)
# change directory if new_dir is valid
if isdir(new_dir) and ROOT_DIR in new_dir:
chdir(new_dir)
else:
print(
"[!] Directory does not exists/Cannot Move out of Project Folder")
else:
print(
"[!] Directory name is required. example: select directory_name")
case 'run':
if cmd_len >= 2:
sh_file_name = command[1]
file_path = join(getcwd(), sh_file_name)
if isfile(file_path) and ROOT_DIR in file_path:
print(f"[*] Starting {sh_file_name}")
execute_cmd(f"bash {file_path}")
else:
print("[!] Invalid File Path")
case _:
print("[-] Invalid Command, use help for to print commands.")
except KeyboardInterrupt or EOFError:
print("\n[!] Exiting.")
exit()
except Exception as e:
print(f"[-] Exception: {e}")
if __name__ == '__main__':
# start UI
start()