keyongtech


  keyongtech > scripting.vbscript > 06/2006

 #1  
06-27-06, 11:13 PM
OldDog
Hi,

I have been asked to scan 70 some subnets and return the names, ip and
OS of all servers found. I am only interested in Windows servers so
UNIX boxes, routers and switches can be ignored.


Normally, I use "IsConnectible(name, "", "")" to determine if a server
is live, however, I think that it needs the server name and all I have
is a potential IP address.


In any event, my attempt at this has failed and I am not sure why.


Any help would be appreciated.


Here is my non working code; (as usual, watch the wrap)


'*******************
'* Define Constants
'*******************
Const ForReading = 1
'********************
'* Declare variables
'********************
Dim results
'================================================= =========================

set wso = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("H:\Files\TXT\NOCSubNets.txt",
ForReading)
' OPEN Excel Worksheet Template and fill in the blanks
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Add
Set objsheet = objWorkbook.Worksheets(1)
k = 2
objSheet.Name = "IP SubNets"
' Format spreadsheet.
objSheet.Rows(1).Font.Bold = True
objSheet.Select
objSheet.Cells.EntireColumn.Columnwidth = 18
objSheet.Cells(1, 1).Value = "IP Address"
objSheet.Cells(1, 2).Value = "Machine Name"
objSheet.Cells(1, 3).Value = "Status"
objSheet.Cells(1, 4).Value = "Operating System"
objsheet.Cells.EntireColumn.AutoFit


Do Until objTextFile.AtEndOfStream
subnet = objTextFile.Readline
If (subnet <> "") Then


' ping the computer to see if it is online
For i = 229 to 255
ip = subnet & "." & i
objSheet.Cells(k, 1).Value = ip
wso.run "cmd /c nslookup " & ip & "
>scannet.txt",0,True

results =
Filter(Split(objFSO.OpenTextFile("scannet.txt").Re adAll,vbCrLf),"Name:")



If ubound(results) = 0 Then
name =
Replace(lcase(trim(mid(results(0),6))),".wellsfarg o.com","")
WScript.Echo Name
objSheet.Cells(k, 2).Value = name
Else
name = ""
End If


Select Case True


Case Left(name,3) = "prt"
name = "PRINTER - " & name
Case Left(name,2) = "ro"
name = "ROUTER - " & name
Case Mid(name,3,4) = "firl"
name = "FIREWALL" & " - " & name
End Select


If IsConnectible(name, "", "") Then
' error handling the connection to strComputer
'On Error Resume Next


Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\" & name & "\root\cimv2")


If Err.Number = 0 Then
On Error Goto 0


Set colItems =
objWMIService.ExecQuery("SELECT * FROM
Win32_OperatingSystem", "WQL", _
wbemFlagReturnImmediately +
wbemFlagForwardOnly)
For Each objItem In colItems
fOs = objItem.Caption
Next


Else
Status = Name & " is online but not available"
End If
Else
Status = Name & " is not online"
End If
objSheet.Cells(k, 3).Value = Status
objSheet.Cells(k, 4).Value = fOs
objsheet.Cells.EntireColumn.AutoFit
K = K + 1
Next
End If
Loop
objWorkbook.SaveAs("X:\Scripts\NocSubNets.xls")
objWorkbook.Close
objExcel.Quit
objTextFile.Close
WScript.Echo "Done Checking your Server List."


Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP


' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used


If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750


Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1


Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"


oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True


Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)


sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)


Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function
 #2  
06-28-06, 01:03 AM
Michael Harris \(MVP\)
OldDog wrote:
> Hi,
>
> I have been asked to scan 70 some subnets and return the names, ip and
> OS of all servers found. I am only interested in Windows servers so
> UNIX boxes, routers and switches can be ignored.
>> Normally, I use "IsConnectible(name, "", "")" to determine if a server

> is live, however, I think that it needs the server name and all I have
> is a potential IP address.


