土法炼钢:服务器定时汇报IP地址给我

我家里面的服务器,每过两天会自动重新拨号,所以公网IP地址天会变的。

而一旦IP地址发生变化,我就没有办法从公网访问我家里的服务器了。


怎么办呢?

1. 在另外一台公网服务器上放一个php程序 myip.php,检测客户端的IP地址

<?php

$reIP=$_SERVER["REMOTE_ADDR"]; 
echo $reIP; 

$file = fopen("myip.txt","w");
echo fwrite($file, $reIP);
fclose($file);

?>


2.在家里服务器上面写一个定时作业脚本xxx.bat

脚本内容:

wget http://zzz.zzz.zzz/myip.php

注意:wget.exe程序要放在C:\Windows目录下面


详细代码如下



<?php
	include('Mail.php');

	$filename = "xxxx.txt";
	$file = fopen($filename, 'r');
	$oldIP = fread($file, filesize($filename));
	fclose($file);
	echo 'Old IP is ' . $oldIP;
	echo '<br/>';

	$clientIP=$_SERVER["REMOTE_ADDR"]; 
	echo 'New IP is ' . $clientIP;
	echo '<br/>';

	

	$file = fopen($filename,"w");
	fwrite($file, $clientIP);
	fclose($file);

	date_default_timezone_set("PRC");
	$message = date("Y-m-d H:i:s") . ' xxxx IP';

	
	if( strcmp($clientIP, $oldIP) == 0) {
		$message = 'OK ' . $message . ' is not changed ' . $clientIP;
	}	else {
		$message = 'ALERT! ' . $message . ' is changed to ' . $clientIP;
	}
	echo $message;
	echo '<br/>';
	sendmail($message);
	
	//send email............................................
	
	
	function sendmail($message){
		$recipients = '[email protected]';

		$headers['From']    = 'xxxx';
		$headers['To']      = 'xxxxx';
		$headers['Subject'] = $message;

		$body = $message ;

		$smtpinfo["host"] = "localhost";
		$smtpinfo["port"] = "25";
		$smtpinfo["auth"] = true;
		$smtpinfo["username"] = "xxxx";
		$smtpinfo["password"] = "xxxxx";


		// Create the mail object using the Mail::factory method
		$mail_object =& Mail::factory("smtp", $smtpinfo); 

		$mail_object->send($recipients, $headers, $body);
		echo "mail sent"; 
	}
?>


你可能感兴趣的:(土法炼钢:服务器定时汇报IP地址给我)