lzquan 发表于 2013-1-24 21:13:36

NSIS获取本机IP地址写入配置文件

安装一个应用程序的时候需要获取当前的网卡IP地址,即服务器IP地址,同时写入到配置文件。NSIS提供了获取IP地址的插件,IP-plugin,详情请看:http://nsis.sourceforge.net/IP_plug-in
插件安装很简单,只需要下载dll文件,拷贝到NSIS安装目录的plugin目录下即可。如果还有其它文件如nsi文件,另见插件安装说明。
outfile IpTest.exename IpTest ; get_ip example; by Hendri Adriaens; HendriAdriaens@hotmail.com;; Usage:; ip::get_ip; Output: 'ip1;ip2;ip3;ip4;'; (semi-colon delimited IP's list, on stack);; Uses NSIS script to retrieve; separate IP-addresses and to; test whether or not it is an; internet IP-address. Based on; information by Joost Verburg:;   These ranges are for local networks: ;   10.x.x.x / 255.0.0.0 ;   172.16.x.x / 255.255.0.0 to 172.31.x.x / 255.255.0.0 ;   192.168.x.x / 255.255.255.0 ;   169.254.x.x / 255.255.0.0; and:;   169.254.x.x is an Automatic Private IP Address,;   which you get when there is no DHCP server available,;   for example, Windows gives these addresses when you check;   "Obtain an IP address automatically" and you have no DHCP server.;; Further information has also been found at:; http://home.t-online.de/home/TschiTschi/ip_adressierung.htm;; Script supplies two funcions:; GetNextIp : get any IP (network and internet); CheckIP   : determine IP type (see function header for available types); Script uses the VersionCheck; function to test IP's.Section; Code to use actual ExtensionDLL; Current script provides an example   ip::get_ipPop $0   ; test entry;StrCpy $0 '192.168.0.100;127.0.0.1;152.168.0.101;169.254.0.1;'   Loop:Push $0Call GetNextIpCall CheckIpPop $2 ; Type of current IP-addressPop $1 ; Current IP-addressPop $0 ; Remaining addressesStrCmp $2 '1' '' NoLoopBackIp    MessageBox MB_OK "LoopBack IP-address: $1"    Goto ContinueNoLoopBackIp:StrCmp $2 '2' '' NoAPA    MessageBox MB_OK "Automatic Private IP-address: $1"    Goto ContinueNoAPA:StrCmp $2 '3' '' NoLanIp    MessageBox MB_OK "Network IP-address: $1"    Goto ContinueNoLanIp:MessageBox MB_OK "Internet IP-address: $1"Continue:StrCmp $0 '' ExitLoop LoopExitLoop:SectionEnd ; Function GetNextIp; input: head of stack; format: 'ip1;ip2;ip3;ip4;'; output: 'ip1' head of stack;         'ip2;ip3;ip4;' second entry of stack Function GetNextIpExch $0Push $1Push $2Strcpy $2 0             ; CounterLoop:    IntOp $2 $2 + 1    StrCpy $1 $0 1 $2    StrCmp $1 '' ExitLoop    StrCmp $1 ';' '' Loop    StrCpy $1 $0 $2       ; IP-address    IntOp $2 $2 + 1    StrCpy $0 $0 '' $2    ; Remaining stringExitLoop:Pop $2Push $0Exch 2Pop $0Exch $1FunctionEnd ; Function CheckIP; input: IP-address on stack; output: additional entry on stack;         1 - LoopBack IP (localhost, indicates no connection to a LAN or to the internet).;         2 - Automatic Private IP Address (no DHCP server).;         3 - Network IP.;         4 - Internet IP.; Eg:; Push '192.168.0.100'; Call CheckIP; Pop $0 ; Contains '3'; Pop $1 ; Contains '192.168.0.100' Function CheckIPExch $0Push $1   ; Check 127.x.x.xPush '127.0.0.0'Push $0Call VersionCheckPop $1StrCmp $1 2 '' Range1   ; IP cannot be in range of LoopBack addressesPush '127.255.255.255'Push $0Call VersionCheckPop $1StrCmp $1 1 LoopBack      ; We found a LoopBack IP   ; Check 10.x.x.xRange1:Push '10.0.0.0'Push $0Call VersionCheckPop $1StrCmp $1 2 '' Range2   ; IP cannot be in range 1Push '10.255.255.255'Push $0Call VersionCheckPop $1StrCmp $1 1 LanIp         ; We found a LanIp   ; Check 172.16.x.x to 172.31.x.xRange2:Push '172.16.0.0'Push $0Call VersionCheckPop $1StrCmp $1 2 '' Range3   ; IP cannot be in range 2Push '172.31.255.255'Push $0Call VersionCheckPop $1StrCmp $1 1 LanIp         ; We found a LanIp   ; Check 192.168.x.xRange3:Push '192.168.0.0'Push $0Call VersionCheckPop $1StrCmp $1 2 '' Range4   ; IP cannot be in range 3Push '192.168.255.255'Push $0Call VersionCheckPop $1StrCmp $1 1 LanIp         ; We found a LanIp   ; Check 169.254.x.xRange4:Push '169.254.0.0'Push $0Call VersionCheckPop $1StrCmp $1 2 '' InternetIp ; It should be an internet IPPush '169.254.255.255'Push $0Call VersionCheckPop $1StrCmp $1 1 APA         ; We found an Automatic Private IP Address   Goto InternetIp         ; Remaining addresses are internet IPs   LoopBack:StrCpy $1 1Goto Exit   APA:StrCpy $1 2Goto Exit   LanIp:StrCpy $1 3Goto Exit   InternetIp:StrCpy $1 4   Exit:Exch $1Exch 1Exch $0Exch 1FunctionEnd ; Function VersionCheck; input: 'v1', 'v2' on stack; output 1 - if number 1 is newer;      2 - if number 2 is newer;      0 - if it is the same verion; Eg:; Push '3.5.1.4'; Push '3.5'; Call VersionCheck; Pop $0 ; now contains 1 Function VersionCheckExch $0 ;second versionnumberExchExch $1 ;first versionnumberPush $R0 ;counter for $0Push $R1 ;counter for $1Push $3 ;temp charPush $4 ;temp string for $0Push $5 ;temp string for $1StrCpy $R0 "-1"StrCpy $R1 "-1"Start:StrCpy $4 ""DotLoop0:IntOp $R0 $R0 + 1StrCpy $3 $0 1 $R0StrCmp $3 "" DotFound0StrCmp $3 "." DotFound0StrCpy $4 $4$3Goto DotLoop0DotFound0:StrCpy $5 ""DotLoop1:IntOp $R1 $R1 + 1StrCpy $3 $1 1 $R1StrCmp $3 "" DotFound1StrCmp $3 "." DotFound1StrCpy $5 $5$3Goto DotLoop1DotFound1:Strcmp $4 "" 0 Not4    StrCmp $5 "" Equal    Goto Ver2LessNot4:StrCmp $5 "" Ver2MoreIntCmp $4 $5 Start Ver2Less Ver2MoreEqual:StrCpy $0 "0"Goto FinishVer2Less:StrCpy $0 "1"Goto FinishVer2More:StrCpy $0 "2"Finish:Pop $5Pop $4Pop $3Pop $R1Pop $R0Pop $1Exch $0FunctionEnd 
运行上面这个例子你就可以看到结果了。
IP地址获取之后,写入配置文件就简单了。如果是ini文件,可以直接用WriteINIStr方法
指令<noscript src="/admin/blogs/339618/chmlink.js" type="text/javascript"></noscript>
WriteINIStr $TEMP\something.ini section1 something 123WriteINIStr $TEMP\something.ini section1 somethingelse 1234WriteINIStr $TEMP\something.ini section2 nsis true 
或者采用其它方式,如替换文件中字符串的方法:
见博客:http://lzquan.iteye.com/blog/339613
页: [1]
查看完整版本: NSIS获取本机IP地址写入配置文件