Хорошо, у меня есть время, чтобы убить. Я предлагаю вам прочитать о том, как использовать preg_match()
, а также пересмотреть, как вы обрабатываете соединение с базой данных. Я также исправил кучу других мелких ошибок и ненужного кода.
<?php
$hostname = gethostname();
// this needs to be quoted
$name = "permanent";
require_once("/etc/blocked/inc/config.inc.php");
// specify the full path to your binary
$output = exec("/sbin/iptables -S permanent", $lines);
// exec will create an array
//$lines=explode("\n",$output);
// you weren't capturing the IP address here
$r="/((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))/";
foreach($lines as $line){
// this can create itself
// $t=array();
// why aren't you checking the results of this call?
if (preg_match($r, $line, $t)) {
// $t[0] is the whole string, $t[1] is the first match
$ip = $t[1];
// I don't know why you were re-assigning this to another array
// $fail[0]=$ip;
#echo "$hostname,$ip,$name \n";
$query = "INSERT INTO blockedips (hostname,ip,name) VALUES ('$hostname','$ip','$name')";
$result = mysqli_query($link,$query)
or die('Query failed: ' . mysqli_error($link));
// why close your database? your second query isn't going to work too well
// mysqli_close($link);
// oh, will never be a second value. is this intentional? why have a loop then?
// exit;
}
}
?>
Но подождите! Prepared statements изготавливаются один раз и выполняются повторно, одновременно снижая накладные расходы системы. Я также настоятельно рекомендую перейти на PDO или, по крайней мере, использовать mysqli object-oriented interface.
<?php
$hostname = gethostname();
$name = "permanent";
require_once("/etc/blocked/inc/config.inc.php");
$output = exec("/sbin/iptables -S $name", $lines);
$stmt = $link->prepare("INSERT INTO blockedips (hostname,ip,name) VALUES (?, ?, ?)";
$octet = "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
$ip = "$octet\.$octet\.$octet\.$octet";
foreach($lines as $line){
if (preg_match("/($ip)/", $line, $t)) {
$ip = $t[1];
$stmt->bind_param("sss", $hostname, $ip, $name);
if ($stmt->execute() === false) {
echo 'Query failed: ' . $link->error();
$link->close();
exit;
}
}
}
?>
Что содержит '$ output' и какие ошибки вы получаете? – AbraCadaver
Держу пари, кто-то может помочь, если они знают, в чем проблема. – miken32