Skip to content
Snippets Groups Projects
Commit 6ead63b8 authored by Miguel Montes's avatar Miguel Montes
Browse files

Scripts auxiliares

parent f28c5649
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
import re, subprocess
ufw = "/usr/sbin/ufw"
ufw_command = [ufw]
ufw_delete = ufw_command + ["--force", "delete"]
def read_hosts():
hosts = set()
pattern = re.compile(r"([0-9.:]+)\s+\w+-(sealer|gateway)")
with open("/etc/hosts") as file:
for line in file:
m = re.match(pattern,line)
if m:
hosts.add(m.group(1))
return hosts
def read_ufw():
pattern = re.compile(r"\[ *(\d+)\] 30303.+ALLOW IN\s+([0-9.:]+)")
rules = {}
ufw = subprocess.run(["/usr/sbin/ufw", "status", "numbered"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
lines = ufw.stdout.decode('utf-8').split("\n")
for line in lines:
m = re.match(pattern,line)
if m:
rules[m.group(2)] = m.group(1)
return rules
def remove_old(allowed, actual):
to_remove = list(set(actual.keys()) - allowed)
to_remove.sort(reverse=True, key=actual.get)
for rule in to_remove:
subprocess.run(ufw_delete + [actual[rule]], check=True)
def add_allowed(allowed):
for host in allowed:
command = ufw_command + ["allow", "from", host, "to", "any", "port", "30303"]
subprocess.run(command, check=True)
allowed = read_hosts()
actual = read_ufw()
remove_old(allowed,actual)
add_allowed(allowed)
#!/usr/bin/env python3
import sys, json
base_content = """127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
# BFA nodes
"""
if __name__ == '__main__':
if len(sys.argv) > 1:
filename = sys.argv[1]
file = open(filename)
if not file:
print("No se pudo abrir el archivo '{}'".format(filename), file=sys.stderr)
sys.exit(1)
else:
file = sys.stdin
try:
json_contents = json.load(file)
sealers = json_contents['sealers']
print(base_content)
for k, v in sealers.items():
nodes = v.get('nodes',[])
k = k.lower()
for node in nodes:
ipv4 = node.get('ipv4',None)
if ipv4:
print("{:30}\t{}-{}".format(ipv4[0],k,node['type']))
ipv6 = node.get('ipv6',None)
if ipv6:
print("{:30}\t{}-{}-ipv6".format(ipv6[0],k,node['type']))
except json.decoder.JSONDecodeError:
print("Error al decodificar JSON", file=sys.stderr)
sys.exit(1)
#!/usr/bin/env python3
import sys, json, os.path, re
db = {}
account_pattern = re.compile(r'("?0x[0-9a-f]{40}"?)')
def replace(m, length):
account = m.group(0)
quoted = False
if account[0] == '"' and account[-1] == '"':
quoted = True
account = account[1:-1]
replacement = db.get(account, account)
if quoted:
return '"{}"'.format(replacement)
else:
return "{0:{1}}".format(replacement,max_len)
if __name__ == '__main__':
if len(sys.argv) > 1:
filename = sys.argv[1]
file = open(filename)
if not file:
print("No se pudo abrir el archivo '{}'".format(filename), file=sys.stderr)
sys.exit(1)
else:
print("Uso: {} <lista_de_selladores.json>".format(os.path.basename(sys.argv[0])), file=sys.stderr)
sys.exit(1)
try:
json_contents = json.load(file)
sealers = json_contents['sealers']
max_len = 0
for k, v in sealers.items():
nodes = v.get('nodes',[])
k = k.lower()
for node in nodes:
account = node.get('account',None)
if account:
value = "{}-{}".format(k,node['type'])
max_len = max(max_len, len(value))
db[account.lower()] = value
for line in sys.stdin.readlines():
print(re.sub(account_pattern,lambda m: replace(m,max_len), line), end="")
except json.decoder.JSONDecodeError:
print("Error al decodificar JSON", file=sys.stderr)
sys.exit(1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment