【问题标题】:Unable to SSH to Ruckus AP using Paramiko [duplicate]无法使用 Paramiko SSH 到 Ruckus AP [重复]
【发布时间】:2021-01-07 22:25:47
【问题描述】:

我正在努力自动化脚本以登录 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)

【问题讨论】:

  • 'Invalid Argument' 是 Ruckus AP 在尝试 ssh 时作为输出返回的内容。这不是 Paramiko 抛出的异常。查看 AP 的日志,我看到“ruckus (rkscli): Invalid argument received from client”,这让我认为 Paramiko 正在向 AP 传递一些它不喜欢的东西

标签: python paramiko ruckus-api


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2020-02-10
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2018-02-25
    相关资源
    最近更新 更多