土法炼钢:服务器定时汇报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 = 'xxxxx@qq.com';

		$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"; 
	}
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值