Skip to content

Instantly share code, notes, and snippets.

@jimfdavies
Created February 11, 2013 14:29
Show Gist options
  • Save jimfdavies/4754707 to your computer and use it in GitHub Desktop.
Save jimfdavies/4754707 to your computer and use it in GitHub Desktop.
Get-GatewayFromIPAddress returns your IP gateway from your IP address. This assumes that your default gateway is on the same /24 subnet and its node address is .1 (configurable). Useful for automatic deployment scripts.
Function Get-GatewayFromIPAddress {
param($ipaddress)
$routerip = ".1"
$gateway = (($ipaddress.Split("."))[0..2] -join ".") + $routerip
return $gateway
}
$ipaddress = "10.1.2.100"
$gateway = Get-GatewayFromIPAddress -ipaddress $ipaddress
$gateway # "10.1.2.1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment