因为有台电脑做服务器,用的是PPPOE拨号,IP是动态的。万一IP更换需要知道服务的IP地址。
1、找一个空间,放一个asp的程序:
ip.asp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<%
function createfile(byval content,byval filedir)
on error resume next
dim obj : set obj = server.createobject("adodb.Stream")
obj.type = 2
obj.open
obj.charset = "GB2312"
obj.position = obj.Size
obj.writeText = content
obj.savetofile server.mappath(filedir), 2
obj.close
if err then err.clear: createfile = false else createfile = true
set obj = nothing
End function
Function GetIP()
Dim strIPAddr
If Request.ServerVariables("HTTP_X_FORWARDED_FOR") = "" Or InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), "unknown") > 0 Then
strIPAddr = Request.ServerVariables("REMOTE_ADDR")
ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",") > 0 Then
strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",")-1)
ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";") > 0 Then
strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";")-1)
Else
strIPAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
End If
getIP = Checkstr(Trim(Mid(strIPAddr, 1, 30)))
If getIP="" then getIP="127.0.0.1"
End Function
Function CheckStr(str)
CheckStr=replace(replace(replace(replace(str,"<","<"),">",">"),chr(13),"")," ","")
CheckStr=replace(replace(replace(replace(CheckStr,"'",""),"and",""),"insert",""),"set","")
CheckStr=replace(replace(replace(replace(CheckStr,"select",""),"update",""),"delete",""),chr(34),"")
CheckStr=replace(replace(replace(replace(CheckStr,"*",""),"=",""),"mid",""),"count","")
CheckStr=replace(replace(replace(replace(CheckStr,"%",""),",",""),"union",""),"where","")
CheckStr=replace(replace(replace(replace(replace(CheckStr,"(",""),")",""),Chr(0),""),"+",""),";","")
end Function
%>
<body>
<%
response.Write(GetIP()&"<Br>")
if(createfile(GetIP(),"ip.html")) then
response.Write("IP写入成功!")
else
response.Write("IP写入失败!")
end if
%>
</body>
</html>
2、然后在linux端安装小型的浏览器:lynx
apt-get install lynx
3、编辑cron任务
/etc/crontab加入
0 * * * * root lynx -dump http://XXX/ip.asp
这样服务器每小时就可以访问一次指定网址。