Hi All,
I’m stumped on this script that is 80 percent done but having trouble getting loop working correctly. The goal of this script is to find MAC Address in ARP table whose prefix matches that of ones in a array. Those that match have a particular action taken against it.
Steps Broken Down:
- Find IP address(s) of active adapter
- Ping every address on network to build out ARP table
- Used custom build function for converting results of “arp -a” into object.
- Defined array for all prefixes to be searched for
- Foreach loop for evaluatign each arp entry and converting into prefix for comparison to each prefix in array
- Do Loop inside foreach that takes the current ARP entry and checks to see if it matches each entry in $ubiquitis. There is control variable that increments in each cycle of the do loop that stops after 14 which is size of array.
Problem:
foreach seems to cycle properly through each mac prefix but it doesn’t take appropriate action when it comes to finding a match for particular MAC that should line up with value 14 in the array. When manually setting $arptable3 to “80-2a-a8” it works fine from the if construct down.
HELP!!! I think somehting is not right with the variable assignments or loop constructs.
$ip=gwmi Win32_NetworkAdapterConfiguration |
Where { $.IPAddress } | # filter the objects where an address actually exists
Select -Expand IPAddress | # retrieve only the property value
Where { $ -like ‘192.168.*’ }
$ipstart=$ip.Substring(0,11)
$ipstartvalue=“1”
$ipaddress=$ipstart + $ipstartvalue
[int]$control=1
Do {
Test-Connection -ComputerName $ipaddress -Count 2 -BufferSize 16
$control=$control + 1
$ipstart=$ip.Substring(0,11)
$ipaddress=$ipstart + $control
}Until ($control -gt “254”)
$arptable=arp -a
$ubiquitis=@(“00156d”,“00-1b-67”,“00-27-22”,“6c-5e-7a”,“00-15-6d”,“00-1b-67”,“00-27-22”,“04-18-d6”,“24-a4-3c”,“30-15-18”,“68-72-51”,“6c-5e-7a”,“9c-b0-08”,“dc-9f-db”,“80-2a-a8”)
$arp = “(?(\d{1,3}.){3}\d{1,3})\s+(?(\w{2}-){5}\w{2})\s+(?\w+$)”
$arptables=arp -g | select -skip 3 | foreach {$.Trim()} | convertfrom-text $arp
$arptables=$arptables | ? {$.type -eq “dynamic”}
foreach ($arptable in $arptables){
$arptable3=$arptable.mac
$arptable3=$arptable3.substring(0,8)
[int]$arraycontrol=0
#start of do until loop to increment array value until last is entry is reached
do {
if ($arptable3 -eq $ubiquitis[14]){
$ipaddressfinal=$arptable.ipaddress
Start-Process ‘C:\Users\Conference Rm - PC\Downloads\putty.exe’ -ArgumentList -ssh admin@$arptable.ipaddress -pw 1234
$arraycontrol=$arraycontrol + 1
}
else{
write-host “No Match. Looking at Next MAC Entry” -ForegroundColor Blue
$arraycontrol=$arraycontrol + 1
}
}until ($arraycontrol -gt “14”)
}