Infoblox IP Reservierung via WebAPI in Powershell auslesen

Mit folgendem Powershell Skript, kann man über Servername, Domäne und VLAN ID eine Adressreservierung auf der Infoblox auslesen.

Die Abwicklung erfolgt über die WebAPI der Infoblox. Die Zugangsdaten stehen der Einfachheit halber im Skript, können aber natürlich auch via Übergabe oder Eingabe abgefragt werden.

Die VLAN ID wird via Extensible Attributes abgefragt, welche natürlich gepflegt sein sollten.
Als Rückgabe erscheint ein Objekt, mit IP-Adresse, Netzwerk, Maske, Gateway, VLAN ID und Typ der gefundenen Reservierung / IP Info

 

-> .get-infoblox-data.ps1 -ServerName "xyz" -DNSDomain "domain.local" -vlanID "10"

 

[CMDletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$ServerName,
[Parameter(Mandatory = $true, Position = 1)]
[string]$DNSDomain,
[Parameter(Mandatory = $true, Position = 2)]
[string]$vlanid
)

$global:ibAPIversion = "v2.7"
$global:ibFQDN = "infoblox_url"
$global:uriStart = "https://$global:ibFQDN/wapi/$global:ibAPIversion"
$pwd = ''
$username = ""
$secPW = $pwd | ConvertTo-SecureString  -asPlainText -Force
$global:cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secPW
$global:ityp=""

function get-fixed-addr ($vlanid, $ServerName, $DNSDomain) {
write-host "$ServerName.$DNSDomain"
try{
$fixedaddrs = Invoke-RestMethod -uri "$uriStart/fixedaddress?*SA_VLAN=$vlanid&_return_fields=options,name,comment,ipv4addr,extattrs&_max_results=1500"  -Credential $cred
$t = $fixedaddrs | ?{($_.name -like "*$ServerName.$DNSDomain*")}
$global:ityp="Reservation"
return $t.ipv4addr
}catch{
catchIBReturnError
return
}
}
function convert-prefix($prefix){
switch ($prefix) {
0{ "0.0.0.0" }
1{ "128.0.0.0" }
2{ "192.0.0.0" }
3{ "224.0.0.0" }
4{ "240.0.0.0" }
5{ "248.0.0.0" }
6{ "252.0.0.0" }
7{ "254.0.0.0" }
8{ "255.0.0.0" }
9{ "255.128.0.0" }
10{ "255.192.0.0" }
11{ "255.224.0.0" }
12{ "255.240.0.0" }
13{ "255.248.0.0" }
14{ "255.252.0.0" }
15{ "255.254.0.0" }
16{ "255.255.0.0" }
17{ "255.255.128.0" }
18{ "255.255.192.0" }
19{ "255.255.224.0" }
20{ "255.255.240.0" }
21{ "255.255.248.0" }
22{ "255.255.252.0" }
23{ "255.255.254.0" }
24{ "255.255.255.0" }
25{ "255.255.255.128" }
26{ "255.255.255.192" }
27{ "255.255.255.224" }
28{ "255.255.255.240" }
29{ "255.255.255.248" }
30{ "255.255.255.252" }
31{ "255.255.255.254" }
32{ "255.255.255.255" }
}
}
function catchIBReturnError{
$response = $_.Exception.Response
if ($response.StatusCode -eq [System.Net.HttpStatusCode]::BadRequest) {
$stream = $response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($stream)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
Write-Verbose $responseBody
$wapiErr = ConvertFrom-Json $responseBody
throw [Exception] "$($wapiErr.Error)"
$wapiErr
} else {
throw
}
}
function get-ip-from-arecord($ServerName,$DNSDomain){
#gibt es schon einen a-record?
try{
$infobloxHost = Invoke-RestMethod -uri "$global:uriStart/record:host?name~=$ServerName.$DNSDomain" -Credential $global:cred
$global:ityp="Host-Record"
#wechel ip?
$ip = $infobloxHost[0].ipv4addrs.ipv4addr
}catch{
catchIBReturnError
return
}
#gibt es schon einen host-record, wenn keinen a-record?
try{
if(!($infobloxHost)){
$infobloxHost = Invoke-RestMethod -uri "$global:uriStart/record:a?name~=$ServerName.$DNSDomain" -Credential $global:cred
$global:ityp="A-Record"
$ip = $infobloxHost[0].ipv4addr
}
}catch{
catchIBReturnError
return
}
return $ip
}
function get-network-from-ip($ip){
#erweiterte infos zur ip
try{
$infobloxIPv4Address = Invoke-RestMethod -uri "$global:uriStart/ipv4address?ip_address=$ip" -Credential $global:cred
#netzwerk
$ipNetwork = $infobloxIPv4Address[0].network
}catch{
catchIBReturnError
return
}
return $ipNetwork
}
function get-gateway-from-network ($ipNetwork) {
#gateway
try{
$infobloxGateway = Invoke-RestMethod -uri "$global:uriStart/network?ipv4addr=$ipNetwork&_return_fields=options,extattrs" -Credential $global:cred
$routers = $infobloxGateway[0].options | ?{$_.name -eq "routers"}
$routers = $routers.value
$vlan = $infobloxGateway[0].extattrs.SA_VLAN.value
}catch{
catchIBReturnError
return
}
return $routers, $vlan
}

#ip bekommen, sollte Reservierung, Host, a-record vorhanden sein
$returnIP = get-fixed-addr $vlanid $ServerName $DNSDomain
if(!($returnIP)){
write-host 'Reservierung nicht gefunden, suche nach Host- oder A-Record'
$returnIP = get-ip-from-arecord $ServerName $DNSDomain
}

#wenn ip vorhanden, netzwerk holen
if($returnIP){
$returnNW = get-network-from-ip $returnIP
$returnNoIP = "FALSE"
}else{$returnNoIP = "TRUE";$global:ityp=""}

#wenn netzwerk geholt, gateway finden
if($returnNW){
$returnGW = get-gateway-from-network $returnNW
$returnVLAN = $returnGW[1]
$returnGW = $returnGW[0]
$returnNWtmp = ($returnNW.split("/"))
$returnNW = ($returnNW.split("/"))["0"]
$returnSNM = convert-prefix $returnNWtmp["1"]
}

#werte zurückgeben
$ipObject = "" | Select IP,NW,SNM,GW,VLAN,Typ,NOIP
$ipObject.IP = $returnIP
$ipObject.NW = $returnNW
$ipObject.SNM = $returnSNM
$ipObject.GW = $returnGW
$ipObject.VLAN = $returnVLAN
$ipObject.Typ = $global:ityp
$ipObject.NOIP = $returnNoIP
write-host $ipObject
return $ipObject
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s