Update configuration on AeroHive APs with Powershell and SSH

2:01 pm Uncategorized
$ahouis = @("00-19-77","88-5b-dd","08-EA-44","D8-54-A2","C4-13-E2","40-18-b1")

#get domain admin creds
$dacreds = $(Get-Credential -Message "Please enter domain admin credentials for pulling DHCP info.")
$creds = $(Get-Credential -Message "Please enter AeroHive SSH username and password.")
$server = Read-Host -Prompt "DHCP Server name?"

$dhcpscopes = Invoke-Command -Credential $dacreds -ComputerName $server -ScriptBlock { Get-DhcpServerv4Scope } | select ScopeId
$dhcpleases = $dhcpscopes | ForEach-Object { $scope = $_.'ScopeID'; Invoke-Command -Credential $dacreds -ComputerName $server -ScriptBlock { Get-DhcpServerv4Lease -ScopeId $Using:scope} } 

$aps = @()

$ahouis | ForEach-Object {
    $oui = $_
    $aps += $dhcpleases | ? { $_.'ClientId' -like "$($oui)*" }
}

$aps | select -ExpandProperty ipaddress | % {

    if (Test-Connection -Count 1 -ComputerName $_) {
        New-SSHSession -ComputerName $_ -Credential $creds -AcceptKey -Force
    } else {
        return
    }
    
}

#Get the sessions where we successfully made a connection.
Get-SSHSession | ? { $_.'Connected' -eq $True } | select -ExpandProperty SessionID | % {

    $session = Get-SSHSession -SessionId $_
    $stream = $session.Session.CreateShellStream("dumb",0,0,0,0,1000)
    $stream.Write("show capwap client | include `"Primary Name`"`n")
    Start-Sleep -Seconds 1
    $result = $stream.Read()

    $result.Split([System.Environment]::NewLine) | % {
        if ($_ -like "*Primary Name:*") {
            if ($_ -like "*:cloud-va2-cws-1*") {
                write-host "Already updated"
                #already done
            } elseif ($_ -like "*aerohive.com") {
                #command ran successfully but the host needs updated.
                #$stream.Write("capwap client name cloud-va2-cws-1.aerohive.com`n")
                write-host "Host $($session.Host) needs updated"
                write-host $_
                $stream.Write("capwap client server name cloud-va2-cws-1.aerohive.com`n")
            }
        }
    }

}

#disconnect all sessions
Get-SSHSession | Remove-SSHSession

exit

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.