我遇到的问题是,如果我在本地wampserver上运行我的脚本,那么如果它已经列入黑名单,它会显示正确的结果但是如果我在我的服务器上运行等... / usr / bin / php path-to -phpscript.php然后它显示这个ip在所有bls名称中被列入黑名单,这是不正确的。
<?php
$bls = array("b.barracudacentral.org",
"bl.score.senderscore.com",
"pbl.spamhaus.org",
"sbl.spamhaus.org",
"xbl.spamhaus.org",
"zen.spamhaus.org",
"dbl.spamhaus.org",
"sbl-xbl.spamhaus.org",
);
$ip = '62.213.183.192';
if ( isset($ip)) {
if ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) || false === filter_var($ip, FILTER_VALIDATE_URL)) {
if (false === filter_var($ip, FILTER_VALIDATE_URL )) {
$iptosplit = gethostbyname($ip);
}
else {
$iptosplit = $ip;
}
$splitip = explode (".", $iptosplit);
$iptolookup = "$splitip[3].$splitip[2].$splitip[1].$splitip[0]";
$counter=1;
$blList = array();
foreach ( $bls as $rbl ) {
//echo "<tr>";
$rbllookup = $iptolookup.".".$rbl;
$lookup = gethostbyname($rbllookup);
if ( $lookup != $rbllookup || $lookup == $ip) {
$qtxtresult = dns_get_record("$rbllookup", DNS_TXT);
if ( ! isset($qtxtresult[0]['txt']) ) {
$qtxtresult[0]['txt'] = "";
}
$blList[$counter]=$ip.' is listed in ('.$rbl.')';
echo '...........Listed in -'.$rbl.'<br />';
}
echo str_repeat(" ", 24), "\n";
$counter++;
}
}
}
?>
答案 0 :(得分:0)
此代码已包含在dnsbl.tornevall.org的源代码段中(可通过https://dnsbl.tornevall.org/download/下载)并在ip被列入黑名单时返回一个数组。它还支持ipv6。如果它被列入黑名单,$ result [3]有一个正值,这是一个位掩码,并且位掩码值取决于ip被标记为黑名单类型的类型。
在这个例子中,我使用随机ip我知道在域中被列入黑名单。不幸的是,该脚本不支持TXT查找,但如果您只需要阻止来自列入黑名单的IP地址的连接,则此脚本也非常基本。
function rblresolve ($ip = '', $rbldomain = '')
{
if (!$ip) {return false;} // No data should return nothing
if (!$rbldomain) {return false;} // No rbl = ignore
// Old function (during betatesting we want to keep those rows so we can fall back if something fails)
// $returnthis = explode('.', gethostbyname(implode('.', array_reverse(explode('.', $ip))) . '.' . $rbldomain)); // Not ipv6-compatible!
// if (implode(".", $returnthis) != implode('.', array_reverse(explode('.', $ip))) . '.' . $rbldomain) {return $returnthis;} else {return false;}
// New ipv6-compatible function
$returnthis = (long2ip(ip2long($ip)) != "0.0.0.0" ? explode('.', gethostbyname(implode('.', array_reverse(explode('.', $ip))) . '.' . $rbldomain)) : explode(".", gethostbyname(v6arpa($ip) . "." . $rbldomain)));
// 127-bug-checking
if (implode(".", $returnthis) != (long2ip(ip2long($ip)) != "0.0.0.0" ? implode('.', array_reverse(explode('.', $ip))) . '.' . $rbldomain : v6arpa($ip) . "." . $rbldomain)) {return $returnthis;} else {return false;}
}
function v6arpa($ip)
{
$unpack = unpack('H*hex', inet_pton($ip));
$hex = $unpack['hex'];
return implode('', array_reverse(str_split($hex)));
}
$result = rblresolve("117.197.11.203", "dnsbl.tornevall.org");
print_r($result);
返回:
Array
(
[0] => 127
[1] => 0
[2] => 0
[3] => 67
)