title: Automate whitelisting ip addresses in iptables via bash
body: <p>I am looking for the best way to automate whitelisting IP addresses into <code>iptables</code>. The list of ip addresses and ports comes from a JSON file <code>/accept-rules.json</code> which is formatted like:</p>
  
  <pre><code>[
    {
      "ip": "1.2.3.4",
      "cidr": 32,
      "protocol": "tcp",
      "port": 3306
    },
    {
      "ip": "2.4.5.6",
      "cidr": 32,
      "protocol": "tcp",
      "port": 80
    },
    {
      "ip": "5.6.7.8",
      "cidr": 32,
      "protocol": "tcp",
      "port": 443
    },
    {
      "ip": "6.8.3.1",
      "cidr": 32,
      "protocol": "tcp",
      "port": 53
    }
  ]
  </code></pre>
  
  <p>I need a <code>bash</code> or <code>python</code> script which reads the json file and creates <code>ACCEPT</code> iptables rules. Example <code>ACCEPT</code> rules based on the <code>json</code> above should look like:</p>
  
  <pre><code>iptables -A INPUT -s 1.2.3.4/32 -p tcp -m tcp --dport 3306 -j ACCEPT
  iptables -A INPUT -s 2.4.5.6/32 -p tcp -m tcp --dport 80 -j ACCEPT
  iptables -A INPUT -s 5.6.7.8/32 -p tcp -m tcp --dport 443 -j ACCEPT
  iptables -A INPUT -s 6.8.3.1/32 -p tcp -m tcp --dport 53 -j ACCEPT
  </code></pre>
  
  <p>Any idea of the best way to code this up?</p>