裙子太短:VBScript 编写类似 ipconfig 的工具

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 13:10:09
在Windows下,WSH的功能是非常强大的。WMI 则提供了非常丰富的接口,可以与系统进行交互,获取系统相关信息,并能对系统中的设备和对象进行有效管理。 Windows中的ipconfig 工具提供了与系统网络设备的接口,WMI 中同样有相同的接口,因此可以利用 VBScript 编写脚本来实现类似 ipconfig 的功能。利用 WMI 接口可以实现的网络管理功能非常之多,此处仅以显示IP地址、释放和获取DHCP地址为例做一说明:
1、显示IP地址 通过 Win32_NetworkAdapterConfiguration 获取网络配置信息,判断网络适配器是否支持IP协议。然后利用Win32_NetworkAdapter 对象或取网卡信息,Win32_NetworkAdapter有很多属性,重要的包括NetConnectionStatus、NetConnectionID、MACAddress、IPAddress等。所有ipconfig显示出的信息都能够获取得到,那么需要做的工作就是按照 ipconfig 显示格式来排列一下这些信息,使其看起来好看些。
2、释放和获取DHCP地址 调用Win32_NetworkAdapterConfiguration对象的ReleaseDHCPLease()和RenewDHCPLease()方法来实现。
3、添加上帮助说明、整理一下脚本的结构,于是就有了:  代码'******************************************************************************
' 程序名: ipconfig.vbs
' 功能: 类似ipconfig的 WSH 脚本程序, 提供了 ipconfig 的一部分功能,可以用来显示
' IP地址信息, 释放和获取DHCP地址
' 作者: li.zhongnan@hotmail.com (http://hi.baidu.com/li_zhongnan)
' 参考: http://www.reskit.net/monad/net1.htm
'****************************************************************************** On Error Resume Next isShowHelp = False
isShowIpConfig = True
isRelease = False
isRenew = False isShowAll = False '处理命令行参数,如果带参数 /all 则显示较为详细的信息
Set objArgs = WScript.Arguments
If objArgs.Count > 0 Then
If objArgs(0) = "/all" Then
isShowAll = True
End If
If objArgs(0) = "/?" Then
isShowHelp = True
isShowIpConfig = False
End If
If objArgs(0) = "/release" Then
isRelease = True
isShowIpConfig = False
End If
If objArgs(0) = "/renew" Then
isRenew = True
isShowIpConfig = False
End If
End If If isShowHelp Then
ShowHelp()
End If
If isShowIpConfig Then
ShowIpConfig(isShowAll)
End If
If isRelease Then
ReleaseDHCP()
ShowIpConfig(False)
End If
If isRenew Then
RenewDHCP()
ShowIpConfig(False)
End If
'******************************************************************************
' 函数: ShowHelp
' 显示帮助信息
'******************************************************************************
Function ShowHelp
WScript.Echo
WScript.Echo "USAGE:"
WScript.Echo " ipconfig [/? /all /release]"
WScript.Echo "where"
WScript.Echo " Options:"
WScript.Echo " /? Display this help message"
WScript.Echo " /all Display full configuration information"
WScript.Echo " /release Release the IP address for the specified adapter"
WScript.Echo " /renew Renew the IP address for the specified adapter"
WScript.Echo
WScript.Echo "The default is to display only the IP address, subnet mask and"
WScript.Echo "default gateway for each adapter bound to TCP/IP"
End Function '******************************************************************************
' 函数: ShowHelp
' 显示帮助信息
'******************************************************************************
Function ShowHelp
WScript.Echo
WScript.Echo "USAGE:"
WScript.Echo " ipconfig [/? /all /release]"
WScript.Echo
WScript.Echo "where"
WScript.Echo
WScript.Echo " Options:"
WScript.Echo " /? Display this help message"
WScript.Echo " /all Display full configuration information"
WScript.Echo " /release Release the IP address for the specified adapter"
WScript.Echo
WScript.Echo "The default is to display only the IP address, subnet mask and"
WScript.Echo "default gateway for each adapter bound to TCP/IP."
End Function '******************************************************************************
' 函数: ShowIpConfig(showAll)
' 显示网络配置信息
'******************************************************************************
Function ShowIpConfig(showAll)
'准备获取全局信息
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strKeyPath1 = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
strKeyPath2 = "SYSTEM\CurrentControlSet\Services\NetBT\Parameters"
strHostEntry = "Hostname"
strDomainEntry = "Domain"
strNodeEntry = "DhcpNodeType"
strRoutingEntry = "IPEnableRouter" '通过注册表获取主机名称、域名、节点类型、是否支持IP路由、是否支持WINS代理、DNS搜索前缀等信息
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath1,strHostEntry,strHostname
objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath1,strDomainEntry,strDomain
objReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath2,strNodeEntry,dwNodeType
objReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath1,strRoutingEntry,dwIPRouting Select Case dwNodeType
Case 4 strNodeType = "Mixed"
Case 8 strNodeType = "Hybrid"
Case Else strNodeType = "Unknown"
End Select
If dwIPRouting = 0 Then
strIPRouting = "No"
ElseIf dwIPRouting = 1 Then
strIPRouting = "Yes"
Else
strIPRouting = "?"
End If
'通过WMI Win32_NetworkAdapterConfiguration对象获取支持IP功能的网络适配器配置信息
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colFirstNicConfig = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objFirstNicConfig In colFirstNicConfig
strDnsWins = objFirstNicConfig.DNSEnabledForWINSResolution
Next
If strDnsWins = False Then
strWinsProxy = "No"
ElseIf strDnsWins = True Then
strWinsProxy = "Yes"
Else
strWinsProxy = "?"
End If '显示全局信息
WScript.Echo VbCrLf & "Windows IP Configuration" & VbCrLf
If showAll Then
WScript.Echo " Host Name . . . . . . . . . . . . : " & strHostname
WScript.Echo " Primary DNS Suffix . . . . . . . : " & strDomain
WScript.Echo " Node Type . . . . . . . . . . . . : " & strNodeType
WScript.Echo " IP Routing Enabled. . . . . . . . : " & strIPRouting
WScript.Echo " WINS Proxy Enabled. . . . . . . . : " & strWinsProxy
WScript.Echo " DNS Suffix Search List. . . . . . : " & strDomain
End If Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
sngOsVer = GetOsVer '先是每个网卡的网络配置信息
For Each objNicConfig In colNicConfigs
intIndex = objNicConfig.Index
Set objNic = objWMIService.Get("Win32_NetworkAdapter.DeviceID=" & intIndex) strAdapterType = objNic.AdapterType
If IsEmpty(strAdapterType) Or IsNull(strAdapterType) Or _
(strAdapterType = "") Then
strAdapterType = "Network"
End If If sngOsVer > 5 Then
strNetConn = objNic.NetConnectionID
Else
strNetConn = intIndex
End If WScript.Echo VbCrLf & strAdapterType & " adapter " & strNetConn
WScript.Echo
If objNic.NetConnectionStatus <> 2 Then 'not connected
Select Case objNic.NetConnectionStatus
Case 0 strConnStatus = "Disconnected"
Case 1 strConnStatus = "Connecting"
Case 3 strConnStatus = "Disconnecting"
Case 4 strConnStatus = "Hardware not present"
Case 5 strConnStatus = "Hardware disabled"
Case 6 strConnStatus = "Hardware malfunction"
Case 7 strConnStatus = "Media disconnected"
Case 8 strConnStatus = "Authenticating"
Case 9 strConnStatus = "Authentication succeeded"
Case 10 strConnStatus = "Authentication failed"
Case 11 strConnStatus = "Invalid address"
Case 12 strConnStatus = "Connecting"
Case Else strConnStatus = "Credentials required"
End Select
' 如果 NetConnectionStatus 的值为 7, 说明网线被拔掉了, 因此通过循环检测 Win32_NetworkAdapter 对象的 NetConnectionStatus 属性, 就可以判断网线是否被拔掉
WScript.Echo " Connection Status . . . . . . . . : " & _
strConnStatus
If showAllInfo Then
WScript.Echo " Description . . . . . . . . . . . : " & _
objNicConfig.Description
WScript.Echo " Physical Address. . . . . . . . . : " & _
objNicConfig.MACAddress
End If
Else
WScript.Echo " Connection-specific DNS Suffix . : " & _
objNicConfig.DNSDomain
If showAll Then
WScript.Echo " Description . . . . . . . . . . . : " & _
objNicConfig.Description
WScript.Echo " Physical Address. . . . . . . . . : " & _
objNicConfig.MACAddress
WScript.Echo " DHCP Enabled. . . . . . . . . . . : " & _
objNicConfig.DHCPEnabled End If strIPAddresses = ""
If Not IsNull(objNicConfig.IPAddress) Then
For Each strIPAddress In objNicConfig.IPAddress
strIPAddresses = strIPAddresses & strIPAddress & " "
Next
End If
WScript.Echo " IP Address. . . . . . . . . . . . : " & strIPAddresses
strIPSubnets = ""
If Not IsNull(objNicConfig.IPSubnet) Then
For Each strIPSubnet In objNicConfig.IPSubnet
strIPSubnets = strIPSubnets & strIPSubnet & " "
Next
End If
WScript.Echo " Subnet Mask . . . . . . . . . . . : " & strIPSubnets
strDefaultIPGateways = ""
If Not IsNull(objNicConfig.DefaultIPGateway) Then
For Each strDefaultIPGateway In objNicConfig.DefaultIPGateway
strDefaultIPGateways = strDefaultIPGateways & strDefaultIPGateway & " "
Next
End If
WScript.Echo " Default Gateway . . . . . . . . . : " & _
strDefaultIPGateways
If showAll Then
WScript.Echo " DHCP Server . . . . . . . . . . . : " & _
objNicConfig.DHCPServer
strDNSServerSearchOrder = ""
If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
For Each strDNSServer In objNicConfig.DNSServerSearchOrder
strDNSServerSearchOrder = strDNSServerSearchOrder & VbCrLf & _
" " & strDNSServer
Next
End If
WScript.Echo " DNS Servers . . . . . . . . . . . :" & _
strDNSServerSearchOrder
If Not IsNull(objNicConfig.WINSPrimaryServer) Then
WScript.Echo " Primary WINS Server . . . . . . . : " & _
objNicConfig.WINSPrimaryServer
End If
If Not IsNull(objNicConfig.WINSSecondaryServer) Then
WScript.Echo " Secondary WINS Server . . . . . . : " & _
objNicConfig.WINSSecondaryServer
End If
If objNicConfig.DHCPEnabled Then
dtmRawLeaseObtainedDate = objNicConfig.DHCPLeaseObtained
strFormattedLeaseObtainedDate = WMIDateToString(dtmRawLeaseObtainedDate)
WScript.Echo " Lease Obtained. . . . . . . . . . : " & _
strFormattedLeaseObtainedDate
dtmRawLeaseExpiresDate = objNicConfig.DHCPLeaseExpires
strFormattedLeaseExpiresDate = WMIDateToString(dtmRawLeaseExpiresDate)
WScript.Echo " Lease Expires . . . . . . . . . . : " & _
strFormattedLeaseExpiresDate
End If
End If
End If
Next
End Function
'******************************************************************************
' 函数: RenewDHCP
' 获取DHCP地址
'******************************************************************************
Function RenewDHCP
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration " _
& "Where IPEnabled = True")
For Each objNicConfig in colNicConfigs
objNicConfig.RenewDHCPLease()
Next
End Function '******************************************************************************
' 函数: ReleaseDHCP
' 释放DHCP地址
'******************************************************************************
Function ReleaseDHCP
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration " _
& "Where IPEnabled = True")
For Each objNicConfig in colNicConfigs
objNicConfig.ReleaseDHCPLease()
Next
End Function '******************************************************************************
' 函数: WMIDateStringToDate(dtmDate)
' 将 WMI 日期转换成字符串
'******************************************************************************
Function WMIDateToString(dtmDate)
WMIDateToString = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & _
Left(dtmDate, 4) & " " & _
Mid(dtmDate, 9, 2) & ":" & _
Mid(dtmDate, 11, 2) & ":" & _
Mid(dtmDate, 13, 2))
End Function '******************************************************************************
' 函数: GetOsVer
' 获取操作系统版本号
'******************************************************************************
Function GetOsVer
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem In colOperatingSystems
GetOSVer = CSng(Left(objOperatingSystem.Version, 3))
Next
End Function
文章来自: ASP技术网站(www.aspjs.net) 详文参考:http://www.aspjs.net/aspjs1html/ASPjiaocheng/vbscriptjiaobenjiaocheng/40.html