Script Center > Scripting Forums > The Official Scripting Guys Forum! > vbscript ipconfig that will do an ipconfig every 10 to 30 sends repeatedly until I stop the script.
Ask a questionAsk a question
 

Answervbscript ipconfig that will do an ipconfig every 10 to 30 sends repeatedly until I stop the script.

  • Wednesday, December 17, 2008 3:31 PMScottG14 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    I edited a release renew script to make it do just ipconfig.  I would like it to do ipconfig every 10-20 seconds continuously until I stop the script or until it gets the ip address I am looking for.  This script justs does ipconfig but opens a new window everythime.  I don't want it to open a new window everytime unless it closes the window and reopens it.  That is an option.  
    Thanks
    Scott

     intTime = 5000
    Do
    Set objShell = Wscript.CreateObject("Wscript.Shell")
    objShell.run("%comspec% /k ipconfig")
    Wscript.Sleep intTime
    Loop
    Skier

Answers

  • Friday, December 19, 2008 3:44 PMJRussell97 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try this script.  It uses WMI to get the address info and display it on the screen.  It will ask you if you want to quit and if not search tha address info again.  You will want to run it using cscript so the info displays in the window and not in seperate boxes.

    Example: cscript ThisScript.vbs


    On Error Resume Next

    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly = &h20

    ' Put your computer name here
    arrComputers = Array("NRGRITD14")
    Quit = "N"
    Do While Quit= "N"
    For Each strComputer In arrComputers
       WScript.Echo
       WScript.Echo "=========================================="
       WScript.Echo "Computer: " & strComputer
       WScript.Echo "=========================================="

       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
       Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", _
                                              wbemFlagReturnImmediately + wbemFlagForwardOnly)

       For Each objItem In colItems
         
          strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
            WScript.Echo "DefaultIPGateway: " & strDefaultIPGateway
                    
          strIPAddress = Join(objItem.IPAddress, ",")
             WScript.Echo "IPAddress: " & strIPAddress
         
          strIPSubnet = Join(objItem.IPSubnet, ",")
             WScript.Echo "IPSubnet: " & strIPSubnet
        
          WScript.Echo
       Next
    Next
    Answer = Inputbox ("Terminate the script Y/N?", "IP Address Check")
    Quit = ucase(Answer)
    Loop



    You will still need to script the ping but you could put in at the bottom of this script.

    Hope this helps,
    Jrussell97

     



  • Monday, December 29, 2008 10:57 PMMatthewBeattie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hey...you could also use WMI to enumerate your systems IP Address and send a ping to the remote systems. I've also added a timeout so it won't run indefinately if you never recieve the ipaddress you expect. Try modifying the variables in this script to meet your requirements...just add the IP Address of the systems you want to ping to the array.

    Option Explicit  
    'On Error Resume Next  
       ProcessScript  
       If Err.Number <> 0 Then 
          Wscript.Quit  
       End If 
    On Error Goto 0  
    '----------------------------------------------------------------------------------------------------------------------------  
    Function ProcessScript  
       Dim correctIPAddress, ipAddresses, ipAddress  
       Dim timeOut, waited  
       ipAddresses      = Array("192.168.1.1","192.168.1.2")  
       correctIPAddress = "192.168.1.2" 
       timeOut = 10  
       waited  = 0  
       Do While EnumerateIPAddress <> correctIPAddress Or waited >= timeOut   
          Wscript.Sleep 1000  
          waited = waited + 1  
          If waited >= timeOut Then 
             MsgBox "The script did not respond within " & waited & " seconds." 
             Exit Function 
          End If 
       Loop 
       For Each ipAddress In ipAddresses  
          If Not CheckConnection(ipAddress) Then 
             MsgBox ipAddress & " did not reply to a ping." 
          Else 
             MsgBox ipAddress & " replied to a ping." 
          End If 
       Next 
    End Function 
    '----------------------------------------------------------------------------------------------------------------------------  
    'Name       : EnumerateIPAddress -> Enumerates the IP Address of the local system via WMI.
    'Parameters : none               -> 
    'Return     : EnumerateIPAddress -> Returns the IP Address of the local system.  
    '----------------------------------------------------------------------------------------------------------------------------  
    Function EnumerateIPAddress  
       Dim objConfig, ipAddress  
       ipAddress = "" 
       For Each objConfig in Getobject("winmgmts:").ExecQuery("select * from Win32_NetworkAdapterConfiguration where IPEnabled = True")  
          ipAddress = objConfig.IPAddress(0)  
          If ipAddress <> "" And ipAddress <> "0.0.0.0" Then 
             Exit For 
          End If 
       Next 
       EnumerateIPAddress = ipAddress  
    End Function 
    '----------------------------------------------------------------------------------------------------------------------------  
    'Name       : CheckConnection -> Checks network connectivity of a remote host using WMI ping.  
    'Parameters : hostName        -> Hostname or IP Address of computer system to verify network connectivity with.  
    'Return     : Boolean         -> True if hostname replies. False otherwise.  
    '----------------------------------------------------------------------------------------------------------------------------  
    Function CheckConnection(hostName)  
       Dim ping, response, replied  
       Set ping = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _  
                           ("select * from Win32_PingStatus where address = '" & hostName & "'")  
       For each response in ping  
          replied = Not IsNull(response.StatusCode) And response.StatusCode = 0  
       Next 
       CheckConnection = replied  
    End Function 
    '---------------------------------------------------------------------------------------------------------------------------- 

    Hope this helps

    Cheers

    Matt :)

