@danielf wrote:
Hello,
After reading @jfinstrom's article (http://wiki.freepbx.org/display/FOP/Adding+fwconsole+commands, I started to build a small additional fwconsole command.
The purpose:
1. fix my predefined iptables rules and fail2ban settings as the freepbx start/stop functions flushes them.
2. run this fwconsole command after the start/stop/restart functions (fwconsole start/stop/restart
).My first try was to write a hook to the start function (really simple and no fancy stuff).
The process itself:
make a directory in your modules directory for your new command:
mkdir /var/www/html/admin/modules/fixiptables
make a Console sub directory in your
fixiptables
directory:mkdir /var/www/html/admin/modules/fixiptables/Console
.prepapre your
module.xml
file in the module directory:<module>
<rawname>fixiptables</rawname>
<repo>standard</repo>
<name>Fix Iptables Rules</name>
<hooks>
<framework class="Start" namespace="FreePBX\Console\Command">
<method callingMethod="postAsteriskHooks" class="Fixiptables">postAsteriskHooks</method>
</framework>
</hooks>
</module>make your class file in the Console directory (
Fixiptables.class.php
):<?php
//Namespace should be FreePBX\Console\Command
namespace FreePBX\Console\Command;
//Symfony stuff all needed add these
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
//progress bar
use Symfony\Component\Console\Helper\ProgressBar;
//Start class. Class name should be same as file name. Fixiptables.class.php
class Fixiptables extends Command {
//Declare component and your options.
protected function configure(){
$this->setName('fixiptables')
->setDescription('Fixes the iptables rules')
->setHelp('Fixes the iptables rules');
}
protected function execute(InputInterface $input, OutputInterface $output){
system('service iptables restart > /dev/null 2>&1');
system('service fail2ban restart > /dev/null 2>&1');
$output->writeln('iptables and fail2ban have been fixed');
}
public function postAsteriskHooks($output) {
system('service iptables restart > /dev/null 2>&1');
system('service fail2ban restart > /dev/null 2>&1');
$output->writeln('iptables and fail2ban have been fixed');
}
}
?>- Install the module:
fwconsole ma install fixiptables
- verify that I can see my command:
fwconsole | grep fixiptables
.- run the command manually:
fwconsole fixiptables
.Everything works great when I run the new fwconsole command:
fwconsole fixiptables
.My problem is that for some reason (I must be missing something) I cannot hook it to the start function of the fwconsole.
I tried implementing the code sample itself directly, but with no luck either hooking it to the start/stop functions.Can you help me to hook it to the start/stop/restart framework functions?
Thank you,
Daniel Friedman
Trixton LTD.
Posts: 11
Participants: 4