-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_iptables_save.py
44 lines (37 loc) · 1.65 KB
/
find_iptables_save.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
import os
import shutil
def find_iptables_save_files(root_dir):
iptables_save_files = []
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
try:
with open(file_path, 'r') as f:
content = f.read()
if "# Generated by iptables-save" in content:
iptables_save_files.append(file_path)
except UnicodeDecodeError:
print(f"Skipping file due to encoding error: {file_path}")
return iptables_save_files
def copy_files_to_directory(file_list, destination_dir):
os.makedirs(destination_dir, exist_ok=True)
copied_count = 0
for file_path in file_list:
# Get just the filename without the path
base_filename = os.path.basename(file_path)
dest_path = os.path.join(destination_dir, base_filename)
# Handle duplicate filenames by adding a suffix
counter = 1
while os.path.exists(dest_path):
name, ext = os.path.splitext(base_filename)
dest_path = os.path.join(destination_dir, f"{name}_{counter}{ext}")
counter += 1
shutil.copy2(file_path, dest_path)
copied_count += 1
return copied_count
if __name__ == "__main__":
acl_dir = input("Enter the source directory for ACL files: ")
output_dir = input("Enter the destination directory: ")
iptables_files = find_iptables_save_files(acl_dir)
copied_count = copy_files_to_directory(iptables_files, output_dir)
print(f"Copied {copied_count} files to {output_dir}")