145 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			PowerShell
		
	
	
	
	
	
			
		
		
	
	
			145 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			PowerShell
		
	
	
	
	
	
# Beschreibung: 
 | 
						|
# Parameter:    -Domain meindomainname.de
 | 
						|
# Autor:        Patrick Asmus
 | 
						|
# Web:          https://www.techniverse.net
 | 
						|
# Version:      1.0
 | 
						|
# Datum:        21.03.2025
 | 
						|
# Modifikation: Initial
 | 
						|
#####################################################
 | 
						|
 | 
						|
param (
 | 
						|
    [string]$Domain
 | 
						|
)
 | 
						|
 | 
						|
# Prüfen, ob eine Domain übergeben wurde
 | 
						|
if (-not $Domain) {
 | 
						|
    $result = @{
 | 
						|
        prtg = @{
 | 
						|
            error = 1
 | 
						|
            text  = "Error: No domain parameter provided!"
 | 
						|
        }
 | 
						|
    }
 | 
						|
    $result | ConvertTo-Json -Depth 3
 | 
						|
    exit 1
 | 
						|
}
 | 
						|
 | 
						|
# Pfad zur whois.exe
 | 
						|
$whoisPath = "C:\Apps\WhoIs\whois.exe"
 | 
						|
 | 
						|
# Prüfen ob whois.exe vorhanden ist
 | 
						|
if (!(Test-Path $whoisPath)) {
 | 
						|
    $result = @{
 | 
						|
        prtg = @{
 | 
						|
            error = 1
 | 
						|
            text  = "Error: whois.exe not found at $whoisPath"
 | 
						|
        }
 | 
						|
    }
 | 
						|
    $result | ConvertTo-Json -Depth 3
 | 
						|
    exit 1
 | 
						|
}
 | 
						|
 | 
						|
# Whois-Abfrage durchführen
 | 
						|
try {
 | 
						|
    $whoisResult = & $whoisPath -accepteula $Domain | Out-String
 | 
						|
    $whoisResult = $whoisResult -replace "`r`n", " " -replace "`n", " " -replace "`r", " " # Zeilenumbrüche entfernen
 | 
						|
    $whoisResult = $whoisResult.Trim()
 | 
						|
} catch {
 | 
						|
    $result = @{
 | 
						|
        prtg = @{
 | 
						|
            error = 1
 | 
						|
            text  = "Error during whois query"
 | 
						|
        }
 | 
						|
    }
 | 
						|
    $result | ConvertTo-Json -Depth 3
 | 
						|
    exit 1
 | 
						|
}
 | 
						|
 | 
						|
# Debugging: Whois-Output in eine Datei schreiben
 | 
						|
$whoisResult | Out-File "C:\Apps\WhoIs\whois_output.log"
 | 
						|
 | 
						|
# Prüfen, ob es sich um eine .de-Domain handelt
 | 
						|
$isDeDomain = $Domain -match "\.de$"
 | 
						|
 | 
						|
if ($isDeDomain) {
 | 
						|
    # .de-Domains liefern keine Creation/Updated-Daten -> Nur Status anzeigen
 | 
						|
    $creationDate = $updatedDate = $creationTimestamp = $updatedTimestamp = $null
 | 
						|
} else {
 | 
						|
    # Whois-Abfrage für .com/.net/.org-Domains auswerten
 | 
						|
    $creationDate = if ($whoisResult -match "(?i)Creation\s*Date:\s*([\d]{4}-[\d]{2}-[\d]{2})") { $matches[1] } else { $null }
 | 
						|
    $updatedDate  = if ($whoisResult -match "(?i)Updated\s*Date:\s*([\d]{4}-[\d]{2}-[\d]{2})")  { $matches[1] } else { $null }
 | 
						|
}
 | 
						|
 | 
						|
# Konvertiere Datum in YYYYMMDD (PRTG kann nur Zahlen speichern)
 | 
						|
function Convert-ToTimestamp($dateString) {
 | 
						|
    if ($dateString -match "\d{4}-\d{2}-\d{2}") {
 | 
						|
        return $dateString -replace "-", ""
 | 
						|
    } else {
 | 
						|
        return $null  # Falls kein Datum gefunden wurde
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
$creationTimestamp = Convert-ToTimestamp $creationDate
 | 
						|
$updatedTimestamp  = Convert-ToTimestamp $updatedDate
 | 
						|
 | 
						|
# Prüfen auf "Status: free" oder andere Indikatoren für nicht registrierte Domains
 | 
						|
if ($whoisResult -match "(?i)Status:\s+free" -or $whoisResult -match "(?i)No match" -or $whoisResult -match "(?i)not found" -or $whoisResult -match "(?i)does not exist") {
 | 
						|
    $result = @{
 | 
						|
        prtg = @{
 | 
						|
            result = @(@{
 | 
						|
                channel = "Domain Status"
 | 
						|
                value   = 2
 | 
						|
                unit    = "Custom"
 | 
						|
            })
 | 
						|
            text = "Domain '$Domain' is free! Risk of domain grabbing!"
 | 
						|
        }
 | 
						|
    }
 | 
						|
    $result | ConvertTo-Json -Depth 3
 | 
						|
    exit 2
 | 
						|
} else {
 | 
						|
    # Domain ist registriert
 | 
						|
    $prtgResult = @(
 | 
						|
        @{
 | 
						|
            channel = "Domain Status"
 | 
						|
            value   = 0
 | 
						|
            unit    = "Custom"
 | 
						|
        }
 | 
						|
    )
 | 
						|
 | 
						|
    # Nur bei Nicht-.de-Domains Created/Updated anzeigen
 | 
						|
    if (-not $isDeDomain) {
 | 
						|
        if ($creationTimestamp) {
 | 
						|
            $prtgResult += @{
 | 
						|
                channel = "Creation Date (YYYYMMDD)"
 | 
						|
                value   = $creationTimestamp
 | 
						|
                unit    = "Custom"
 | 
						|
            }
 | 
						|
        }
 | 
						|
        if ($updatedTimestamp) {
 | 
						|
            $prtgResult += @{
 | 
						|
                channel = "Updated Date (YYYYMMDD)"
 | 
						|
                value   = $updatedTimestamp
 | 
						|
                unit    = "Custom"
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    # Textausgabe für PRTG: Bei .de-Domains nur "registered" ohne Created/Updated
 | 
						|
    if ($isDeDomain) {
 | 
						|
        $statusText = "Domain '$Domain' is registered."
 | 
						|
    } else {
 | 
						|
        $statusText = "Domain '$Domain' is registered."
 | 
						|
        if ($creationDate) { $statusText += " Created: $creationDate" }
 | 
						|
        if ($updatedDate) { $statusText += ", Updated: $updatedDate" }
 | 
						|
    }
 | 
						|
 | 
						|
    # JSON für PRTG ausgeben
 | 
						|
    $result = @{
 | 
						|
        prtg = @{
 | 
						|
            result = $prtgResult
 | 
						|
            text   = $statusText
 | 
						|
        }
 | 
						|
    }
 | 
						|
    $result | ConvertTo-Json -Depth 3
 | 
						|
    exit 0
 | 
						|
}
 |