0

我正在努力自动化脚本以登录 Ruckus AP(H510 Unleashed)并使用 Paramiko 从中收集信息。我已经使用相同的 Paramiko 脚本毫无问题地登录到各种其他设备(linux、HP/Aruba 等),但是当我尝试连接到 Ruckus AP 时,即使没有发送命令,我也会得到“无效参数”的输出我有内置的错误检查,可以检测无效密码等,所以不应该这样。我也可以从运行 python 脚本的同一台机器上进行 SSH,而不会出现问题,并且 SSH 会话中没有任何奇怪的标头。

有没有人有任何想法?下面的代码:

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)
4

1 回答 1

0

好的,所以通过使用 Paramiko 对其他 SSH 方法进行一些测试,我能够让“invoke_shell()”选项真正为我工作以便登录。似乎在 SSH 连接期间有一个参数传递给 AP 时使用 'ssh.exec_command' 导致它失败。我还必须在所有命令之间添加一个 time.sleep 计时器,以便 AP 顶部有时间识别它们将输出传递回脚本。

于 2021-01-08T15:14:00.207 回答