用VBS脚本查询纯真IP库QQWry.dat(转)
2012-04-08 22:43
375 查看
查询给的IP地址所在的国家,ADSL服务商名称:
前提:需要提供一个纯真的IP库,这个可以只需将从网上搜索QQWry.dat进行搜索这个文件下载即可,然后将该文件放到与该脚本同目录下即可。
原文地址:http://demon.tw/programming/vbs-qqwry-dat.html
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
前提:需要提供一个纯真的IP库,这个可以只需将从网上搜索QQWry.dat进行搜索这个文件下载即可,然后将该文件放到与该脚本同目录下即可。
原文地址:http://demon.tw/programming/vbs-qqwry-dat.html
无他,只不过想证明其他主流语言能实现的 VBS 不一定不能实现而已,而且早就已经有人实现了,只不过是在 ASP 中,作者不详。我测试了一下貌似能用,只不过这个类封装得不太好,我稍微修改了一下属性的访问修饰符。 Class TQQWry ' ============================================ ' 变量声名 ' ============================================ Public Country, LocalStr Public QQWryFile Private Buf, OffSet Private StartIP, EndIP, CountryFlag Private FirstStartIP, LastStartIP, RecordCount Private Stream, EndIPOff ' ============================================ ' 类模块初始化 ' ============================================ Private Sub Class_Initialize Country = "" LocalStr = "" StartIP = 0 EndIP = 0 CountryFlag = 0 FirstStartIP = 0 LastStartIP = 0 EndIPOff = 0 QQWryFile = "QQWry.Dat" End Sub ' ============================================ ' 类终结 ' ============================================ Private Sub Class_Terminate On ErrOr Resume Next Stream.Close If Err Then Err.Clear Set Stream = Nothing End Sub ' ============================================ ' IP地址转换成整数 ' ============================================ Function IPToInt(IP) Dim IPArray, i IPArray = Split(IP, ".", -1) FOr i = 0 to 3 If Not IsNumeric(IPArray(i)) Then IPArray(i) = 0 If CInt(IPArray(i)) < 0 Then IPArray(i) = Abs(CInt(IPArray(i))) If CInt(IPArray(i)) > 255 Then IPArray(i) = 255 Next IPToInt = (CInt(IPArray(0))*256*256*256) + (CInt(IPArray(1))*256*256) + (CInt(IPArray(2))*256) + CInt(IPArray(3)) End Function ' ============================================ ' 整数逆转IP地址 ' ============================================ Function IntToIP(IntValue) p4 = IntValue - Fix(IntValue/256)*256 IntValue = (IntValue-p4)/256 p3 = IntValue - Fix(IntValue/256)*256 IntValue = (IntValue-p3)/256 p2 = IntValue - Fix(IntValue/256)*256 IntValue = (IntValue - p2)/256 p1 = IntValue IntToIP = Cstr(p1) & "." & Cstr(p2) & "." & Cstr(p3) & "." & Cstr(p4) End Function ' ============================================ ' 获取开始IP位置 ' ============================================ Private Function GetStartIP(RecNo) OffSet = FirstStartIP + RecNo * 7 Stream.Position = OffSet Buf = Stream.Read(7) EndIPOff = AscB(MidB(Buf, 5, 1)) + (AscB(MidB(Buf, 6, 1))*256) + (AscB(MidB(Buf, 7, 1))*256*256) StartIP = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1))*256) + (AscB(MidB(Buf, 3, 1))*256*256) + (AscB(MidB(Buf, 4, 1))*256*256*256) GetStartIP = StartIP End Function ' ============================================ ' 获取结束IP位置 ' ============================================ Private Function GetEndIP() Stream.Position = EndIPOff Buf = Stream.Read(5) EndIP = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1))*256) + (AscB(MidB(Buf, 3, 1))*256*256) + (AscB(MidB(Buf, 4, 1))*256*256*256) CountryFlag = AscB(MidB(Buf, 5, 1)) GetEndIP = EndIP End Function ' ============================================ ' 获取地域信息,包含国家和和省市 ' ============================================ Private Sub GetCountry(IP) If (CountryFlag = 1 Or CountryFlag = 2) Then Country = GetFlagStr(EndIPOff + 4) If CountryFlag = 1 Then LocalStr = GetFlagStr(Stream.Position) ' 以下用来获取数据库版本信息 If IP >= IPToInt("255.255.255.0") And IP <= IPToInt("255.255.255.255") Then LocalStr = GetFlagStr(EndIPOff + 21) Country = GetFlagStr(EndIPOff + 12) End If Else LocalStr = GetFlagStr(EndIPOff + 8) End If Else Country = GetFlagStr(EndIPOff + 4) LocalStr = GetFlagStr(Stream.Position) End If ' 过滤数据库中的无用信息 Country = Trim(Country) LocalStr = Trim(LocalStr) If InStr(Country, "CZ88.NET") Then Country = "" If InStr(LocalStr, "CZ88.NET") Then LocalStr = "" End Sub ' ============================================ ' 获取IP地址标识符 ' ============================================ Private Function GetFlagStr(OffSet) Dim Flag Flag = 0 Do While (True) Stream.Position = OffSet Flag = AscB(Stream.Read(1)) If(Flag = 1 Or Flag = 2 ) Then Buf = Stream.Read(3) If (Flag = 2 ) Then CountryFlag = 2 EndIPOff = OffSet - 4 End If OffSet = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1))*256) + (AscB(MidB(Buf, 3, 1))*256*256) Else Exit Do End If Loop If (OffSet < 12 ) Then GetFlagStr = "" Else Stream.Position = OffSet GetFlagStr = GetStr() End If End Function ' ============================================ ' 获取字串信息 ' ============================================ Private Function GetStr() Dim c GetStr = "" Do While (True) c = AscB(Stream.Read(1)) If (c = 0) Then Exit Do '如果是双字节,就进行高字节在结合低字节合成一个字符 If c > 127 Then If Stream.EOS Then Exit Do GetStr = GetStr & Chr(AscW(ChrB(AscB(Stream.Read(1))) & ChrB(C))) Else GetStr = GetStr & Chr(c) End If Loop End Function ' ============================================ ' 核心函数,执行IP搜索 ' ============================================ Public Function QQWry(DotIP) Dim IP, nRet Dim RangB, RangE, RecNo IP = IPToInt (DotIP) Set Stream = CreateObject("ADodb.Stream") Stream.Mode = 3 Stream.Type = 1 Stream.Open Stream.LoadFromFile QQWryFile Stream.Position = 0 Buf = Stream.Read(8) FirstStartIP = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1))*256) + (AscB(MidB(Buf, 3, 1))*256*256) + (AscB(MidB(Buf, 4, 1))*256*256*256) LastStartIP = AscB(MidB(Buf, 5, 1)) + (AscB(MidB(Buf, 6, 1))*256) + (AscB(MidB(Buf, 7, 1))*256*256) + (AscB(MidB(Buf, 8, 1))*256*256*256) RecordCount = Int((LastStartIP - FirstStartIP)/7) ' 在数据库中找不到任何IP地址 If (RecordCount <= 1) Then Country = "未知" QQWry = 2 Exit Function End If RangB = 0 RangE = RecordCount Do While (RangB < (RangE - 1)) RecNo = Int((RangB + RangE)/2) Call GetStartIP (RecNo) If (IP = StartIP) Then RangB = RecNo Exit Do End If If (IP > StartIP) Then RangB = RecNo Else RangE = RecNo End If Loop Call GetStartIP(RangB) Call GetEndIP() If (StartIP <= IP) And ( EndIP >= IP) Then ' 没有找到 nRet = 0 Else ' 正常 nRet = 3 End If Call GetCountry(IP) QQWry = nRet End Function End Class Set Wry = New TQQWry Wry.QQWry("8.8.8.8") WScript.Echo Wry.Country & "/" & Wry.LocalStr
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- 如何将QQWry.Dat导出为access数据,导入mssql及查询纯真导入后的ip数据
- 用Python脚本查询纯真IP库
- C# 调用IP库(QQWry.Dat)查询IP位置及自动升级IP库方法(附IP库下载地址及相关dll下载)
- ip物理定位搜索类 与配合结合纯真数据库qqwry.dat完美结合,可以查询世界各地数据库,可以做网站按地区登录不同的地区网站
- C# 调用IP库(QQWry.Dat)查询IP位置及自动升级IP库方法(附IP库下载地址及相关dll下载)
- 用Python脚本查询纯真IP库
- 使用ruby解析纯真IP库(qqwry.dat)
- lib-qqwry v1.0 发布 nodejs解析纯真IP库(qqwry.dat)
- 使用ruby解析纯真IP库(qqwry.dat)
- C# 调用IP库(QQWry.Dat)查询IP位置及自动升级IP库方法(附IP库下载地址及相关dll下载)
- 关于IP纯真数据库(QQWry.dat)的使用 ----笔记
- C# 读取纯真IP数据库QQWry.dat获取地区信息
- qqwry - 纯真ip库的golang服务
- 使用共享内存查询纯真IP库(微秒级)
- C# 读取纯真IP数据库QQWry.dat获取地区信息
- 纯真IP数据库QQWry.dat格式详解
- java读取纯真IP数据库qqwry.dat的源代码
- PHP获取IP地址所在地信息的实例(使用纯真IP数据库qqwry.dat)
- QQwry.dat格式分析和查询IP位置的PHP程序
- 读取纯真IP数据库QQwry.dat的java版程序报数组越界异常问题