说明:
正常web服务器下都会有多个域名,少则几个,多则上百。如果服务器ip更换,我们应该如何快速批量测试域名跟ip的对应关系呢?
本文通过createip.php在web服务器各站点根目录新建ipcheck.php文件,ipcheck.php打印出服务器ip;然后在任意台服务器上执行php checkip.php,通过域名去访问前面生成的ipcheck.php,检查域名跟新ip是否对应。
这边说明下,我站点目录是以/www/一级域名/二级域名方式存放的,如mail.coolnull.com域名,则站点目录为/www/coolnull.com/mail这样的格式
实现:
一.createip.php部份,通过执行/usr/local/php/bin/php /shell/createip.php,在各个域名站点根目录下生成ipcheck.php文件,ipcheck.php内容如下:
# less createip.php //createip.php内容如下 read())) { #echo $www.$entry."\n"; if($entry!='.' && $entry!='..' && is_dir($www.$entry)) { $d1 = dir($www.$entry.'/'); while (false !== ($entry1 = $d1->read())) { if($entry1!='.' && $entry1!='..' && is_dir($www.$entry.'/'.$entry1)) { echo $www.$entry.'/'.$entry1.'/ipcheck.php'."\n"; file_put_contents($www.$entry.'/'.$entry1.'/ipcheck.php',''); } } } else { continue; } } $d->close(); ?>
二.checkip.php部份,在任意台服务器上新建checkip.php。$all = array定义域名与ip关系,多ip的以;分隔
# cat /shell/checkip.php #!/usr/local/php/bin/php $a[$i],'has'=>0); array_push($tmp['ip'],$tmpip); } array_push($urls,$tmp); } define('LOGFILE','checkip.log'); if(file_exists(LOGFILE)) { rename(LOGFILE,LOGFILE.'.'.date('YmdHis')); } /* print_r($urls); exit; $urls = array(); $tmp = array(); $tmp['url'] = 'gamepay.geekoom.com'; $tmp['ip'] = array(); $tmpip = array('ip'=>'117.27.152.24','has'=>0); array_push($tmp['ip'],$tmpip); $tmpip = array('ip'=>'117.27.152.120','has'=>0); array_push($tmp['ip'],$tmpip); array_push($urls,$tmp); */ foreach($urls as &$v) { for($i=0; $i<10; $i++) { $ip = file_get_contents('http://'.$v['url'].'/ipcheck.php?'.rand(0,10000)); if(strlen($ip)>20) { errors($ip); errors($v['url']); exit; } errors($v['url'].'=>'.$ip."\n"); $v = checkip($v,$ip); } } errors(print_r($urls,true)); function checkip($url,$ip) { $err = true; foreach($url['ip'] as &$v) { if($v['ip']==$ip) { $err = false; $v['has']++; } } if($err) { if(is_array($url['error'])) { if(!in_array($ip,$url['error'])) { array_push($url['error'],$ip); } } else { $url['error'] = array(); array_push($url['error'],$ip); } } return $url; } function errors($log) { echo $log."\n"; @error_log($log.chr(13).chr(10),3,LOGFILE); } ?>
三.最后,执行checkip.php查看当前目录下的日志信息即可!
# php checkip.php
结果:
# less checkip.log //测试结果会如下所示,如果同一域名有多个ip,程序会分别测试
www.coolnull.com=>222.222.222.222 www.coolnull.com=>111.111.111.111 www.coolnull.com=>111.111.111.111 www.coolnull.com=>111.111.111.111 www.coolnull.com=>222.222.222.222 www.coolnull.com=>222.222.222.222 www.coolnull.com=>111.111.111.111 www.coolnull.com=>222.222.222.222 www.coolnull.com=>111.111.111.111 www.coolnull.com=>222.222.222.222 Array ( [0] => Array ( [url] => www.coolnull.com [ip] => Array ( [0] => Array ( [ip] => 117.27.139.153 [has] => 5 ) [1] => Array ( [ip] => 117.27.139.154 [has] => 5 ) ) ) )
附录:
暂空!