@@ -22,6 +22,8 @@ class FailtwobanipCommand extends ConsoleCommand
2222{
2323 protected $ resultBanIps = [];
2424 protected $ resultUnBanIps = [];
25+ protected $ ignogeips ;
26+ protected $ ssh_port = 22 ;
2527 public function run ($ args )
2628 {
2729
@@ -53,19 +55,32 @@ public function run($args)
5355
5456
5557
58+ if (isset ($ args [0 ])) {
59+ $ this ->ssh_port = $ args [0 ];
60+ }
61+
62+
63+
5664 $ sql = 'SELECT ip FROM pkg_firewall WHERE action = 3 ' ;
5765 $ command = Yii::app ()->db ->createCommand ($ sql );
5866 $ this ->resultUnBanIps = $ command ->queryAll ();
5967
60- $ sql = 'DELETE FROM pkg_firewall WHERE action = 3 ' ;
61- $ command = Yii::app ()->db ->createCommand ($ sql );
62-
68+ foreach ($ this ->resultUnBanIps as $ unbanIP ) {
69+ $ sql = 'DELETE FROM pkg_firewall WHERE ip = " ' . $ unbanIP ['ip ' ] . '" ' ;
70+ Yii::app ()->db ->createCommand ($ sql )->execute ();
71+ }
6372
6473 $ sql = 'SELECT ip FROM pkg_firewall WHERE action = 1 ' ;
6574 $ command = Yii::app ()->db ->createCommand ($ sql );
6675 $ this ->resultBanIps = $ command ->queryAll ();
6776
6877
78+ echo "\n\nresultUnBanIps " ;
79+ print_r ($ this ->resultUnBanIps );
80+
81+ echo "\nresultBanIps " ;
82+ print_r ($ this ->resultBanIps );
83+
6984 $ sql = 'TRUNCATE TABLE pkg_firewall ' ;
7085 Yii::app ()->db ->createCommand ($ sql )->execute ();
7186
@@ -88,9 +103,17 @@ public function run($args)
88103 $ modelServers = Servers::model ()->findAll ('status IN (1,3,4) ' );
89104 }
90105
106+ $ this ->ignogeips = 'ignoreip = 127.0.0.1 ' ;
91107 foreach ($ modelServers as $ key => $ server ) {
108+ $ this ->ignogeips .= $ server ['host ' ] . " " ;
109+ }
92110
93- echo $ server ['host ' ] . "\n" ;
111+
112+ $ modelServers = Servers::model ()->findAll ('status IN (1,3,4) ' );
113+
114+ foreach ($ modelServers as $ key => $ server ) {
115+
116+ echo "\n" . $ server ['host ' ] . "\n\n" ;
94117
95118 if ($ server ['type ' ] == 'sipproxy ' ) {
96119
@@ -107,11 +130,18 @@ public function run($args)
107130
108131 public function getLinesCommand ($ command , $ action = 0 , $ server )
109132 {
133+
134+ echo "sed -i 's/^ignoreip = .*/ " . $ this ->ignogeips . "/' /etc/fail2ban/jail.local \n" ;
135+
110136 if ($ server ['type ' ] == 'mbilling ' ) {
111137
138+ shell_exec ("sed -i 's/^ignoreip = .*/ " . $ this ->ignogeips . "/' /etc/fail2ban/jail.local " );
139+ shell_exec ("systemctl reload fail2ban " );
140+
141+
112142 foreach ($ this ->resultUnBanIps as $ unbanIP ) {
113143
114- echo "unbanip IP " . $ unbanIP ['ip ' ] . "\n" ;
144+ echo "unbanip IP " . $ unbanIP ['ip ' ] . " on MASTER \n" ;
115145
116146 @shell_exec ("sudo fail2ban-client set asterisk-iptables unbanip " . $ unbanIP ['ip ' ]);
117147 @shell_exec ("sudo fail2ban-client set ip-blacklist unbanip " . $ unbanIP ['ip ' ]);
@@ -138,19 +168,22 @@ public function getLinesCommand($command, $action = 0, $server)
138168 $ status = shell_exec ("fail2ban-client status " . $ command );
139169 } else {
140170
171+ @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p ' . $ this ->ssh_port . ' "sed -i \'s/^ignoreip = .*/ ' . $ this ->ignogeips . '/ \' /etc/fail2ban/jail.local" ' );
172+ @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p ' . $ this ->ssh_port . ' "systemctl reload fail2ban" ' );
173+
141174 foreach ($ this ->resultUnBanIps as $ unbanIP ) {
142175
143- echo "unbanip IP " . $ unbanIP ['ip ' ] . "\n" ;
176+ echo "unbanip IP " . $ unbanIP ['ip ' ] . " on " . $ server [ ' host ' ] . " \n" ;
144177
145- @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p 22 "fail2ban-client set asterisk-iptables unbanip ' . $ unbanIP ['ip ' ] . '" ' );
146- @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p 22 "fail2ban-client set ip-blacklist unbanip ' . $ unbanIP ['ip ' ] . '" ' );
147- @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p 22 "fail2ban-client set sshd unbanip ' . $ unbanIP ['ip ' ] . '" ' );
148- @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p 22 "fail2ban-client set opensips-iptables unbanip ' . $ unbanIP ['ip ' ] . '" ' );
178+ @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p ' . $ this -> ssh_port . ' "fail2ban-client set asterisk-iptables unbanip ' . $ unbanIP ['ip ' ] . '" ' );
179+ @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p ' . $ this -> ssh_port . ' "fail2ban-client set ip-blacklist unbanip ' . $ unbanIP ['ip ' ] . '" ' );
180+ @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p ' . $ this -> ssh_port . ' "fail2ban-client set sshd unbanip ' . $ unbanIP ['ip ' ] . '" ' );
181+ @shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p ' . $ this -> ssh_port . ' "fail2ban-client set opensips-iptables unbanip ' . $ unbanIP ['ip ' ] . '" ' );
149182 }
150183
151184 if ($ command == 'ip-blacklist ' ) {
152185 foreach ($ this ->resultBanIps as $ blokedIP ) {
153- $ status = shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p 22 "fail2ban-client set ip-blacklist banip ' . $ blokedIP ['ip ' ] . '" ' );
186+ $ status = shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p ' . $ this -> ssh_port . ' "fail2ban-client set ip-blacklist banip ' . $ blokedIP ['ip ' ] . '" ' );
154187
155188 $ sqlCheck = "SELECT COUNT(*) FROM pkg_firewall WHERE ip = ' " . $ blokedIP ['ip ' ] . "' AND id_server = ' " . $ server ['id ' ] . "' " ;
156189 $ exists = Yii::app ()->db ->createCommand ($ sqlCheck )->queryScalar ();
@@ -165,8 +198,7 @@ public function getLinesCommand($command, $action = 0, $server)
165198 }
166199 }
167200
168-
169- $ status = shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p 22 "fail2ban-client status ' . $ command . '" ' );
201+ $ status = shell_exec ('ssh -o StrictHostKeyChecking=no root@ ' . $ server ['host ' ] . ' -p ' . $ this ->ssh_port . ' "fail2ban-client status ' . $ command . '" ' );
170202 }
171203
172204 preg_match ('/Banned IP list:\s*(.*)/ ' , $ status , $ ipMatches );
0 commit comments