locked
Ping IP Range with HostNames? RRS feed

  • Question

  • I use a ping script at work which allows me to run a ping command on a range of IPs (I have provided the main part of the code below (you can also refer back to an older posting at http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/c510e5a2-5449-4012-9773-4b53b9fa0d55))

    I have tried using the -a command to show the HostName as well as the status of the IP Address, but it will only display the HostName  for the first IP Address. Is there a way to expand the -a function to include a range of IPs or will it only work on the initial address? I have been researching this for over a month (I'm not a programmer I just get tasked t=with things at work) and have not been able to find anything that will allow me to ping a range of IP Addresses and show their HostNames? Thank you for your assistance.

    <P><SPAN ID="AppName">Application Name</SPAN> <B>Version</B> <SPAN ID=AppVer>0.00</SPAN><BR> <br /> <TABLE border="0" cellpadding="0" cellspacing="0" width="95%"> <TR> <TD width=100% align=left> <B>Enter Class C IP Range From: </B> <INPUT TYPE="text" NAME="FromIPRange" SIZE="25"> <INPUT ID="submit" CLASS="button" TYPE="button" VALUE="Set From IP" NAME="submit" onClick="SetFromIP(FromIPRange.Value)"> </br><B>Enter Class C IP Range To: </B> <INPUT TYPE="text" NAME="ToIPRange" SIZE="25"> <INPUT ID="submit" CLASS="button" TYPE="button" VALUE="Set To IP" NAME="submit" onClick="SetToIP(ToIPRange.Value)"> <INPUT ID="submit" CLASS="button" TYPE="button" VALUE="ping" NAME="submit" onClick="PingHosts" TITLE="Click here to submit the request."> <INPUT ID="submit" CLASS="button" TYPE="button" VALUE="Review Buffer" NAME="ViewAll" onClick="ViewBuffer" TITLE="Click here to Expose Buffer."> </TD> <TR><TD colSpan="2"> </TD></TR> </TR> </TABLE> <TABLE border=0 cellpadding=0 cellspacing=0 width="95%"> <TR><TD width=100% align=left> <SPAN ID=PingResults></SPAN>

    Wednesday, August 15, 2012 7:22 PM

Answers

  • <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>My HTML Application</title>
    <script language="vbscript">
    Dim wmi
        Sub window_onload
            Set wmi=GetObject("winmgmts:root\CimV2")
        End Sub
        Sub PingRange()
            For n=oct4.Value To octEnd.Value
                newIP = oct1.Value & "." & oct2.Value& "." & oct3.Value & "." & n
                wql="SELECT * FROM Win32_PingStatus " _
                     & "WHERE Address='" & newIP & "' " _
                     & "AND ResolveAddressNames=True"
                Set pings = wmi.ExecQuery(wql,,48)
                For Each ping In pings
                    Set opt = document.createElement("option")
                    If ping.StatusCode = 0 Then 
                        opt.style.color="green"
                    Else
                        opt.style.color="red"
                    End If
                    opt.innerText = "pinging host " & ping.ProtocolAddressResolved  & " Status:" & ping.StatusCode
                    results.appendChild(opt)
                    results.blur
                Next
            Next
        End Sub
    </script>
    <hta:application
    	applicationname="MyHTA"	
    	border="dialog"
    	borderstyle="normal"
    	caption="My HTML Application"
    	contextmenu="no"
    	icon="myicon.ico"
    	maximizebutton="no"
    	minimizebutton="yes"
    	navigable="no"
    	scroll="no"
    	selection="no"
    	showintaskbar="yes"
    	singleinstance="yes"
    	sysmenu="yes"
    	version="1.0"
    	windowstate="normal"
    >
    </head>
    <body>
    <table border="0" cellpadding="0" cellspacing="0" width="95%">
        <tr>
            <td>
                <h1>Application Name</h1>
                <h4>0.00</h4>
            </td>
        </tr>
        <tr>
            <td>
                <label>Enter Class C Address and ending address last octet</label><br />
                <label>Start:</label>
                    <input type="text" name="oct1" maxlength="3" size="1" value="192"/>.
                    <input type="text" name="oct2" maxlength="3" size="1" value="168"/>.
                    <input type="text" name="oct3" maxlength="3" size="1" value="1"/>.
                    <input type="text" name="oct4" maxlength="3" size="1" value="90"/><br />
        		<label>&nbsp;Last:</label><input type="text" id="octEnd" maxlength="3" size="1" value="105"/><br />
                <input type="button" value="ping" onclick="PingRange" title="click here to submit the request." /><br />
            </td>
        <tr>
            <td>
                <select id="results" size="40" style="width:90%;"/>
             </td>
        </tr>
    </table>
    </body>
    </html>

    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, August 15, 2012 9:48 PM
    • Proposed as answer by Tom Lavedas Thursday, August 16, 2012 12:46 PM
    • Marked as answer by IamMred Friday, August 17, 2012 10:33 PM
    Wednesday, August 15, 2012 9:44 PM

All replies

  • That post which you linked to has old code on it.  Please post your revised code so we have current code with which to debug.


    I'm the most humble person you've ever met.

    Wednesday, August 15, 2012 8:07 PM
  • <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>My HTML Application</title>
    <script language="vbscript">
    Dim wmi
        Sub window_onload
            Set wmi=GetObject("winmgmts:root\CimV2")
        End Sub
        Sub PingRange()
            For n=oct4.Value To octEnd.Value
                newIP = oct1.Value & "." & oct2.Value& "." & oct3.Value & "." & n
                wql="SELECT * FROM Win32_PingStatus " _
                     & "WHERE Address='" & newIP & "' " _
                     & "AND ResolveAddressNames=True"
                Set pings = wmi.ExecQuery(wql,,48)
                For Each ping In pings
                    Set opt = document.createElement("option")
                    If ping.StatusCode = 0 Then 
                        opt.style.color="green"
                    Else
                        opt.style.color="red"
                    End If
                    opt.innerText = "pinging host " & ping.ProtocolAddressResolved  & " Status:" & ping.StatusCode
                    results.appendChild(opt)
                    results.blur
                Next
            Next
        End Sub
    </script>
    <hta:application
    	applicationname="MyHTA"	
    	border="dialog"
    	borderstyle="normal"
    	caption="My HTML Application"
    	contextmenu="no"
    	icon="myicon.ico"
    	maximizebutton="no"
    	minimizebutton="yes"
    	navigable="no"
    	scroll="no"
    	selection="no"
    	showintaskbar="yes"
    	singleinstance="yes"
    	sysmenu="yes"
    	version="1.0"
    	windowstate="normal"
    >
    </head>
    <body>
    <table border="0" cellpadding="0" cellspacing="0" width="95%">
        <tr>
            <td>
                <h1>Application Name</h1>
                <h4>0.00</h4>
            </td>
        </tr>
        <tr>
            <td>
                <label>Enter Class C Address and ending address last octet</label><br />
                <label>Start:</label>
                    <input type="text" name="oct1" maxlength="3" size="1" value="192"/>.
                    <input type="text" name="oct2" maxlength="3" size="1" value="168"/>.
                    <input type="text" name="oct3" maxlength="3" size="1" value="1"/>.
                    <input type="text" name="oct4" maxlength="3" size="1" value="90"/><br />
        		<label>&nbsp;Last:</label><input type="text" id="octEnd" maxlength="3" size="1" value="105"/><br />
                <input type="button" value="ping" onclick="PingRange" title="click here to submit the request." /><br />
            </td>
        <tr>
            <td>
                <select id="results" size="40" style="width:90%;"/>
             </td>
        </tr>
    </table>
    </body>
    </html>

    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, August 15, 2012 9:48 PM
    • Proposed as answer by Tom Lavedas Thursday, August 16, 2012 12:46 PM
    • Marked as answer by IamMred Friday, August 17, 2012 10:33 PM
    Wednesday, August 15, 2012 9:44 PM