profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Friday, July 31, 2015

Increment those IP addresses

Have you ever wanted to perform a certain kind of action on a range of ip addresses using powercli or powershell? Well, I have.
for ex:
foreach ($host in $hosts) {scriptblock}
and here I only have the 1st ip of the hosts and the rest should auto increment.
Well thanks to Lucd (If you have ever seen anyone answering all the powercli queries on vmware communities) we have it now.
#let's get the 1st ip address
$host1 = Read-Host "type the 1st ip address"

#Then let us have the number of ip addresses that you want
$max = Read-Host "Maximum nubmer of ip addresses?"

#now split that ip address and retain the 1st 3 octets which will be fixed
$fixed = $host1.Split('.')[0..2]

#now let us have the last octet of the host's ip address
$last = [int]($host1.Split('.')[3])

#let us subtract 1 from the maximum number of IPs that we want since it counts from 0
$max_hosts = $max - 1

#now let us increment the $last octet with a +1 till we get $max_hosts of ip addresses
$hosts =
$last..($last + $max_hosts) | %{
    [string]::Join('.',$fixed) + "." + $_
}

#now let us print the results to the screen
$hosts

Get this IPincrementer on github.

3 comments:

  1. Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards,192.168.1.254

    ReplyDelete
  2. With the new legislation effective from October 2009 many new and existing entrepreneurs might be confused as to how many and what kind of addresses their company should maintain.best ip tracker online

    ReplyDelete
  3. Network address: This is the assignment utilized in steering to send parcels to a remote organization. For instance 10.0.0.0, 172.16.0.0, and 192.168.10.0 are network addresses.https://onohosting.com/

    ReplyDelete