###assumptions: you have the right commands to locate the MAC on a switch, example below for cisco, you have a txt doc that lists the IPs/Hostnames of your switches, the MAC is actually live and connected in the environment...###
$macaddy = Read-Host "Enter mac address in 1234.abcd.1234 format: "
[String[]]$Commands = @("***checkcommands***","en","***checkcommands***","terminal length 0","show mac address-table address $macaddy","exit")
###check that these commands do what you expect first, then script
$Port = "23"
$WaitTime = 500
CLS
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
$ErrorActionPreference= 'silentlycontinue'
Clear-Variable $switches -Scope Global
Clear-Variable $result -Scope Global
Clear-Variable $check -Scope Global
$switches = Get-Content \\sever.domain.edu\share$\scripts\switches.txt
##############relies on having a txt doc that lists the IP’s of your switches
Foreach ($remotehost in $switches)
{
Write-Progress -activity "Searching..."
Clear-Variable $result -Scope Global
$Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
If ($Socket)
{
$Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.AsciiEncoding
ForEach ($Command in $Commands)
{ $Writer.WriteLine($command)
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
}
Start-Sleep -Milliseconds ($WaitTime * 4)
While($Stream.DataAvailable)
{ $Read = $Stream.Read($Buffer, 0, 1024)
$Result = ($Encoding.GetString($Buffer, 0, $Read))
$Result += [string]::concat($Result,",")
}
$check.success=$false
$check = $result -match 'DYNAMIC'
####trigger word, check manually that this makes sense for your environment
####note any devices routing the mac will show up, including switches trunking the traffic etc
if($check)
{
write-host ""
write-host "**** Located $macaddy on $remotehost ****"
write-host ""
Clear-Variable $result -Scope Global}
}
Else
{ write-host "Unable to connect to host: $RemoteHost...."
}
}
Comments