The following PowerShell script downloads the list of connected clients to Aruba Access Point and sets the corresponding openHAB Item state to ON/OFF:
The following command needs to be run only once in powershell to install the necessary module:
find-module posh-ssh | install-module
Powershell script exportwifi.ps1:
$ArubaIP = "192.168.229.23"
$Password = "mypassword"
$User = "admin"
$Command = "sh clients"
$ClientsFilePath = "/home/openhabian/exportwifi.exp"
Get-SSHTrustedHost | Remove-SSHTrustedHost
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
$SessionID = New-SSHSession -ComputerName $ArubaIP -Credential $Credentials -AcceptKey
$SSHStream = New-SSHShellStream -Index $SessionId.SessionId
$streamOut = $SSHStream.Read()
while ($streamOut -notlike "*# *") {
Start-Sleep -s 1
$streamOut = $SSHStream.Read()
}
Write-Output "SSH Connected, sending sh clients"
$OutputString = Invoke-SSHStreamShellCommand -ShellStream $SSHStream -Command $Command
# Invoke-SSHStreamShellCommand -ShellStream $SSHStream -Command $Command | Out-File -NoNewline -FilePath $ClientsFilePath -outvariable $OutputString
Write-Output "sh clients finished"
Remove-SSHSession -SessionId $SessionId.SessionId
Write-Output "Writing to file"
Set-Content -Path $ClientsFilePath -Value $OutputString
$OutputStringText = [string]$OutputString
Write-Output $OutputStringText
Write-Output ""
if ($OutputStringText.IndexOf("7d:73:b5:77:19:fa") -gt 0) {
Write-Output "Miha is present"
& /usr/bin/curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://192.168.212.4:8080/rest/items/PresenceMiha"
}
else {
Write-Output "Miha is absent"
& /usr/bin/curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "OFF" "http://192.168.212.4:8080/rest/items/PresenceMiha"
}
Write-Output ""
if ($OutputStringText.IndexOf("42:ab:65:18:cd:22") -gt 0) {
Write-Output "Peter is present"
& /usr/bin/curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://192.168.212.4:8080/rest/items/PresencePeter"
}
else {
Write-Output "Peter is absent"
& /usr/bin/curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "OFF" "http://192.168.212.4:8080/rest/items/PresencePeter"
}
Write-Output ""
When executing the above script exportwifi.ps1 on debian/rapsberry pi the following runexportwifi.sh is used (this is necessary to avoid a problem that powershell does not terminate):
env TERM=linux /usr/bin/pwsh /home/openhabian/exportwifi.ps1
To run this script periodically every minute start crontab -e and add the following lines:
MAILTO=""
* * * * * /home/openhabian/runexportwifi.sh