All Replies

  • Wednesday, December 17, 2008 4:09 PMJRussell97 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    If you change the /k to a /r it will close the window and open a new one next time.  The problem is that it closes immediately and you can not see the info.

    If you change the line like this

    objShell.run("%comspec% /r ipconfig >> iptest.txt")

    it will output the display to a file that you can view at completion of the script.

    This will however show the same information over and over again.  I am not sure what you are trying to find.  The IP configuration will not change unless you do somthing to make it change.

    Can you explain more about what you are looking for?
  • Thursday, December 18, 2008 4:46 PMScottG14 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
     I have DHCP set up on my Laptop and when I am logging in and out with different users they are receiving different policies which requires the user to get a different Ip Address.  So I am looking to be able to start the script and be able to do an ipconfig. it takes a minute or so for it to get the DHCP address so I would like the script to do an ipconfig every 10 seconds or so until the user gets the correct ip address.   Then I am looking to put another script together that will ping two or three ipaddresses to verify the policy on my switch is working correctly and able to ping the correct default gateways  or not ping the other gateways.
    I believe I would need to do this seperately but If I could combine them that would be great also.
    Thanks
    Scott
    Skier
  • Friday, December 19, 2008 3:44 PMJRussell97 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try this script.  It uses WMI to get the address info and display it on the screen.  It will ask you if you want to quit and if not search tha address info again.  You will want to run it using cscript so the info displays in the window and not in seperate boxes.

    Example: cscript ThisScript.vbs


    On Error Resume Next

    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly = &h20

    ' Put your computer name here
    arrComputers = Array("NRGRITD14")
    Quit = "N"
    Do While Quit= "N"
    For Each strComputer In arrComputers
       WScript.Echo
       WScript.Echo "=========================================="
       WScript.Echo "Computer: " & strComputer
       WScript.Echo "=========================================="

       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
       Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", _
                                              wbemFlagReturnImmediately + wbemFlagForwardOnly)

       For Each objItem In colItems
         
          strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
            WScript.Echo "DefaultIPGateway: " & strDefaultIPGateway
                    
          strIPAddress = Join(objItem.IPAddress, ",")
             WScript.Echo "IPAddress: " & strIPAddress
         
          strIPSubnet = Join(objItem.IPSubnet, ",")
             WScript.Echo "IPSubnet: " & strIPSubnet
        
          WScript.Echo
       Next
    Next
    Answer = Inputbox ("Terminate the script Y/N?", "IP Address Check")
    Quit = ucase(Answer)
    Loop



    You will still need to script the ping but you could put in at the bottom of this script.

    Hope this helps,
    Jrussell97

     



  • Monday, December 29, 2008 3:44 PMScottG14 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks for the script info.
    I ran this with CSCRIPT but it ran too fast without any delay not giving the policy a chance to be applied to the switch.  Once the policy gets appled the PC will recieve a new IP address.  I will paly around with it and see if I can slow it down inbetween retries.

    Thanks

    Scott


    Skier
  • Monday, December 29, 2008 7:29 PMJRussell97 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Scott,
    The data is just from one iteration.  WMI returns a collection of data and then loops through the results.  It displays as if there are different adapters in the PC.  If you do not see the address you want type N in the response and it will query and display the info again.  When you see the address change type Y in the response and the script will terminate.

  • Monday, December 29, 2008 8:38 PMScottG14 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    JRussell97,
    Hi I found this script and modified it so it would run every 10-20 seconds without opening a new window.  All I need to do is to add a ping script that will ping three addresses.  I found a ping script but I can only get it to ping one address.  Then all I need to do is get it to start on startup.  Here is the ping script I found. 

    Thanks for your assistance
    Scott

    do
    Set objShell = CreateObject("WScript.Shell")
    Set objWshScriptExec = objShell.Exec("ipconfig /all")
    Set objStdOut = objWshScriptExec.StdOut

    '
    ' Skip first three lines
    '
    strLine = objStdOut.ReadLine
    WScript.Echo strLine
    strLine = objStdOut.ReadLine
    WScript.Echo strLine
    strLine = objStdOut.ReadLine
    WScript.Echo strLine


    '
    ' Add date/time information
    '

    strCurrentTime = "        Current Date/Time. . . . . . . . .: " & Now()
    WScript.Echo strCurrentTime

    '
    ' Display the rest of the output
    '

    While Not objStdOut.AtEndOfStream
       strLine = objStdOut.ReadLine
       WScript.Echo strLine
    Wend
    wscript.sleep 10000
    Loop



    Set objShell = CreateObject("WScript.Shell")

    Set objWshScriptExec = objShell.Exec("ping 10.1.180.1")
    Set objWshScriptExec = objShell.Exec("ping 192.168.1.1")
    Set objWshScriptExec = objShell.Exec("ping 190.169.20.1")
    Set objWshScriptExec = objShell.Exec("ping 10.1.40.1")
    Set objStdOut = objWshScriptExec.StdOut

    Do Until objStdOut.AtEndOfStream
        strLine = objStdOut.ReadLine
        If Len(strLine) > 2 Then
            WScript.Echo Now & " -- " & strLine
        Else
            Wscript.Echo strLine
        End If
    Loop


    Skier
  • Monday, December 29, 2008 10:57 PMMatthewBeattie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hey...you could also use WMI to enumerate your systems IP Address and send a ping to the remote systems. I've also added a timeout so it won't run indefinately if you never recieve the ipaddress you expect. Try modifying the variables in this script to meet your requirements...just add the IP Address of the systems you want to ping to the array.

    Option Explicit  
    'On Error Resume Next  
       ProcessScript  
       If Err.Number <> 0 Then 
          Wscript.Quit  
       End If 
    On Error Goto 0  
    '----------------------------------------------------------------------------------------------------------------------------  
    Function ProcessScript  
       Dim correctIPAddress, ipAddresses, ipAddress  
       Dim timeOut, waited  
       ipAddresses      = Array("192.168.1.1","192.168.1.2")  
       correctIPAddress = "192.168.1.2" 
       timeOut = 10  
       waited  = 0  
       Do While EnumerateIPAddress <> correctIPAddress Or waited >= timeOut   
          Wscript.Sleep 1000  
          waited = waited + 1  
          If waited >= timeOut Then 
             MsgBox "The script did not respond within " & waited & " seconds." 
             Exit Function 
          End If 
       Loop 
       For Each ipAddress In ipAddresses  
          If Not CheckConnection(ipAddress) Then 
             MsgBox ipAddress & " did not reply to a ping." 
          Else 
             MsgBox ipAddress & " replied to a ping." 
          End If 
       Next 
    End Function 
    '----------------------------------------------------------------------------------------------------------------------------  
    'Name       : EnumerateIPAddress -> Enumerates the IP Address of the local system via WMI.
    'Parameters : none               -> 
    'Return     : EnumerateIPAddress -> Returns the IP Address of the local system.  
    '----------------------------------------------------------------------------------------------------------------------------  
    Function EnumerateIPAddress  
       Dim objConfig, ipAddress  
       ipAddress = "" 
       For Each objConfig in Getobject("winmgmts:").ExecQuery("select * from Win32_NetworkAdapterConfiguration where IPEnabled = True")  
          ipAddress = objConfig.IPAddress(0)  
          If ipAddress <> "" And ipAddress <> "0.0.0.0" Then 
             Exit For 
          End If 
       Next 
       EnumerateIPAddress = ipAddress  
    End Function 
    '----------------------------------------------------------------------------------------------------------------------------  
    'Name       : CheckConnection -> Checks network connectivity of a remote host using WMI ping.  
    'Parameters : hostName        -> Hostname or IP Address of computer system to verify network connectivity with.  
    'Return     : Boolean         -> True if hostname replies. False otherwise.  
    '----------------------------------------------------------------------------------------------------------------------------  
    Function CheckConnection(hostName)  
       Dim ping, response, replied  
       Set ping = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _  
                           ("select * from Win32_PingStatus where address = '" & hostName & "'")  
       For each response in ping  
          replied = Not IsNull(response.StatusCode) And response.StatusCode = 0  
       Next 
       CheckConnection = replied  
    End Function 
    '---------------------------------------------------------------------------------------------------------------------------- 

    Hope this helps

    Cheers

    Matt :)