{"id":1721,"date":"2024-03-11T04:00:16","date_gmt":"2024-03-11T07:00:16","guid":{"rendered":"https:\/\/blog.abratel.com.br\/?p=1721"},"modified":"2024-03-11T04:00:17","modified_gmt":"2024-03-11T07:00:17","slug":"script-linux-to-check-dynamic-source-ip-and-add-to-iptables-whitelist","status":"publish","type":"post","link":"https:\/\/blog.abratel.com.br\/?p=1721","title":{"rendered":"Script Linux to check Dynamic Source IP and Add to IPTables Whitelist"},"content":{"rendered":"<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\n#!\/bin\/bash\n# DNS address to resolve\nDNS_ADDRESS=&quot;server.ddns.com&quot;\n# Path to store the last resolved IP\nIP_FILE=&quot;\/etc\/asterisk\/last_ip.txt&quot;\n\n# Function to resolve DNS and update iptables rule\n# Resolve DNS to get the current IP\nNEW_IP=$(dig +short $DNS_ADDRESS | tail -1)\n\n# Read the last saved IP\nLAST_IP=$(cat $IP_FILE 2&amp;gt;\/dev\/null)\n\n# Compare the IPs\nif [ &quot;$NEW_IP&quot; != &quot;$LAST_IP&quot; ]; then\n# Delete old iptables rule if it exists\nsudo iptables -D INPUT -s $LAST_IP -j ACCEPT 2&amp;gt;\/dev\/null\n# Update iptables rule\nsudo iptables -I INPUT -s $NEW_IP -j ACCEPT 2&amp;gt;\/dev\/null\n# Save the new IP to file\necho $NEW_IP &amp;gt; $IP_FILE\n#        echo &quot;IP updated to $NEW_IP&quot;\n#    else\n#        echo &quot;IP is already up to date&quot;\nfi\n<\/pre>\n<p>Execute Permission:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nChmod +x \/etc\/asterisk\/script_atualiza_IP\n<\/pre>\n<p>Crontab:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n*\/59 * * * * root \/etc\/asterisk\/script_atualiza_ip\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Execute Permission: Crontab:<\/p>\n","protected":false},"author":1,"featured_media":1724,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/1721"}],"collection":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1721"}],"version-history":[{"count":2,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/1721\/revisions"}],"predecessor-version":[{"id":1726,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/1721\/revisions\/1726"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/media\/1724"}],"wp:attachment":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}