本帖最后由 moech 于 2011-6-5 13:18 编辑 #AutoIt3Wrapper_Res_FileVersion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=希网动态域名更新器
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#EndRegion
#include "WinHttp.au3" ;需要用到winhttp的UDF,没有的附件里下载
#NoTrayIcon
Opt("MustDeclareVars", 1)
If $CmdLine[0] <> 0 Then ; /i 开机自启动 /u 删除开机启动
If StringUpper(StringMid($CmdLine[1], 2, 1)) = "I" Then
_Install()
ElseIf StringUpper(StringMid($CmdLine[1], 2, 1)) = "U" Then
_UnInstall()
Else
MsgBox(64, "提示", @CRLF & "参数:" & @CRLF & @CRLF & " /I 添加开机启动 " & @CRLF & @CRLF & " /U 删除开机启动 " & @CRLF, 5)
EndIf
Else
_Main()
EndIf
Func _Install()
RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "动态域名更新器")
RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "动态域名更新器", "REG_SZ", @ScriptFullPath)
EndFunc ;==>_Install
Func _UnInstall()
RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "动态域名更新器")
EndFunc ;==>_UnInstall
Func _DynDns()
Local $dyndns = "xxx.3322.org" ;你的希网动态域名
Local $User = "xxxxxx" ;希网的账号
Local $Pass = "xxxxxx" ;密码
Local $myip = _MyIP()
While $myip = 0 ;获取不到本机IP就一直循环
$myip = _MyIP()
Sleep(30000) ;休息30秒再获取本机IP
WEnd
Local $sHost = "members.3322.org"
Local $hOpen = _WinHttpOpen("Cn99CIP/1.1.0")
Local $hConnect = _WinHttpConnect($hOpen, $sHost)
; 指定请求
Local $hRequest = _WinHttpOpenRequest($hConnect, "GET", "/dyndns/update?system=dyndns&hostname=" & $dyndns & "&myip=" & $myip & "&wildcard=OFF&backmx=NO&offline=NO")
; 设置凭证
_WinHttpSetCredentials($hRequest, $WINHTTP_AUTH_TARGET_SERVER, $WINHTTP_AUTH_SCHEME_BASIC, $User, $Pass)
; 发送请求
_WinHttpSendRequest($hRequest)
; 等待响应
_WinHttpReceiveResponse($hRequest)
;下面隐去的是用来记录日志的 返回的数据 $sData
;~ Local $sChunk, $sData
;~ If _WinHttpQueryDataAvailable($hRequest) Then
;~ While 1
;~ $sChunk = _WinHttpReadData($hRequest)
;~ If @error Then ExitLoop
;~ $sData &= $sChunk
;~ WEnd
;~ FileWriteLine(StringTrimRight(@ScriptName, 4) & ".Log", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " - " & $sData)
;~ Return 1
;~ Else
;~ FileWriteLine(StringTrimRight(@ScriptName, 4) & ".Log", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " - Send Error " & $myip)
;~ Return 0
;~ EndIf
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
EndFunc ;==>_DynDns
Func _MyIP() ;获取本机IP
Local $sChunk, $sData
Local $sHost = "www.3322.org"
Local $hOpen = _WinHttpOpen("Cn99CIP/1.1.0")
Local $hConnect = _WinHttpConnect($hOpen, $sHost)
Local $hRequest = _WinHttpOpenRequest($hConnect, "GET", "/dyndns/getip")
_WinHttpSendRequest($hRequest, "Content-Type: application/x-www-form-urlencoded")
_WinHttpReceiveResponse($hRequest)
If _WinHttpQueryDataAvailable($hRequest) Then
While 1
$sChunk = _WinHttpReadData($hRequest)
If @error Then ExitLoop
$sData &= $sChunk
WEnd
Else
$sData = 0 ;获取失败 返回 0
EndIf
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Return StringStripCR(StringStripWS($sData, 8))
EndFunc ;==>_MyIP
Func _Main()
Local $existpid = ProcessList(@ScriptName)
If $existpid[0][0] > 1 Then ;如果本程序运行两个实例则退出
Exit
EndIf
While 1 ;程序运行后 在后台一直循环检测
If Ping("www.baidu.com") = 0 Then ;用来做开机的延迟运行
Sleep(10000) ;休息10s
Else
If _DynDns() Then
Sleep(900000) ;15分钟更新一次IP,不建议这里时间设置的太短
Else
Sleep(30000) ;更新失败时 先休息30s再说
EndIf
EndIf
WEnd
EndFunc ;==>_Main
附件:
Winhttp的UDF文件
Winhttp的中文使用帮助文件
|