路由器发送IP至服务器


路由器上

/etc/storage/ddns.sh

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
newip=$(ifconfig ppp0|grep inet|awk '{print $2}'|awk -F ':' '{print $2}')
oldip=$(cat /tmp/lastIP)
if [ $oldip == $newip ];then
logger "ddns: IP:"$oldip",IP has not changed"
echo "ddns: IP:"$oldip",IP has not changed"
else
info=$(curl http://andi.press/ebook/ddns.php?ip=$newip:10090 -s)
echo $newip > /tmp/lastIP
logger $info
echo $info
fi

运行命令

1
2
3
chmod +x /etc/storage/ddns.sh
crontab -e
0 * * * * /etc/storage/ddns.sh

服务器上

ddns.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
header("Content-type:text/html;charset=utf-8");
$ip = $_GET['ip'];
$host = '';
$port = '3306';
$user = '';
$password = '';
$database = '';
$con = mysql_connect($host.':'.$port,$user,$password);
mysql_query("SET NAMES 'UTF8'");
mysql_select_db($database,$con);
$query = "truncate ip";
$result = mysql_query($query,$con);
$query = "insert into ip(addr) values(\"".$ip."\")";
$result = mysql_query($query,$con);
if ($result)
echo "ddns: IP:".$ip.", Update succeed";
else
echo "ddns: IP:".$ip." Update failed";
mysql_free_result($result);
mysql_close($con);
?>