用户名:
密 码:
 
 
软 件 安 装
软 件 知 识
经 验 技 巧
学 习 指 南
相 关 下 载
站内搜索
 
 
首页 >> linux

在linux下实现动态IP的域名自动指向

作者:未知   时间:2004-07-27 0:23:28

在Internet上,用户的域名和IP地址是一一对应的。但以虚拟拨号的方式上网,则产生了动态IP地
址。这对于没有太多钱申请域名的电脑爱好者造成了麻烦。前一段日子,由于笔者所在地区的ADSL
改为虚拟拨号方式,这麻烦也困扰了笔者好一阵。经多次调试,终于实现了在Linux下动态IP地址

 
之前,笔者在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值,
产生中间文件outfile
cp /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",i,ip);}
else{j=i; printf("j=%d",j); }}
for (i=0; i<=j;i++) dip=ip;
dip[j]='';
printf("a=%s",dip);
fclose(fp);
strcpy(inip,na);
printf("inip=%s",inip);
strcat(inip,dip);

printf("inip=%s",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上顺利通过。

[ 责编:speedcat 点击: ]

浏览评论 | 发给好友 | 打印版本

 相关文章


Copyright © 2004 SunCN.Net All rights reserved. 阳光网络 版权所有 Power by iWPC