Last active
September 16, 2019 12:35
-
-
Save serxoz/3c6b4293b5e58a8f9b05e16f4905bf18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Modifica todos los dominios que devuelven un email cuando no existe la cuenta de correo | |
y en su lugar deja de procesar ese correo. | |
Haz backup del directorio antes de ejecutar este script!!! | |
# cp -a /etc/valiases /etc/valiases-backup | |
""" | |
from tempfile import mkstemp | |
from shutil import move | |
import os | |
DIR = "/etc/valiases/" | |
def replace(file_path, pattern, subst): | |
#Get owner and group | |
stats = os.stat(file_path) | |
#Create temp file | |
fh, abs_path = mkstemp() | |
with os.fdopen(fh,'w') as new_file: | |
with open(file_path) as old_file: | |
for line in old_file: | |
if line.startswith("*:") and ":fail:" in line: | |
print(file_path) | |
new_file.write("*: :blackhole:\n") | |
else: | |
new_file.write(line) | |
#Remove original file | |
os.remove(file_path) | |
#Move new file | |
move(abs_path, file_path) | |
#Set permission, owner and group | |
os.chown(file_path, stats.st_uid, stats.st_gid) | |
os.chmod(file_path, 0o640) | |
for root, dirs, files in os.walk(DIR): | |
for name in files: | |
ruta = os.path.join(root, name) | |
replace(ruta, "*: :fail:", "*: :blackhole:") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment