Note that there are some explanatory texts on larger screens.

plurals
  1. POSplitting IPTables output to a multidimensional array for database
    text
    copied!<p>I'm trying to get the current IPTables rules in my Debian server neatly listed up in a database.</p> <p>To do this, I've written a few steps.</p> <ol> <li><p>First, we get the IPTables output with line number. (Input chain for example)</p> <p>iptables -L INPUT -nvx --line</p></li> </ol> <p>I get a neat output, that I want to get in my database the exact same way. For example; </p> <pre><code>Number, Chain, Protocol, Port, in, out </code></pre> <p>etcetera.</p> <p>To get this output to be compatible with importing to the database, I need to get it into a multidimensional array. This is where I'm stuck. It's needs to be like this;</p> <pre><code>[0] (entry of rule) [number],[chain],[protocol]... [1] [number],[chain],[protocol] </code></pre> <p>How exactly can I do this the most efficient way?</p> <p>-- UPDATED CODE --</p> <pre><code>function getIPTables() { $processHandle = popen('/usr/bin/sudo /sbin/iptables -L INPUT -nvx --line-numbers | tail -n +3', 'r'); $content = ''; while (!feof($processHandle)) { $content .= fread($processHandle, 4096); } pclose($processHandle); // break out all new lines into an array $lines = explode("\n", $content); foreach ($lines as $line) { $commands = array(); $segments = explode(" ", $line); $newEntry = array( 'Number' =&gt; $segments[0], 'Chain' =&gt; $segments[1], 'Protocol' =&gt; $segments[2], 'Port' =&gt; $segments[3], 'in' =&gt; $segments[4], 'out' =&gt; $segments[5] ); array_push($commands, $newEntry); print_r($commands); } } </code></pre> <p>-- OUTPUT --</p> <pre><code>root@debian:/home/michael/Documents/PHPCLIFILES# php getAllRules Local DB rules from users loaded in array PHP Notice: Undefined offset: 1 in /home/michael/Documents/PHPCLIFILES/getAllRules on line 47 PHP Notice: Undefined offset: 2 in /home/michael/Documents/PHPCLIFILES/getAllRules on line 48 PHP Notice: Undefined offset: 3 in /home/michael/Documents/PHPCLIFILES/getAllRules on line 49 PHP Notice: Undefined offset: 4 in /home/michael/Documents/PHPCLIFILES/getAllRules on line 50 PHP Notice: Undefined offset: 5 in /home/michael/Documents/PHPCLIFILES/getAllRules on line 51 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload