LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2011, 02:59 PM   #1
koosha
LQ Newbie
 
Registered: Jun 2009
Distribution: Debian GNU/Linux
Posts: 29

Rep: Reputation: 0
A PHP Library for IPTables


Dear users

I would like you to just know that I've written a lightweight library in PHP that enables you manipulate iptables chains and rules. You can download it at: https://sourceforge.net/projects/libiptphp/
Feel free to contact me in case of any comment.
 
Old 09-20-2011, 04:57 AM   #2
resolv_25
Member
 
Registered: Aug 2011
Location: Croatia
Distribution: Debian 10/Ubuntu 20.04
Posts: 64

Rep: Reputation: 15
Looks interesting.
Still, would be good to have some example of using this library ? Would be easier to understand the exact way to use the class.
Best regards,
 
Old 09-20-2011, 10:16 AM   #3
koosha
LQ Newbie
 
Registered: Jun 2009
Distribution: Debian GNU/Linux
Posts: 29

Original Poster
Rep: Reputation: 0
Of course an example will help you understand it. An article is going to be published in Linux Journal. But, I'm thinking of including a sample rule file and an example PHP script that uses it.
 
Old 09-21-2011, 01:15 AM   #4
resolv_25
Member
 
Registered: Aug 2011
Location: Croatia
Distribution: Debian 10/Ubuntu 20.04
Posts: 64

Rep: Reputation: 15
Quote:
Originally Posted by koosha View Post
Of course an example will help you understand it. An article is going to be published in Linux Journal. But, I'm thinking of including a sample rule file and an example PHP script that uses it.
Yes, this would be great.
Thank you.
 
Old 09-21-2011, 01:39 AM   #5
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Could you post a small example here so we could get a quick glimpse? I'm pretty sure that if one was within the forums, you would get more people to try your library out.
 
Old 09-21-2011, 04:00 AM   #6
koosha
LQ Newbie
 
Registered: Jun 2009
Distribution: Debian GNU/Linux
Posts: 29

Original Poster
Rep: Reputation: 0
OK. The following simple code lists tables, chains, and rules defined in iptables rules file.
Code:
<?php
    require_once 'libiptables.php';
    // Path of iptables rules. Yours may differ.
    $rules_file = '/etc/iptables/rules';
    $ipt = new IptablesConfig($rules_file);
    foreach ($ipt->getAllTables() as $t) { 
        echo "<pre>Table <b>$t</b><br/>";
	foreach ($ipt->getTableChains($t) as $c) {
	     echo "  Chain <b>$c</b><br/>";
                 foreach ($ipt->getAllRuleStrings($t, $c) as $r) {
		     echo "    <b>Rule</b> $r<br/>";
		}
	}
	echo '</pre>';
    }
?>
 
Old 12-25-2012, 09:31 AM   #7
yonux
LQ Newbie
 
Registered: Dec 2012
Posts: 1

Rep: Reputation: Disabled
hai koosha. thank you for your code.

i try to get byte couter using getRuleByteCounter function, but it shows nothing. here is my code, correct me please.
Code:
<?php
    require_once 'libiptables-php-1.0/libiptables.php';
    // Path of iptables rules. Yours may differ.
    $rules_file = '/etc/iptables/rules';
    $ipt = new IptablesConfig($rules_file);
    foreach ($ipt->getAllTables() as $t) { 
        echo "<pre>Table <b>$t</b><br/>";
	foreach ($ipt->getTableChains($t) as $c) {
	     echo "  Chain <b>$c</b><br/>";
                 foreach ($ipt->getRuleByteCounter($t, $c, 0) as $r) {
		     echo "    <b>bytes</b> $r<br/>";
		}
	}
	echo '</pre>';
    }
?>
could you give me an example for doing this please? i want to get bytes couter of rule #0.

thank you very much for help

Last edited by yonux; 12-25-2012 at 09:36 AM.
 
Old 12-25-2012, 02:40 PM   #8
koosha
LQ Newbie
 
Registered: Jun 2009
Distribution: Debian GNU/Linux
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by yonux View Post
hai koosha. thank you for your code.

i try to get byte couter using getRuleByteCounter function, but it shows nothing. here is my code, correct me please.
Code:
<?php
                 foreach ($ipt->getRuleByteCounter($t, $c, 0) as $r) {
		     echo "    <b>bytes</b> $r<br/>";
		}
?>
could you give me an example for doing this please? i want to get bytes couter of rule #0.

thank you very much for help

Two points:
1. Your rule MUST begin with counters; that is, it must have counters; otherwise it is reasonable that nothing is returned if your rule does not have counters. Rules which begin with [1234:5678] (ignore the numbers, consider the form [\d+:\d+]) have byte and packet counters set.

2. getRuleByteCounter() does not return an array; it returns an integer. So it should be used as a stand-alone statement not in a foreach loop.
 
  


Reply

Tags
firewall, iptables, network, php, web



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626/xsl malayo Debian 8 07-02-2013 04:57 AM
FFMPEG maybe not an php library? Mkipz Linux - Newbie 2 06-25-2009 05:15 AM
PHP-style library for C/C++? DeNayGo Programming 6 03-24-2008 07:42 PM
RH 3 - Update PHP and GD Library SharkBait Red Hat 1 09-12-2006 06:10 AM
Re-compiling PHP with GD library ubers0ldat Linux - Software 1 08-20-2003 02:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration