我不想让它查看文件中的 IP 列表,而是让它查看一个范围。范围需要与 C 类一样大。有人能帮我编辑下面的代码来实现这一点吗?我正在尝试搜索子网掩码为 255.255.0.0 的 10.0.0.0。
' NetworkFindInfo.vbs - Windows Logon Script.
' VBScript - Look up a computers info.
' Author Chris Collins
' Version 1 - July 2018
' ----------------------------------------------------------'
Option Explicit
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const PATH_TO_INPUT = "C:\Users\ccollins\Desktop\NetworkFindingInfo\ComputerList.txt"
Const PATH_TO_OUTPUT = "C:\Users\ccollins\Desktop\NetworkFindingInfo\ComputerInfo.csv"
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Dim shl
Set shl = WScript.CreateObject("WScript.Shell")
Dim input
Set input = fso.OpenTextFile(PATH_TO_INPUT)
Dim output
Set output = fso.CreateTextFile(PATH_TO_OUTPUT, True)
output.WriteLine "Hostname,Computer Name,Domain,Serial Number,Make,Model,BIOS Version,Operating System,CPU,Memory (MB),Disk Drives,MAC Address"
Dim wmiService
Dim wmiResults
Dim hostname
Dim computername
Dim domain
Dim make
Dim model
Dim biosversion
Dim operatingSystem
Dim serialNumber
Dim cpu
Dim memory
Dim drives
Dim mac
Dim line
Dim exec
Dim pingResults
While Not input.AtEndOfStream
line = input.ReadLine
hostname = ""
computername = ""
domain = ""
make = ""
model = ""
biosversion = ""
operatingSystem = ""
serialNumber = ""
cpu = ""
memory = ""
drives = ""
mac = ""
Set exec = shl.Exec("ping -n 2 -w 1000 " & line)
pingResults = LCase(exec.StdOut.ReadAll)
If InStr(pingResults, "reply from") Then
On Error Resume Next
Set wmiService = GetObject("winmgmts:\\" & line & "\root\CIMV2")
If Not Err.Number = 0 Then
output.WriteLine line & ",Error: " & Err.Description
On Error GoTo 0
Else
On Error GoTo 0
hostname = line
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
Dim item
For Each item In wmiResults
serialNumber = Trim(item.SerialNumber)
biosversion = Trim(item.SMBIOSBIOSVersion)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
make = Trim(item.Manufacturer)
model = Trim(item.Model)
computername = Trim(item.Name)
domain = Trim(item.Domain)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
operatingSystem = Trim(item.Name)
operatingSystem = Split(operatingSystem, "|")(0)
memory = Round(Trim(item.TotalVisibleMemorySize) / 1024, 2)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
cpu = Trim(item.Name)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter = 1", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
mac = Trim(item.MACAddress)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
drives = drives & Trim(item.DeviceID) & " " & Round(Trim(item.Size) / (1024^2), 2) & ";"
Next
output.WriteLine hostname & "," & computername & "," & domain & "," & serialNumber & "," & make & "," & model & "," & biosversion & "," & operatingSystem & "," & cpu & "," & memory & "," & drives & "," & mac
End If
Else
output.WriteLine line & ",No Response"
End If
Wend
output.Close
input.Close
Set wmiService = Nothing
Set wmiresults = Nothing
Msgbox("Done Collecting Data")
另外,我的网络上的某些 IP 会停止运行此代码。有没有办法让它继续运行?
答案1
这取决于您的范围需要有多大/多灵活。
最简单的形式
baseIP="10.10."
for a=0 to 255
for b=0 to 255
IP=baseIP&a&"."&b
<\Your code here>
next b
next a
稍后再详述
类似于子网搜索
main
sub main
Dim WshShell, oExec
Dim x
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.OpenTextFile("c:\scripts\computers.txt",1)
Do until objInputFile.AtEndofStream
userIP = objInputFile.ReadLine
numBits=Mid(userIP,instr(1,userIP,"/")+1,3)
numIP=CDbl(2^(32-mid(userIP,instr(1,userIP,"/")+1,3)))
ip=Mid(userIP,1,instr(1,userIP,"/")-1)
a=Split(ip,".")
'Darn vbscript has signed numbers so -2 billion to pos 2 billion
' and ip's go 0 to 4 billion
'lame work around
If a(0)>127 Then offset=128 Else offset=0
a(0)=a(0)-offset
ipnum=CDbl((a(0)*256^3)+(a(1)*256^2)+(a(2)*256)+a(3))
ipEnd=CDbl(ipnum+numIP)
For x=ipnum To ipEnd
grab=Hex(x)
grab2=Mid("00000000"&grab,Len(grab)+1,8)
a=CLng("&h"&Mid(grab2,1,2))+offset
b=CLng("&h"&Mid(grab2,3,2))
c=CLng("&h"&Mid(grab2,5,2))
d=CLng("&h"&Mid(grab2,7,2))
newip=a&"."&b&"."&c&"."&d
WScript.echo "newip:"&newip
inventory(newip)
Next
Loop
end sub
起始和结束 IP 地址
begIP="10.10.3.1"
endIP="10.10.10.1"
a=Split(begIP,".")
b=Split(endIP,".")
'Darn vbscript has signed numbers so -2 billion to pos 2 billion
' and ip's go 0 to 4 billion
'lame work around
If a(0)>127 Then offset=128 Else offset=0
a(0)=a(0)-offset
b(0)=b(0)-offset
ipnum=CDbl((a(0)*256^3)+(a(1)*256^2)+(a(2)*256)+a(3))
ipEnd=CDbl((b(0)*256^3)+(b(1)*256^2)+(b(2)*256)+b(3))
For x=ipnum To ipEnd
grab=Hex(x)
grab2=Mid("00000000"&grab,Len(grab)+1,8)
a=CLng("&h"&Mid(grab2,1,2))+offset
b=CLng("&h"&Mid(grab2,3,2))
c=CLng("&h"&Mid(grab2,5,2))
d=CLng("&h"&Mid(grab2,7,2))
newip=a&"."&b&"."&c&"."&d
WScript.echo "newip:"&newip
inventory(newip)
Next
end sub