BAT or VBS script needed to parse 30 servers for default gateway info


Anyone got a bat or vbs script that parses through a list of servers and reports on the ip they have as the default gateway?

Who is Participating?
Try this - you'll need psexec from - store it in your path (\windows\system32) for access from anywhere:

::begin code copy
@echo off
for /f %%c in (c:\computerList.txt) do call :process %%a
start c:\results.txt
goto :eof

set PC=%1
for /f "tokens=12 delims=: " %%a in ('psexec \\%PC% ipconfig^|find /i "Gateway"') do echo %PC%'s Gateway is %%a >> c:\results.txt
::end code copy
TRAININGBSCAuthor Commented:

Psexec has been disabled at our place. No doubt it would have worked judging by the code and from what I remember of its functionality...
Any other toold that'd pull this off?

I am at work so if there are errors in this i am sorry but this will get us started:
Const objPClist = "C:\PCs.txt"
Public strComputer(),strRespondPC(),GateWay()
Set objController = WScript.CreateObject("WshController")
Set objFSO = CreateObject("Scripting.FileSystemObject")
      If Not objFSO.FileExists(objPClist) Then
            MsgBox "Please edit the constant variable 'objPClist'.  The current file does not exist."
      End If
      Call Main
Sub Main()
MsgBox "1"
      Call GetPCs  ' Pull PC IPs/Hostnames from objPClist
      If IsDementioned(strComputer) = True Then
            MsgBox "2"
            Call TCPtest
      End If
      If IsDementioned(strRespondPC) = True Then
            MsgBox "3"
            Call GetGWs()
      End If
      If IsDementioned(GateWay) = True Then
            MsgBox "4"
            Call DisplayOutput()
      End If
End Sub

Sub GetPCs()
'On Error Resume Next
Set objTextFile = objFSO.OpenTextFile(objPClist, 1) '1 ip or hostname per line
      Do Until objTextFile.AtEndOfStream
            ReDim Preserve strComputer(x)
            strComputer(x) = Trim(objTextFile.readline)
            x = x + 1
End Sub      

Sub TCPtest()
      For z = 0 to Ubound(strComputer)
            Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
            Set colItems = objWMIService.ExecQuery _
        ("Select * from Win32_PingStatus " & _
         "Where Address = '" & strComputer(z) & "'")
            For Each objItem in colItems
          If objItem.StatusCode = 0 Then
            ReDim Preserve strRespondPC(y)
                        strRespondPC(y)= strComputer(z)
                        y = y + 1
            ReDim Preserve strNoRespondPC(w)
                        strNoRespondPC(w)= strComputer(z)
                        w = w + 1      
          End If
End Sub

Sub GetGWs()
For x = 0 To UBound(strRespondPC)
      Set objWMIService = GetObject("winmgmts:\\" & strRespondPC(x) & "\root\CIMV2")
      Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
      For Each objItem in colItems
    If Not isNull(objItem.DefaultIPGateway) Then
          ReDim Preserve GateWay(q)
      GateWay(q) = objItem.DefaultIPGateway
    End If
  q = q + 1
End Sub

Sub DisplayOutput()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Add
Set objWorksheet = objWorkbook.Worksheets(1)
objExcel.Visible = True

For a = 0 To UBound(strRespondPC)
On Error Resume Next
StrLength = Len(strRespondPC(a))
        objWorksheet.Cells(R, 1) = strRespondPC(a)
        R = R + 1             
        If objWorkSheet.Columns(1).ColumnWidth < StrLength Then
              objWorkSheet.Columns(1).ColumnWidth = StrLength + 4
        End If
For a = 0 To UBound(GateWay)    
    StrLength = Len(GateWay(a))
    objWorksheet.Cells(s, 2) = GateWay(a)
        s = s + 1             
        If objWorkSheet.Columns(2).ColumnWidth < StrLength Then
              objWorkSheet.Columns(2).ColumnWidth = StrLength + 4
        End If

objexcel = Nothing
objWorkbook = Nothing
objWorksheet = Nothing

End Sub

Function IsDementioned(ByVal arr)
  IsDementioned = False
  If IsArray(arr) Then
        On Error Resume Next
         UBound arr
        If Err.Number = 0 Then IsDementioned = True
      End If
End Function

I would like to consider this a work in progress...So let me know what problems you run into
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

TRAININGBSCAuthor Commented:
Runs ok.

How about outputting it?
You should have excell... And it should open excell and place it in a spreadsheet for you
Did it not?
Dang, looking back..... I would take that 8 liner from sirbounty
That is some mean batching sir :)  
Thanx - but psexec is disabled in their environment (odd, cause I couldn't live without PSTools! :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.