cancel
Showing results for 
Search instead for 
Did you mean: 

Assistance with automating cli commands on AP using Python/Paramiko

Assistance with automating cli commands on AP using Python/Paramiko

chris_krieter
New Contributor

I am trying to build a python script to connect to the APs cli via SSH using Paramiko, issue commands and collect the output. Ultimately, I want to develop a more automated and reliable method to upgrading the APs software in the field.

 

I am able to connect to the AP using paramiko.SSHClient() and .connect() and this appears to be successful. When I issue .exec_command("show version") for example, I get nothing back in the stdout buffer. Code example below:

 

ssh = paramiko.SSHClient()

   ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

   print("Connecting to AP")

   ssh.connect(hostname=ap_ip, username="admin", password="1234")

   print("Connected -sending command to AP")

   stdin, stdout, stderr = ssh.exec_command("show version")

   print("command issued")

   data_out = stdout.readlines()

   print(data_out)

   stdin.flush()

   ssh.close()

 

Has anyone successfully been able to script against the Aerohive APs? Any direction would be appreciated as I am not making progress with this effort. Thanks!

13 REPLIES 13

dennisph
New Contributor III

Chris that is pretty awesome. I personally think they will keep HiveOS it seems to work well. I don't know much about Extreme's OS or if they even offer CLI. I just hope they keep improving the cloud management because it seems very clunky to me.

chris_krieter
New Contributor

Luckily, I was able to get this working using a Paramiko shell. The code below is an example of how I can send and get feedback from non-interactive commands (like show commands). I will post something on Netmiko so see if we can get HiveOS support added, however, what is the future of HiveOS with the acquisition? Would it be worth the effort?

------------------------------------------------------------

 

# Import pprint (print pretty) and Paramiko libs

from pprint import pprint

import paramiko

 

# Function

def TestSSHtoAP():

# Get host/ip from user

host = input("Enter hostname or ip of device: ")

 

# Setup and connect to AP on SSH - auto accept host key

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

print("Connecting to AP")

ssh.connect(hostname=host, port=22, username='admin', password='XXXX')

 

# Create channel for sending data and prefill the var

channel = ssh.invoke_shell()

channel_data = str(channel.recv(9999))

 

# While loop untli we exit

while True:

# if there is data on the receive of channel, print it otherwise continue

if channel.recv_ready():

channel_data += str(channel.recv(9999))

print('######### Device Output #########
')

pprint(channel_data)

print('
##################')

else:

continue

 

# if the channel_data ends with the hostname#' (note the ' after # is necessary)

if channel_data.endswith(host + "#'"):

# get a command from user to send to AP

show_command = input("Enter a show command to send: ")

# send it and print the output

channel.send(show_command + '
')

channel.send('
')

channel_data += str(channel.recv(9999))

print('
######### Device Output #########
')

pprint(channel_data)

print('
##################
')

#exit while loop and exit function

return True

 

#close the ssh session

ssh.close()

 

# Function call and exit

TestSSHtoAP()

print("Exiting")

 

dennisph
New Contributor III

If you want to send commands via SSH to a large group of access points in my case 6600 access points. I would use Cattools by solarwinds. It is not perfect but it works for most things and is not that expensive. I use it to run VLAN probes all the time.

sderikonja1
Contributor

I started looking into this two years ago. Might be helpful to ping Kirk Byers, the creator of Netmiko to look into the output and add support for HiveOS.

GTM-P2G8KFN