0

I am working to automate a script to log into a Ruckus AP (H510 Unleashed) and gather information from it using Paramiko. I have used the same Paramiko script to log into various other devices (linux, HP/Aruba, etc) without issue but when I try and connect to the Ruckus AP, even without sending a command, I get the output of 'Invalid Argument' I have error checking built in that detects invalid passwords and such so that shouldn't be it. I can also SSH from the same machine I am running the python script on without issue and without any strange headers in the SSH session.

Does anyone have any ideas? Code below:

import paramiko
import csv
from datetime import datetime
import getpass
from socket import error as socket_error


###Define Variables
ip_port = input("Enter Device IP and Port (xxx.xxx.xxx.xxx:yy):")   #Gets the IP address AND port number used to SSH to the devices - will be split apart later
username = input("Enter Username:")                                 #Gets the username for authentication to the device
passwd = input("Enter Password:")                                   #Gets the password for authentication to the device
command = input("Enter the command you wish to execute:")           #Gets the command that is to be executed on the machine
dev_ip_addr = ""                                                    #Variable holding just the IP address or FQDN for the device
dev_port = 0                                                        #Variable holding just the port number for the device

log_file = datetime.now().strftime("D:\\Networking\\Python Testing\\SSH Automation\\User Input with Pamaiko\\Command_Log_%Y%m%d-%H%M%S.csv") #Output file to write command audit log to
log_file_headers = ["Device IP", "Port", "User", "Device Auth Username", "Time", "Command", "Output", "Errors"]     #Headers for the CSFV log file
file_lines = []                                                                                     #Holds the lines for output into the csv file
current_user = getpass.getuser()    #Gets the user currently running the program for logging purposes


###Do Something
#Split IP from the port number for SSH session
dev_ip_addr = ip_port.split(':')[0]
dev_port = ip_port.split(':')[1]

#Try and except section for actuall ssh connection and handeling errors during connection (authentication, socket, and no response errors)
try:
    #Set up Paramiko SSH session
    ssh=paramiko.SSHClient()

    #Host key section for SSH connections
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    #Make the SSH connection
    ssh.connect(dev_ip_addr,dev_port,username,passwd)

    #Execute Command using the inputs from the user
    stdin,stdout,stderr=ssh.exec_command(command)
    stdin.close()

    #Clean up the command output for displaying on screen and to log file, close SSH session
    stdout = stdout.readlines()
    clean_command_output = ("".join(stdout))


    #Set and append inputs and outputs for log file
    output_line = [dev_ip_addr, dev_port, current_user, username, datetime.now(), command, clean_command_output]
    file_lines.append(output_line)

#Handles authentication errors while connecting to device
except paramiko.AuthenticationException:
    print("Authentication failed, please verify your credentials to ", dev_ip_addr)
    #Set and append inputs and outputs for log file, including error
    output_line = [dev_ip_addr, dev_port, current_user, username, datetime.now(), command, "", "AUTHENTICATION ERROR"]
    file_lines.append(output_line)
    print()

#Handles host key verification issues from the device
except paramiko.BadHostKeyException:
    print("Host key could not be verified from ", dev_ip_addr)
    #Set and append inputs and outputs for log file, including error
    output_line = [dev_ip_addr, dev_port, current_user, username, datetime.now(), command, "", "UNABLE TO VERIFY HOST KEY"]
    file_lines.append(output_line)
    print()

#Handles SSH errors while connecting to device
except paramiko.SSHException as sshException:
    print("Unable to establish SSH connection: %s" % sshException)
    #Set and append inputs and outputs for log file, including error
    output_line = [dev_ip_addr, dev_port, current_user, username, datetime.now(), command, "", "SSH CONNECTION EXCEPTION OCCURRED"]
    file_lines.append(output_line)
    print()

#Handles socket and refusal errors while connecting to device
except socket_error as socket_error:
    print("Connection refused/no response to ", dev_ip_addr)
    #Set and append inputs and outputs for log file, including error
    output_line = [dev_ip_addr, dev_port, current_user, username, datetime.now(), command, "", "CONNECTION REFUSED OR NO RESPONSE"]
    file_lines.append(output_line)
    print()

#Create CSV file for logging of what device, port, user, and commands were run along with a time stamp
with open(log_file, 'w', newline='') as log_out:
            csvwriter = csv.writer(log_out)
            csvwriter.writerow(log_file_headers)
            csvwriter.writerows(file_lines)
  • 'Invalid Argument' is what is returned from the Ruckus AP as the output when attempting to ssh to it. It is not an exception thrown from Paramiko. Looking through the logs of the AP I see "ruckus (rkscli): Invalid argument received from client" which makes me think that Paramiko is passing something to the AP that it does not like – scooter_118 Jan 08 '21 at 14:51

1 Answers1

0

Ok so with some testing of other SSH methods using Paramiko I was able to get the 'invoke_shell()' option to actually work for me in order to log in. It seems that there is an argument passed to the AP during the SSH connection when using 'ssh.exec_command' causing it to fail. I also had to add a time.sleep timer in-between all commands in order for the AP top have time to recognize them pass the output back to the script.