我试图检查是否可以访问其他PC的IPaddress。如果IPaddress是静态的,那么下面的代码就可以了。
vbscript代码
Dim Shell, strCommand, strHost, ReturnCode
strHost = "192.168.10.1"
Set Shell = wscript.createObject("wscript.shell")
strCommand = "ping -n 1 -w 300 " & strHost
ReturnCode = Shell.Run(strCommand, 0, True)
If ReturnCode = 0 Then
wscript.echo strHost & " is pingable"
Else
wscript.echo strHost & " is not pingable"
End If由于我想检查动态IPaddress,请使用ini文件。
ini文件
[IPaddress]
IP001 = "192.168.10.1";
IP002 = "192.168.10.2"; 现在,我想知道如何连接ini文件和vbscript代码。请解释给我听。
发布于 2011-09-14 08:25:29
正如影子向导在他的评论中提到的那样,您需要使用FSO (FileSystemObject)来读取文件。OpenTextFile的示例代码可能是一个很好的起点。
如果您将当前的vbscript代码移动到接受一个ip地址作为参数的Sub中,您只需为文件中的每个ip地址调用该子地址即可。
可能是这样的(完全未经测试的代码):
Const ForReading = 1
Set MyFile = fso.OpenTextFile(FileName, ForReading)
Do While MyFile.AtEndOfStream <> True
line = MyFile.ReadLine
If Left(line, 2) = "IP" Then
ipAddress = Replace(Replace(Right(line, 8), ";", ""), """", "")
YourSubThatPings ipAddress
End If
Loop
MyFile.Close但是,从ipAddress中提取line的代码可能需要重写,以便更灵活一些。如果ini文件包含大量其他数据,则可能需要添加一些代码以跳过正确的部分,等等。
https://stackoverflow.com/questions/7412439
复制相似问题