楼主 罗刚君 |
Q:如何获取IP等信息 A:方法较多,不过XP与WIN7有所不同
- Sub 获取IP1() 'WIN7 专用
- On Error GoTo err
- Dim aa, bb As String, cc As String
- Set wExec = CreateObject("WScript.Shell").Exec("%ComSpec% /c ipconfig") '自己改目录
- Do While wExec.Status = 0
- DoEvents
- Loop
- result = wExec.StdOut.ReadAll
- aa = Split(result, Chr(10))
- For i = 0 To UBound(aa) - 1
- cc = LTrim(aa(i))
- If Left(cc, 2) = "IP" Or Left(cc, 2) = "子网" _
- Or Left(cc, 2) = "默认" Then bb = bb & Chr(10) & cc
- Next
- MsgBox bb
- err:
- End Sub
- Sub 获取IP2() 'XP专用
- On Error GoTo err
- Dim aa, bb As String, cc As String
- Set wExec = CreateObject("WScript.Shell").Exec("%ComSpec% /c ipconfig") '自己改目录
- Do While wExec.Status = 0
- DoEvents
- Loop
- result = wExec.StdOut.ReadAll
- aa = Split(result, Chr(10))
- For i = 0 To UBound(aa) - 1
- cc = LTrim(aa(i))
- If Left(cc, 2) = "IP" Or Left(cc, 2) = "Su" _
- Or Left(cc, 2) = "De" Then bb = bb & Chr(10) & cc
- Next
- MsgBox bb
- err:
- End Sub
- Sub 获取IP3() 'WIN 7和XP通用
- On Error GoTo err
- Dim aa, bb As String, cc As String
- Set wExec = CreateObject("WScript.Shell").Exec("%ComSpec% /c ipconfig") '自己改目录
- Do While wExec.Status = 0
- DoEvents
- Loop
- result = wExec.StdOut.ReadAll
- aa = Split(result, Chr(10))
- For i = 0 To UBound(aa) - 1
- cc = LTrim(aa(i))
- If InStr(Application.OperatingSystem, "6") Then
- If Left(cc, 2) = "IP" Or Left(cc, 2) = "子网" _
- Or Left(cc, 2) = "默认" Then bb = bb & Chr(10) & cc
- Else
- If Left(cc, 2) = "IP" Or Left(cc, 2) = "Su" _
- Or Left(cc, 2) = "De" Then bb = bb & Chr(10) & cc
- End If
- Next
- MsgBox bb
- err:
- End Sub
- Sub 获取IP4() '利用正则表达式:WIN 7和XP通用
- On Error GoTo err
- Dim result As String, regEx, str(1 To 3)
- Set wExec = CreateObject("WScript.Shell").Exec("%ComSpec% /c ipconfig") '自己改目录
- Do While wExec.Status = 0
- DoEvents
- Loop
- result = wExec.StdOut.ReadAll
- Set regEx = CreateObject("VBSCRIPT.REGEXP") '建立正则表达式对象
- regEx.Global = True '搜索多个对象
- regEx.Pattern = "\d{1,3}\.+\d{1,3}\.+\d{1,3}\.+\d{1,3}" '按IP地址分布规律取值
- Set colMatches = regEx.Execute(result)
- MsgBox "IP地址:" & vbTab & colMatches(0) & Chr(10) & "子网掩码:" & vbTab & colMatches(1) & Chr(10) & "默认网关:" & vbTab & colMatches(2)
- Set regEx = Nothing
- err:
- End Sub
- Sub GetIP_SubNet_Gatway() '利用WMI获取:WIN 7和XP通用
- Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
- Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
- For Each objNetAdapter In colNetAdapters
- If TypeName(objNetAdapter.IPAddress) <> "Null" Then
- MsgBox "您的IP:........:" & objNetAdapter.DefaultIPGateway(0) & Chr(13) _
- & "子网掩码:........:" & objNetAdapter.IPAddress(0) & Chr(13) _
- & "默认网关:........:" & objNetAdapter.IPSubnet(0)
- Exit Sub
- End If
- Next
- End Sub
|