Feb 09 2020 10:56 AM
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!
Feb 12 2020 07:36 PM
@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
Feb 25 2020 01:44 AM - edited Feb 25 2020 05:16 AM
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?
Feb 25 2020 12:50 PM
@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
Feb 27 2020 03:58 AM
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:
Feb 27 2020 09:20 AM
@fstorer Awesome, thanks for the update. So we need to push commands that way whenever the command has params, good to know.
Regards
Erick
Mar 02 2020 06:43 AM
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:
Mar 02 2020 08:20 AM
@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
Mar 02 2020 11:53 PM
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:
Sep 06 2021 06:31 AM - edited Sep 06 2021 06:34 AM
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?