Retrieving information from multiple Access Points via PowerShell

Contributor

Hello everyone!

 

I am doing a WiFi site survey and I would need to retrieve some information from all the Access Points installed in our school (around 136). I wonder if it would be feasible to write a Powershell script which should be able to:

- Connect to all the Aerohive Access Points via SSH (from a list of IP addresses)
- Run a specific command for each AP (e.g. "show interface")
- Export the results of that command to a txt/html file

My purpose is getting all the different MAC addresses used by each AP and listing them on a txt/html file.

I have been told that I could use a Python module (Aeromiko) to extract data from the Access Points but I am not familiar with the Python language. 

Could you please advise on this?

 

Thank you in advance!

9 Replies

@fstorer I wrote this, I think it covers what you are trying to accomplish. 

Let me know how it goes.

 

 <#
.SYNOPSIS
    Create ssh session and run a command from a list of hosts
.DESCRIPTION
    
.NOTES
    Author:     Erick A. Moreno
    Email:      emoreno@request-script.com
    Date:       12 Jan 2020
    PowerShell: v3
.References
#https://www.powershellgallery.com/packages/Posh-SSH/2.1  #Install Posh-Ssh Module to use the corresponding cmdlets
#requested: https://techcommunity.microsoft.com/t5/forums/replypage/board-id/WindowsPowerShell/message-id/1011

#>

$WorkingDir  = "C:\Users\erick\Desktop\LogsFolder"
$ScriptTime  = Get-Date -UFormat "%m%d%Y-%H%M"
$Credentials = Get-Credential
$APList      = Import-Csv "$WorkingDir\APIPs.csv"
$Output      = @()
 
Foreach($AP in $APList)
 {
  Write-Host "Working on AP: $($AP.Name)" -ForegroundColor Cyan
  $Command = $AP.Command 
  $Results = $null
  $session = New-SSHSession -ComputerName $AP.IP -Credential $Credentials -AcceptKey

  If(![string]::IsNullOrEmpty($($session)))
   {
    $Results  = Invoke-SSHCommand -Index $session.SessionId -Command $Command 
    
    $Properties  = [Ordered]@{
                              APName      = $AP.Name
                              APIPAddress = $AP.IP
                              Command     = $Command
                              Results     = $($Results.Output) -join ";" #Adapt this depending on the type of output that you expect 
                             }
    
    $Output  += New-Object PSObject -Property $Properties
    
    Remove-SSHSession -SessionId $session.SessionId
   }
  Else
   {
    Write-Host "Unable to stablish ssh session with host: $($AP.Name) / $($AP.IP)" -ForegroundColor Yellow
   }
 }

 $Output | Export-Csv "$WorkingDir\APsReport_$Scripttime.csv" -NoTypeInformation -Encoding UTF8

 

 

Dear@Erick A. Moreno R.

 

Thank you so much for your kind reply and apologies for replying so late, I was off for a few weeks... I have tried your script, I am able to start a SSH session with the AP (I can see the SessionID and that the connection is working), but the output of the commands is always empty. When I try to run the single commands, I got something like this:

 

Host : 192.168.x.x
Output : {}
ExitStatus : 255

 

For example, I tried to run the following commands:

Invoke-SSHCommand -SessionId 0 -Command "show interface"

Invoke-SSHCommand -SessionId 0 -Command "show ssid"

In both cases, I got the results above.

I see that the ExitStatus is returning a 255 error code, but I can't understand why.

 

When running those commands on the AP, I get tables with multiple columns (Name/MAC addr/Mode/State/Chan/VLAN/Radio/Hive/SSID) and a list of MAC addresses/SSIDs.

 

Any suggestions?

 

 

 

 

 

 

 

 

 

@fstorer don't worry I understand, I'm glad to help. 

About the error, have you confirmed that the network is ok? I mean the session was established correctly but there could be a timeout so please increase the timeout option to see how it behaves.  Could you try with a single command like help just to see if it is taking the command string correctly?

 

I don't think the format is a problem is should display something as I said in my comment in the script we may need to work on formatting that table. 

 

Regards 

Hi @Erick A. Moreno R.

 

I was able to solve my problem thanks to this article: https://github.com/darkoperator/Posh-SSH/issues/146

Using New-SSHShellStream instead of Invoke-SSHCommand  returned all the results I wanted.

I need to tweak the final script, but these are the commands I used:

 
$Credentials = Get-Credential
$session = New-SSHSession -ComputerName x.x.x.x -Credential $Credentials -AcceptKey
$SSHStream = New-SSHShellStream -SessionId 0
$SSHStream.WriteLine("show interface")
$SSHStream.read()
 
I hope this may be of some help!
Thanks again for your help, I really appreciate that! And thanks to the creator of "Posh-SSH" module!

@fstorer Awesome, thanks for the update. So we need to push commands that way whenever the command has params, good to know. 

 

Regards

Erick

Dear @Erick A. Moreno R. 

 

Unfortunately, I am still having issues with the output...

I want to send the output of the "$SSHStream.read()" to a txt file, but when I run the command:

 

Out-File -FilePath ".\APInterfaces.txt" -InputObject $SSHStream.read()
 
I got a blank txt file. 
Any suggestions?

@fstorer Hello, you got that as once you run the read from the stream you reset the variable to no data. 

I've tested this saving the read output to a new Variable and it is working for me, please test it and let me know the outcome.

 

$session = New-SSHSession -ComputerName $AP.IP -Credential $Credentials -AcceptKey
$SSHStream = New-SSHShellStream -SessionId $session.SessionId
$SSHStream.WriteLine("show interface")

$OutputVar =  $null
$OutputVar = $($SSHStream.read())
$OutputVar 

$OutputVar | Out-File -FilePath "$WorkingDir\APInterfaces.txt" -Encoding utf8 

 

Regards

Erick Moreno

@Erick A. Moreno R.

 

Thank you for your reply!

I managed to make it work, but I had to add the "Start-Sleep" command between the WriteLine and the SSHStream.read, otherwise it didn't work. So here is the script that worked for me:

 

$Credentials = Get-Credential
$session = New-SSHSession -ComputerName x.x.x.x -Credential $Credentials -AcceptKey
$SSHStream = New-SSHShellStream -SessionId 0
$SSHStream.WriteLine("show interface")
Start-Sleep 4
$OutputVar = $($SSHStream.read())
$OutputVar | Out-File -FilePath ".\APInterfaces.txt" -Encoding utf8
 
Thanks again @Erick A. Moreno R.  for all your help!

Dear @Erick A. Moreno R

Thank you for the above script. It was insightful and helped me achieved a goal.
I'm at present working on another project where I have automated the script using kshell. But now I'm trying run the script from Windows PowerShell.
Below is the script:

#! /bin/ksh
Variable= install_dir/xyz.cfg
Export Variable
$install_dir/pmgr -i $install_dir/abc/init.cfg
echo "application started"
Sleep 2
Ps - ef|grep pmgr
Echo $!


In PowerShell, I'm able to check the status of the application using invoke-sshcommand -sessionid 0 -command "ps -ef|grep pmgr

It gives the status of the application, but need to start the service using PowerShell. Could you please help?