Browse Source

CheckGateway_codereview

Alexandre MOTTIER 3 years ago
parent
commit
c54e17e918
1 changed files with 14 additions and 13 deletions
  1. 14 13
      Check Gateway.ps1

+ 14 - 13
Check Gateway.ps1

@@ -1,27 +1,28 @@
-$ErrorActionPreference= 'silentlycontinue'
-$LocalIP = ((Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.IPEnabled}).DefaultIPGateway)
+$LocalIP = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.IPEnabled}).DefaultIPGateway
 $MacAddress = ((Get-NetNeighbor -IPAddress $LocalIP).LinkLayerAddress)
 $PublicIP = ((Resolve-DnsName -Name myip.opendns.com -Server resolver1.opendns.com).IPAddress)
 
-$MacFreebox = 'F4-CA-E5*'
-$MacLivebox = '00-37-B7*'
-$MacApple = 'BC-B8-63*'
-$MacXiaomi = 'E0-CC-F8*'
+$MacFreebox = "A0-B1-C2","F4-CA-E5"
+$MacLivebox = "00-37-B7"
+$MacApple = "BC-B8-63"
+$MacXiaomi = "E0-CC-F8"
+
+$MacSplit = $MacAddress -split "-"
+$MacPrefix = $MacSplit[0] + "-" + $MacSplit[1] + "-" + $MacSplit[2]
 
 # Checking if Mac Address is known
-if($MacAddress -like $MacFreebox) # Check for Freebox
+if($MacFreebox -contains $MacPrefix) # Check for Freebox
 {$Router = "a Freebox"}
-elseif($MacAddress -like $MacLivebox) # Check for Livebox
+elseif($MacLivebox -contains $MacPrefix) # Check for Livebox
 {$Router = "a Livebox"}
-elseif($MacAddress -like $MacApple) # Check for Apple Device
+elseif($MacApple -contains $MacPrefix) # Check for Apple Device
 {$Router = "an Apple device"}
-elseif($MacAddress -like $MacXiaomi) # Check for Xiaomi Device
+elseif($MacXiaomi -contains $MacPrefix) # Check for Xiaomi Device
 {$Router = "a Xiaomi device"}
 else # If router is unknown
 {$Router = "Unknown"}
 
 if ($Router -like 'Unknown') # In the case router is unknown
-{Write-Host "Router is unknown ($MacAddress)." ; exit 1}
-else
+{Write-Host "Router is unknown ($MacAddress). Maybe a randomized MAC address" ; exit 1} # Router is unknown, it's an error, so error code 1
+else # In the case router is known
 {Write-Host "Gateway is $Router ($MacAddress). `nLocal IP is $LocalIP. `nPublic IP is $PublicIP." ; exit 0} # Router is known, it's ok, error code 0
-echo $?