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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-19-2011, 02:59 PM
|
#1
|
|
LQ Newbie
Registered: Jun 2009
Distribution: Debian GNU/Linux
Posts: 29
Rep:
|
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.
|
|
|
|
09-20-2011, 04:57 AM
|
#2
|
|
Member
Registered: Aug 2011
Location: Croatia
Distribution: Debian 6/Ubuntu 11.04
Posts: 56
Rep:
|
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,
|
|
|
|
09-20-2011, 10:16 AM
|
#3
|
|
LQ Newbie
Registered: Jun 2009
Distribution: Debian GNU/Linux
Posts: 29
Original Poster
Rep:
|
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.
|
|
|
|
09-21-2011, 01:15 AM
|
#4
|
|
Member
Registered: Aug 2011
Location: Croatia
Distribution: Debian 6/Ubuntu 11.04
Posts: 56
Rep:
|
Quote:
Originally Posted by koosha
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.
|
|
|
|
09-21-2011, 01:39 AM
|
#5
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
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.
|
|
|
|
09-21-2011, 04:00 AM
|
#6
|
|
LQ Newbie
Registered: Jun 2009
Distribution: Debian GNU/Linux
Posts: 29
Original Poster
Rep:
|
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>';
}
?>
|
|
|
|
12-25-2012, 09:31 AM
|
#7
|
|
LQ Newbie
Registered: Dec 2012
Posts: 1
Rep: 
|
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.
|
|
|
|
12-25-2012, 02:40 PM
|
#8
|
|
LQ Newbie
Registered: Jun 2009
Distribution: Debian GNU/Linux
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by yonux
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:51 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|