See Alex's comments right in the IsConnectible code...

Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP
...
[..]
 #3  
06-28-06, 09:37 PM
OldDog
You are correct sir. I got it to work by following directions;

'*******************
'* Define Constants
'*******************
Const ForReading = 1
'********************
'* Declare variables
'********************

'================================================= =========================
set wso = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("H:\Files\TXT\NOCSubNets.txt",
ForReading)
' OPEN Excel Worksheet Template and fill in the blanks
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Add
Set objsheet = objWorkbook.Worksheets(1)
k = 2
objSheet.Name = "IP SubNets"
' Format spreadsheet.
objSheet.Rows(1).Font.Bold = True
objSheet.Select
objSheet.Cells.EntireColumn.Columnwidth = 18
objSheet.Cells(1, 1).Value = "IP Address"
objSheet.Cells(1, 2).Value = "Machine Name"
objSheet.Cells(1, 3).Value = "Status"
objSheet.Cells(1, 4).Value = "Operating System"
objsheet.Cells.EntireColumn.AutoFit

Do Until objTextFile.AtEndOfStream
subnet = objTextFile.Readline
If (subnet <> "") Then

' ping the computer to see if it is online
For i = 1 to 255
ip = subnet & "." & i
objSheet.Cells(k, 1).Value = ip


If IsConnectible(ip, "", "") Then
' error handling the connection to strComputer
On Error Resume Next
sName = " "
Status = " "
fOs = " "
Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\" & ip & "\root\cimv2")

If Err.Number <> 0 Then
Status = " is not Windows"
sName = " "
fOs = " "
On Error Resume Next
Else
Status = "Windows"
'Get Server Name
Set colItems = objWMIService.ExecQuery("SELECT * FROM
Win32_ComputerSystem", "WQL", _
wbemFlagReturnImmediately +
wbemFlagForwardOnly)

For Each objItem In colItems
sName = objItem.Caption
Next
'Get Server OS
Set colItems = objWMIService.ExecQuery("SELECT * FROM
Win32_OperatingSystem", "WQL", _
wbemFlagReturnImmediately +
wbemFlagForwardOnly)
For Each objItem In colItems
fOs = objItem.Caption
Next
End If
Else
Status = " is not online"
sName = " "
fOs = " "
End If
objSheet.Cells(k, 2).Value = sName
objSheet.Cells(k, 3).Value = Status
objSheet.Cells(k, 4).Value = fOs
objsheet.Cells.EntireColumn.AutoFit
K = K + 1
Next

End If
Loop
objWorkbook.SaveAs("X:\Scripts\NocSubNets.xls")
objWorkbook.Close
objExcel.Quit
objTextFile.Close
WScript.Echo "Done Checking your Server List."


Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP


' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used


If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750


Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1


Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"


oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True


Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)


sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)


Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function

Michael Harris (MVP) wrote:
[..]
Similar Threads
Scan a Subnet and return the Names of servers found

Hi, I have been asked to scan 70 some subnets and return the names, ip and OS of all servers found. I am only interested in Windows servers so UNIX boxes, routers and...

Install SMS 2.0 agents on NT servers & Windows 2000 Servers

Their is any procedure to install SMS Agents (SMS 2.) on different Domains and servers which are member of workgrougs

2 sbs2003 servers on same subnet

This question is a little different than previous questions about two sbs servers on the same domain. I support two companies that share office space and a single Internet...

No network Connectivity between 2 servers (windows 2003 servers)

Hi there Situration :- Trying to join 1 server to domain but can't. Cannot ping between 2 servers (windows 2003 servers) Details 1.Router - 192.168.0.1

Connectivity between Windwos 2000 Citrix servers and Windows 2003 file servers

Hi, We've got 5 citrix terminal servers running. They run on W2k with Sp3 and all the updates installed. On top of that we've got Citrix MetaFrame XP FR3 running. The...


All times are GMT. The time now is 11:20 AM. | Privacy Policy