在Linux下实现动态IP的域名自动指向
在Internet上,用户的域名和IP地址是一一对应的。但以虚拟拨号的方式上网,则产生了动态IP地址。这对于没有太多钱申请域名的电脑爱好者造成了麻烦。前一段日子,由于笔者所在地区的ADSL改为虚拟拨号方式,这麻烦也困扰了笔者好一阵。经多次调试,终于实现了在Linux下动态IP地址?br>挠蛎?远?赶颉O纸?浼锹枷吕矗?氪蠹夜蚕恚?之前,笔者在http://www.deerfield.com/download/dns2go/linux/index.htm为自设的服务器申请了免费域名,把dns2go放在/etc/rc.d/rc.local里,开机即可启动固定的免费域名。但ADSL改为虚拟拨号的方式后,没有固定IP地址,启动dns2go很麻烦,先用ifconfig指令查出ppp0得到的IP,再在/etc/dns2go.conf下修改IP,然后才能启动dns2go。每次的启动都要使用手工操作,很不方便。在没有人的干预情况下,不能自动启动它。写了个C程序来解决问题,例中所有程序都假设安装在/usr/local/bin/下,编写个shell程序来调用C程序,目录含有xnbh(shell程序),trans(C程序编译后执行程序),outfile(中间临时文件),pppoe.txt(中间文件),dns2go.conf(典型的配置文件?br>?
xnbh的shell程序如下:
adsl-start#启动adls的虚拟拨号程序,ifconfig ppp0 |grep -v "Link">/usr/local/bin/pppoe.txt#在目录中产生pppoe.txt文件/usr/local/bin/trans#调用C程序提取文件pppoe.txt中的IP值,然后替换dns2go.conf中的IP值,产生中间文件outfilecp /etc/dns2go.conf /etc/dns2go.confold #备份原来的dns2go.conf文件cp /usr/local/bin/outfile /etc/dns2go.conf #替换后的dns2go.conf文件nohup /usr/local/bin/dns2go >/var/tmp/dns2go.log 2>&1 启动固定域名
在xnbh执行第二句后产生的pppoe.txt如下:
inet addr:218.65.217.109
P-t-P:172.0.0.1
Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST
MTU:1492
Metric:1
RX packets:26 errors:0 dropped:0 overruns:0 frame:0
TX packets:27 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:1400 (1.3 Kb)
TX bytes:1358 (1.3 Kb)
trans.c原程序如下:
#include <stdio.h>
#include <ctype.h>
main()
{
FILE *fp ,*bp,*out;
int
i=0;
int j;
char ip[16];
char dip[16];
char na[]="address=";
char inip[24];
char readln [255];
char readlnn [255];
fp=fopen("/usr/local/bin/pppoe.txt","r");
if(fgets(readln,255,fp)!=NULL)
{for (i=0;i<16;i++)
ip=readln[20+i];
}
i=0;
for (i=0;i<16;i++)
{ if(ip!=' ')
{dip=ip;
printf("ip%d=%c\n",i,ip);}
else{j=i; printf("j=%d",j); }}
for (i=0; i<=j;i++) dip=ip;
dip[j]='\0';
printf("a=%s\n",dip);
fclose(fp);
strcpy(inip,na);
printf("inip=%s\n",inip);
strcat(inip,dip);
printf("inip=%s\n",inip);
fp=fopen("/usr/local/bin/dns2go.conf","r");
out=fopen("/usr/local/outfile","w");
while (!feof(fp)){
if(fgets(readlnn,255,fp)!=NULL)
if (strncmp("address=aaa.bbb.ccc.ddd",readlnn,15)) fputs(readlnn,out);
else fputs(inip,out);}
fclose(fp);
fclose(out);
}
在linux下用gcc ?o trans trans.c编译通过产生文件trans,
dns2go.conf的配置如下:
#
# Simple configuration file for DNS2Go linux client
#
# For more information refer to the dns2go.conf manual page
#
# GLOBAL DIRECTIVES
#
# DNS2Go Server
server = discovery.dns2go.com
# Key (required, case-sensitive)
key =aaaaaa-bbbbbb-ccccccc-ddddd(此项为申请的激活码)
# log file (optional, default: syslog)
#log = /var/log/dns2go.txt
# Send debug output to stdout
debug
# number of connection attempts (optional, default: 8)
#retries = 8
# rate to send heartbeats (in minutes) (optional, default: 0=automatic)
#heartbeat-rate = 0
#
# DOMAIN DIRECTIVES
#
# Fully qualified domain name (required)
domain =XXXXX.dnsgo.com(此项填写固定免费域名)
# static IP address (optional, default: external IP address)
address=aaa.bbb.ccc.ddd(此项为假设IP)
# online www alias redirection: choose one of the following:
# (optional, default: www alias resolves to domain IP)
#www-redirect-url = http://...
#www-redirect-port = 8080
# offline redirect option: choose one of the following
# (optional, default: offline status page)
#offline-url = http://...
#offline-ip
= 0.0.0.0
以上xnbh程序若加入/etc/rc.d/rc.local/中则可以启动时自动启动固定免费域名,此时你可以启动的apache,ftp,telnet等都可以用此免费域名了,出门在外的朋友可以很方便找到自己的服务器,熟悉shell的朋友也可以用shell完成C程序的功能,以上的程序在redhat7.2上顺利通过。