LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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, 03: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, 05:57 AM   #2
resolv_25
Member
 
Registered: Aug 2011
Location: Croatia
Distribution: Debian 6/Ubuntu 11.04
Posts: 56

Rep: Reputation: 9
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, 11: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, 02:15 AM   #4
resolv_25
Member
 
Registered: Aug 2011
Location: Croatia
Distribution: Debian 6/Ubuntu 11.04
Posts: 56

Rep: Reputation: 9
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, 02:39 AM   #5
corp769
Guru
 
Registered: Apr 2005
Posts: 5,814

Rep: Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001
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, 05: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, 10: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 10:36 AM.
 
Old 12-25-2012, 03: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:57 AM
FFMPEG maybe not an php library? Mkipz Linux - Newbie 2 06-25-2009 06:15 AM
PHP-style library for C/C++? DeNayGo Programming 6 03-24-2008 08:42 PM
RH 3 - Update PHP and GD Library SharkBait Red Hat 1 09-12-2006 07:10 AM
Re-compiling PHP with GD library ubers0ldat Linux - Software 1 08-20-2003 03:59 PM


All times are GMT -5. The time now is 02:59 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration