From d8217d16a6c81ecd331c16b050ed28b38818d5a4 Mon Sep 17 00:00:00 2001 From: Dejan Date: Fri, 20 Feb 2026 11:22:11 +0000 Subject: [PATCH] Update ip-settings.ps1 --- ip-settings.ps1 | 81 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/ip-settings.ps1 b/ip-settings.ps1 index d72ae9c..f795c4f 100644 --- a/ip-settings.ps1 +++ b/ip-settings.ps1 @@ -3,6 +3,14 @@ function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green } function Write-Info($msg) { Write-Host " [..] $msg" -ForegroundColor Yellow } function Write-Err($msg) { Write-Host " [!!] $msg" -ForegroundColor Red } +function Write-Warn($msg) { Write-Host " [!!] $msg" -ForegroundColor DarkYellow } + +function Test-IPInUse($ip) { + Write-Info "Checking if $ip is already in use on the network..." + $ping = New-Object System.Net.NetworkInformation.Ping + $result = $ping.Send($ip, 1000) + return ($result.Status -eq "Success") +} # Admin check $id = [Security.Principal.WindowsIdentity]::GetCurrent() @@ -60,7 +68,28 @@ $nicName = $selected.Adapter.Name $nicIdx = $selected.Adapter.InterfaceIndex Write-OK "Selected: $nicName (Index $nicIdx)" -# STEP 3 - Choose profile +# STEP 3 - Show current config +Write-Host "" +Write-Host " Current Configuration:" -ForegroundColor Magenta +Write-Host " ----------------------------------------" -ForegroundColor DarkGray + +$curIP = Get-NetIPAddress -InterfaceIndex $nicIdx -AddressFamily IPv4 -ErrorAction SilentlyContinue +$curIface = Get-NetIPInterface -InterfaceIndex $nicIdx -AddressFamily IPv4 -ErrorAction SilentlyContinue +$curRoute = Get-NetRoute -InterfaceIndex $nicIdx -DestinationPrefix "0.0.0.0/0" -ErrorAction SilentlyContinue +$curDns = Get-DnsClientServerAddress -InterfaceIndex $nicIdx -AddressFamily IPv4 -ErrorAction SilentlyContinue + +$curMode = if ($curIface -and $curIface.Dhcp -eq "Enabled") { "DHCP (Automatic)" } else { "Static" } +$curIPStr = if ($curIP) { "$($curIP.IPAddress)/$($curIP.PrefixLength)" } else { "Not assigned" } +$curGWStr = if ($curRoute) { $curRoute.NextHop } else { "None" } +$curDnsStr = if ($curDns -and $curDns.ServerAddresses) { $curDns.ServerAddresses -join " / " } else { "Automatic" } + +Write-Host (" Mode : {0}" -f $curMode) -ForegroundColor White +Write-Host (" IP : {0}" -f $curIPStr) -ForegroundColor White +Write-Host (" Gateway : {0}" -f $curGWStr) -ForegroundColor White +Write-Host (" DNS : {0}" -f $curDnsStr) -ForegroundColor White +Write-Host " ----------------------------------------" -ForegroundColor DarkGray + +# STEP 4 - Choose profile Write-Host "" Write-Host " Profiles:" -ForegroundColor White Write-Host " [1] Camera Control -> 192.168.178.22 /24 GW: 192.168.178.1 DNS: 192.168.178.10 / 192.168.178.14" -ForegroundColor White @@ -70,7 +99,25 @@ Write-Host " [4] DHCP (automatic) Write-Host "" $profileChoice = Read-Host " Enter profile number" -# STEP 4 - Apply +# STEP 5 - Confirmation +$profileNames = @{ "1" = "Camera Control -> 192.168.178.22 /24"; "2" = "LAN Profile 2 -> 192.168.1.222 /24"; "3" = "DHCP (automatic)"; "4" = "DHCP + custom DNS" } +$chosenName = if ($profileNames.ContainsKey($profileChoice)) { $profileNames[$profileChoice] } else { "Unknown" } + +Write-Host "" +Write-Host " You are about to apply:" -ForegroundColor Yellow +Write-Host " Profile : $chosenName" -ForegroundColor Yellow +Write-Host " Adapter : $nicName" -ForegroundColor Yellow +Write-Host "" +$confirm = Read-Host " Confirm? (Y/N)" +if ($confirm -notmatch "^[Yy]$") { + Write-Host "" + Write-Info "Cancelled. No changes were made." + Write-Host "" + Read-Host "Press Enter to exit" + exit 0 +} + +# STEP 6 - Apply Write-Info "Applying settings..." if ($profileChoice -eq "1") { @@ -80,6 +127,21 @@ if ($profileChoice -eq "1") { $dns1 = "192.168.178.10" $dns2 = "192.168.178.14" + if (Test-IPInUse $newIP) { + Write-Host "" + Write-Warn "WARNING: $newIP is already responding on the network!" + Write-Warn "Another device may be using this IP address." + Write-Host "" + $forceApply = Read-Host " Apply anyway? (Y/N)" + if ($forceApply -notmatch "^[Yy]$") { + Write-Info "Cancelled. No changes were made." + Read-Host "Press Enter to exit" + exit 0 + } + } else { + Write-OK "$newIP is free - no conflict detected" + } + Remove-NetIPAddress -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue Remove-NetRoute -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue New-NetIPAddress -InterfaceIndex $nicIdx -IPAddress $newIP -PrefixLength $prefix -DefaultGateway $gateway | Out-Null @@ -96,6 +158,21 @@ elseif ($profileChoice -eq "2") { $prefix = 24 $gateway = "192.168.1.1" + if (Test-IPInUse $newIP) { + Write-Host "" + Write-Warn "WARNING: $newIP is already responding on the network!" + Write-Warn "Another device may be using this IP address." + Write-Host "" + $forceApply = Read-Host " Apply anyway? (Y/N)" + if ($forceApply -notmatch "^[Yy]$") { + Write-Info "Cancelled. No changes were made." + Read-Host "Press Enter to exit" + exit 0 + } + } else { + Write-OK "$newIP is free - no conflict detected" + } + Remove-NetIPAddress -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue Remove-NetRoute -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue New-NetIPAddress -InterfaceIndex $nicIdx -IPAddress $newIP -PrefixLength $prefix -DefaultGateway $gateway | Out-Null