Solved

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

Posted on 2006-12-01
7
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Make bootable USB with kon-boot 6 1,027
Windows Server 2016 Failover Cluster Question 3 85
Unknown Windows 7 64-bit Screen Appearance 5 75
ZEPTO Ransomware - Removal 8 93
Many people tend to confuse the function of a virus with the one of adware, this misunderstanding of the basic of what each software is and how it operates causes users and organizations to take the wrong security measures that would protect them ag…
#Citrix #POC #XenDesktop #vCenter #VMware #ESX
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

751 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