-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_files.py
executable file
·57 lines (47 loc) · 2.33 KB
/
change_files.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
#/bin//usr/python3
import os
import argparse
import fileinput
import sys
from colorama import Fore
from pwn import log
def parse_arguments():
"""
Get user arguments
"""
parser = argparse.ArgumentParser(description=f'{Fore.CYAN}Search and replace text in index.html files recursively.{Fore.RESET}')
parser.add_argument('-n', '--name', type=str, help='Replacement text')
parser.add_argument('-d', '--destination', type=str, help='Directory to search and replace text for index.html files')
return parser.parse_args()
def replace_text_in_files(name: str, destination: str):
p = log.progress(f"{Fore.RED}Replacing text{Fore.RESET}")
modified_files: int = 0
for root, _, files in os.walk(destination):
for filename in files:
if filename == "index.html":
modified_files += 1
p.status(f"{Fore.BLUE}{modified_files} modified files{Fore.RESET}")
file_path = os.path.join(root, filename)
with fileinput.FileInput(file_path, inplace=True) as file:
for line in file:
try:
line = line.replace('href=/images/', f'href=/{name}/images/')
line = line.replace('src=/images/', f'src=/{name}/images/')
line = line.replace(f'type=image/svg+xml href=/{name}/images/favicon.svg', f'type=image/png href=/{name}/images/favicon-32x32.png')
print(line, end='')
except Exception as e:
p.failure(f"{Fore.RED}something happened: {Fore.YELLOW}{e!r}{Fore.RESET}")
sys.exit(1)
p.success(f"{Fore.GREEN}successfully updated {modified_files} files{Fore.RESET}")
def main()->None:
args = parse_arguments()
# Check if the user has passed all the needed arguments
if args.name and args.destination:
replace_text_in_files(args.name, args.destination)
# If not, exit the program
else:
print(f"{Fore.RED}[!] Please provide both {Fore.YELLOW}-n/--name{Fore.RED} and {Fore.YELLOW}-d/--destination{Fore.RED} arguments.{Fore.RESET}")
print(f"{Fore.RED}[!] {Fore.YELLOW}Usage example: {Fore.RED}python3 {sys.argv[0]} -d docs -n pentesting{Fore.RESET}")
sys.exit(1)
if __name__ == "__main__":
main()