歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> 在Linux下實現動態IP的域名自動指向

在Linux下實現動態IP的域名自動指向

日期:2017/3/6 15:50:12   编辑:關於Unix
在Internet上,用戶的域名和IP地址是一一對應的。但以虛擬撥號的方式上網,則產生了動態IP地址。這對於沒有太多錢申請域名的電腦 愛好者造成了麻煩。前一段日子,由於筆者所在地區的ADSL改為虛擬撥號方式,這麻煩也困擾了筆者好一陣。經多次調試,終於實現了
  在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.clearcase/" target="_blank" >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上順利通過。

Copyright © Linux教程網 All Rights Reserved