Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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

Hi,

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?

Thanks.
0
TRAININGBSC
Asked:
TRAININGBSC
  • 3
  • 2
  • 2
2 Solutions
 
sirbountyCommented:
Try this - you'll need psexec from http://www.microsoft.com/technet/sysinternals/utilities/psexec.mspx - 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

:process
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
0
 
TRAININGBSCAuthor Commented:
Hi,

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?

0
 
JRockSolidCommented:
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()
x=0:q=0:y=0:w=0:q=0:r=0:s=0
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."
            wscript.quit
      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
      Loop
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
      Else
            ReDim Preserve strNoRespondPC(w)
                        strNoRespondPC(w)= strComputer(z)
                        w = w + 1      
          End If
            Next
      Next
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
  Next
  q = q + 1
Next
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
Next
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
Next

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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TRAININGBSCAuthor Commented:
Runs ok.

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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now