我有一个提供 DynDns 服务的 ISP,我应该让服务器使用 http 与 ISP 的 DynDns 服务器进行通信。操作如下:
https://[username]:[password]@dyndns.myisp.com/nic/update?hostname=[host]&myip=[ip]
现在我的问题来了:我不知道如何让我的服务器“使用”这个 http 命令来更改 IP。我的意思是我知道 cronjobs 以及如何制作一个简单的 bash 脚本,但我不知道使用什么命令让我的机器“访问”这个页面。
答案1
通常服务器就是这样做的,它提供服务,假设你正在谈论的是 HTTP 服务器。
在大多数情况下,路由器 - DSL 调制解调器负责执行此类操作。请检查以下内容,看看是否适用于您的情况:
典型的家庭用户几乎每次连接到互联网或租约结束时都会从其 ISP 获取动态 IP 地址。
当此类用户想要托管一个可访问互联网的家庭服务器时,必须在以下两种解决方案之间进行选择:
- 支付静态 IP
- 查找动态 DNS 服务
使用第二种解决方案时,应注意以下事项:
- 用户保持 IP 的动态特性
- 每次为用户分配新的动态 IP 时,用户都会通知动态 DNS 服务。
- 动态 DNS 服务将用户的 IP 映射到 URL。
上述过程的第二部分通常由用户的路由器(DSL 调制解调器)或用户计算机(服务器)中运行的守护进程(服务)应用程序完成。该服务登录到动态 DNS 提供商并通知提供商用户当前的 IP 地址。
要完成上述任务,首先查看路由器 - DSM 调制解调器设置,这是最干净、最简单的方法。如果调制解调器不支持此功能,请咨询 ISP 是否提供用于此任务的应用程序。最后,每次获取新 IP 或每 n 分钟(通常不少于 10 分钟)在服务器中运行脚本。
向动态 DNS 服务报告已获取新 IP 的各种方法的示例:
/bin/bash #!/bin/bash
#################################################################
## ChangeIP.com bash update script ##
#################################################################
## Written 3/18/09 by Tom Rinker, released to the Public Domain##
#################################################################
## This is a simple bash script to preform a dDNS update with ##
## ChangeIP.com. It uses only bash and wget, and so should be ##
## compatible with virtually any UNIX/Linux based system with ##
## bash. It is intended to be executed as a cron job, and ##
## will only execute an update of dDNS records when the IP ##
## address changes. As ChangeIP.com dDNS records have a 5 min ##
## Time-To-Live, it is basically pointless and wasteful to ##
## execute it more often than every 5 minutes. This script ##
## supports logging all activity, in 3 available log levels, ##
## and supports simple management of log file size. ##
#################################################################
## To use this script: ##
## 1) set the variables in the script below ##
## 2) execute the script as a cron job ##
#################################################################
## WARNING: This script has two potential security holes. ##
## First, the username and password are stored plaintext in ##
## the script, so a system user who has read access to the ##
## script could read them. This risk can be mitigated with ##
## careful use of file permissions on the script. ##
## Second, the username and password will show briefly to other##
## users of the system via ps, w, or top. This risk can be ##
## mitigated by moving the username and password to .wgetrc ##
## This level of security is acceptable for some installations ##
## including my own, but may be unacceptable for some users. ##
#################################################################
################ Script Variables ###############################
IPPATH=/var/log/IP # IP address storage file
TMPIP=/tmp/tmpIP # Temp IP storage file
LOGPATH=/var/log/changeip.log # Log file
TEMP=/tmp/temp # Temp storage file
CIPUSER= # ChangeIP.com Username
CIPPASS= # ChangeIP.com Password
CIPSET=1 # ChangeIP.com recordset
LOGLEVEL=2 # 0=off,1=normal,2=verbose
LOGMAX=500 # Max log lines, 0=unlimited
#################################################################
# get current IP from ip.changeip.com, and store in $TEMP
wget -q -U "rinker.sh wget 1.0" -O $TEMP ip.changeip.com
# parse $TEMP for the ip, and store in $TMPIP
grep IPADDR < $TEMP | cut -d= -s -f2 | cut -d- -s -f1 > $TMPIP
# compare $IPPATH with $TMPIP, and if different, execute update
if diff $IPPATH $TMPIP > /dev/null
then # same IP, no update
if [ $LOGLEVEL -eq 2 ]
then # if verbose, log no change
echo "--------------------------------" >> $LOGPATH
date >> $LOGPATH
echo "No Change" >> $LOGPATH
echo -e "IP: \c" >> $LOGPATH
cat $IPPATH >> $LOGPATH
fi
else # different IP, execute update
wget -q -U "rinker.sh wget 1.0" -O $TEMP --http-user=$CIPUSER --http-password=$CIPPASS "https://nic.changeip.com/nic/update?cmd=update&set=$CIPSET"
if [ $LOGLEVEL -ne 0 ]
then # if logging, log update
echo "--------------------------------" >> $LOGPATH
date >> $LOGPATH
echo "Updating" >> $LOGPATH
echo -e "NewIP: \c" >> $LOGPATH
cat $TMPIP >> $LOGPATH
if [ $LOGLEVEL -eq 2 ]
then # verbose logging
echo -e "OldIP: \c" >> $LOGPATH
cat $IPPATH >> $LOGPATH
cat $TEMP >> $LOGPATH # log the ChangeIP.com update reply
fi
fi
cp $TMPIP $IPPATH # Store new IP
fi
# if $LOGMAX not equal to 0, reduce log size to last $LOGMAX number of lines
if [ $LOGMAX -ne 0 ]
then
tail -n $LOGMAX $LOGPATH > $TEMP
cp $TEMP $LOGPATH
fi