none
Ping subnet based on syntax..? RRS feed

  • Question

  • trying to word this right,

    What I would like to do is allow the user to enter a subnet and have all addresses in the subnet pinged for reply based on the syntax being correct and the routing table on the local machine be searched for the subnet as well,

    Example if the user enters 10.0.1 the script says that is not a valid syntax for a subnet please try again,

    however if they enter 10.0.0.0 for example then the script attempts to check the subnet range if you will and ping all addressses, this would be an internal test only.

    Essentially this would be something that would be run at the end of week or when starting the workweek to see if any systems appear offline.

    Any thoughts on how this would have to be declared?  Below is what I have so far:

    Write-host -foregroundcolor green "Enter ip address range to search: " -nonewline

    $a = Read-host

    if ($a -notlike "10.0.0.1"){ 

    write-host -foregroundcolor red "Address context $a is not valid!"

    getip

    }

    else {

    Write-Host ""

    write-host -foregroundcolor blue "Address is $a"

    ping $a"

    I used 10.0.0.1 as a way to just check that my if else was looking at $a.  I believe ($a -notlike "10.0.0.1") would need to be a wildcard though maybe with subnet range.  Sorry if this is confusing.  trying to get back into poweshell world.  Any thoughts are appreciated.


    JCtech1123, Cheers

    Tuesday, August 11, 2015 7:47 AM

Answers

  • I would add that just because a system doesn't respond to a ping doesn't mean it isn't connected to the network.

    A host could be online and connected but not respond to ICMP echo request packets (pings) if it is running a host-based firewall.

    Incorrectly configured TCP/IP or routing problems can also make it appear that a host isn't responding.

    I think you need a better definition of your actual problem before you start trying to write code to address it.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 12, 2015 3:11 PM
    Moderator

All replies

  • You need a mask to identify a subnet.

    Thus is a class C subnet: 10.0.0.0/24 or 10.0.0.0 255.255.255.0

    As is this:
     10.0.0.0/28 or 10.0.0.0 255.255.255.240


    \_(ツ)_/

    Tuesday, August 11, 2015 8:25 AM
  • Hi JCtech,

    Why do you want to check if its in the subnet etc. or not. If you have a router in between it can ping beyond its subnet as well. Like ping google.com etc.

    So point is if you can ping or not, hence don't check for subnet range etc. check if you can ping or not. If required you can ask which range to ping.(i.e. subnet as jrv mentioned)

    You can use Test-Connection as a ping replacement.

    $IP = "127.0.0.1"
    
    Test-Connection $IP

    And for the validation you need to refer this

    $IPAddress = "10.0.0"

    $IsValid = $IPAddress -match [IPAddress]$IPAddress $IsValid



    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Tuesday, August 11, 2015 10:54 AM
    Tuesday, August 11, 2015 10:53 AM
  • This is a reply all three posts.  All your information is great. Essentially I wanted to find a powershell way to run a non-indepth check to all hosts on the internal network just too see if I get a response.  As far as the subnet, I was more trying to figure out what the syntax would need too be and how it would need to be placed into the script so I could see it.  I'm getting back into the script world, will use what has been posted.

    Cheers.


    JCtech1123, Cheers

    Wednesday, August 12, 2015 6:57 AM
  • Unfortunately no one can figure out what you mean by "subnet syntax".  Subnets do not have a syntax.  A subnet has a subnet number, a subnet mask and a range of IP addresses.

    Given that try to ask your question.

    A simple ping of a full class "C" subnet looks like this:

    1..254 | %{ Test-Connection "10.0.0.$_" }

    Of course and internal network is made up of many subnets. If there are routers then there can be any manner of ranges and sub-netted networks.

    Start by ask your network administrators to show you a map of the network. 

    In most correctly configured domains AD will have a list of sites and subnets. This can be seen in the AD Sites and Subnets snapin.


    \_(ツ)_/

    Wednesday, August 12, 2015 7:06 AM
  • I would add that just because a system doesn't respond to a ping doesn't mean it isn't connected to the network.

    A host could be online and connected but not respond to ICMP echo request packets (pings) if it is running a host-based firewall.

    Incorrectly configured TCP/IP or routing problems can also make it appear that a host isn't responding.

    I think you need a better definition of your actual problem before you start trying to write code to address it.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 12, 2015 3:11 PM
    Moderator
  • OK as an update to what I was asking earlier, for example I have a subnet such as the following: 172.16.8.0/22, and I want to just do a test against just specific subnets within the range.  Like example the subnet of  172.16.8.1 - 172.16.11.254, and I also want to allow the range to be put into a variable and tested against that.

    Essentially just want to ping the specific subnet within the environment. The subnet may only have like 5 addresses in it, the whole Idea is really to see how I would write the variable to test.  So if its a 172.16.8.0/22 and I want the variable to test like:

    $address = "specific subnet to test", maybe this was all ready answered and I missed it.  Or maybe I asked the question wrong, sorry If I did.

    Want to be able to re populate the variable just like I would any variable.

    Thanks all.


    JCtech1123, Cheers

    Monday, February 15, 2016 3:10 AM
  • This thread has been answered for a year.  It would be best if you started a new question with a clear statement of what you need to do.


    \_(ツ)_/

    Monday, February 15, 2016 3:23 AM
  • August is a year?  There is no need, this is for lab testing and for script testing.  I'll ask my question in another Forum since this seems to be a hassle to some.

    Thanks all.


    JCtech1123, Cheers

    Monday, February 15, 2016 3:37 AM
  • Sorry but if you need to ask a new question you need to start a new topic. 

    The answer above shows you how to ping an address in a subnet.  Now you want to ping all addresses in a subnet which is pretty mch tegh sam.e

    net address + mask gives available bits

    /24 is 78bits or net address + 0-255,  First and last addresses are reserved so it is 1-254


    \_(ツ)_/

    Monday, February 15, 2016 3:49 AM
  • Example for a subnetted class  'C'  network

    173.110.189.161/29 is 32-29 = 3 bits or 7 addresses minus 2 (5)

    173.110.189.161-173.110.161.167

    or 173.110.161.161 + 1-6 where 161 is network address and 167 9s broadcast address.


    \_(ツ)_/

    Monday, February 15, 2016 4:00 AM