Solved

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

Posted on 2006-12-01
7
303 Views
Last Modified: 2012-05-05
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
Comment
Question by:TRAININGBSC
  • 3
  • 2
  • 2
7 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 25 total points
ID: 18053279
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
 

Author Comment

by:TRAININGBSC
ID: 18053520
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
 
LVL 3

Assisted Solution

by:JRockSolid
JRockSolid earned 25 total points
ID: 18054015
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:TRAININGBSC
ID: 18054411
Runs ok.

How about outputting it?
0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18057335
You should have excell... And it should open excell and place it in a spreadsheet for you
Did it not?
0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18061288
Dang, looking back..... I would take that 8 liner from sirbounty
                                                   
That is some mean batching sir :)  
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18061567
Thanx - but psexec is disabled in their environment (odd, cause I couldn't live without PSTools! :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now