Slackjeff Wiki

Juntando bits aprendemos juntos

Ferramentas do usuário

Ferramentas do site


script:dynamichostsufw

Diferenças

Aqui você vê as diferenças entre duas revisões dessa página.

Link para esta página de comparações

Ambos lados da revisão anteriorRevisão anterior
script:dynamichostsufw [2025/04/16 14:31] – edição externa 127.0.0.1script:dynamichostsufw [2026/01/27 02:44] (atual) – removida gabrielovsky
Linha 1: Linha 1:
-<note important>Script retirado de: [[http://notepad2.blogspot.com/2012/06/shell-script-update-ufw-rules-for-hosts.html]]</note> 
  
-<code> 
-#!/bin/bash 
- 
-HOSTS_ALLOW=/etc/ufw-dynamic-hosts.allow 
-IPS_ALLOW=/var/tmp/ufw-dynamic-ips.allow 
- 
-add_rule() { 
-  local proto=$1 
-  local port=$2 
-  local ip=$3 
-  local regex="${port}\/${proto}.*ALLOW.*IN.*${ip}" 
-  local rule=$(ufw status numbered | grep $regex) 
-  if [ -z "$rule" ]; then 
-      ufw allow proto ${proto} from ${ip} to any port ${port} 
-  else 
-      echo "rule already exists. nothing to do." 
-  fi 
-} 
- 
-delete_rule() { 
-  local proto=$1 
-  local port=$2 
-  local ip=$3 
-  local regex="${port}\/${proto}.*ALLOW.*IN.*${ip}" 
-  local rule=$(ufw status numbered | grep $regex) 
-  if [ -n "$rule" ]; then 
-      ufw delete allow proto ${proto} from ${ip} to any port ${port} 
-  else 
-      echo "rule does not exist. nothing to do." 
-  fi 
-} 
- 
- 
-sed '/^[[:space:]]*$/d' ${HOSTS_ALLOW} | sed '/^[[:space:]]*#/d' | while read line 
-do 
-    proto=$(echo ${line} | cut -d: -f1) 
-    port=$(echo ${line} | cut -d: -f2) 
-    host=$(echo ${line} | cut -d: -f3) 
- 
-    if [ -f ${IPS_ALLOW} ]; then 
-      old_ip=$(cat ${IPS_ALLOW} | grep ${host} | cut -d: -f2) 
-    fi 
- 
-    ip=$(dig +short $host | tail -n 1) 
- 
-    if [ -z ${ip} ]; then 
-        if [ -n "${old_ip}" ]; then 
-            delete_rule $proto $port $old_ip 
-        fi 
-        echo "Failed to resolve the ip address of ${host}." 1>&2 
-        exit 1 
-    fi 
- 
-    if [ -n "${old_ip}" ]; then 
-        if [ ${ip} != ${old_ip} ]; then 
-            delete_rule $proto $port $old_ip 
-        fi 
-    fi 
-    add_rule $proto $port $ip 
-    if [ -f ${IPS_ALLOW} ]; then 
-      sed -i.bak /^${host}*/d ${IPS_ALLOW} 
-    fi 
-    echo "${host}:${ip}" >> ${IPS_ALLOW} 
-done 
-</code> 
- 
-The sample hosts file: /etc/ufw-dynamic-hosts.allow: 
- 
-<code> 
-tcp:22:yourpc.no-ip.org 
-</code> 
- 
-The crontab which execute the script every 5 minutes to update the rules: 
- 
-<code> 
-# m h  dom mon dow   command 
-*/5 * * * * /usr/local/sbin/ufw-dynamic-host-update  2>&1 > /dev/null 
-</code> 
script/dynamichostsufw.1744813880.txt.gz · Última modificação: por 127.0.0.1