SSHLibrary 提示输入密码

SSHLibrary prompts for password

我的测试套件有问题。我使用机器人框架,python.I 在 python 中创建了一个函数,它在远程 Linux 客户端中执行控制台命令。

def purge_default_dns(device_ip):
    ssh_val = "usr1@" + device_ip
    command = "ctool dns | grep -v \"grep\" | awk \'{print }\'"
    test = check_output(["ssh", ssh_val, "-p", "6022", command])

check_output()函数连接device_ip并执行命令。如果我尝试连接完全限定的域名(例如 my.domain.io),则会收到输入密码的提示(为空)。我按回车键,命令正常执行。出现密码提示时是否有任何参数通过例如 Enter? 我尝试了 ssh -e 切换,我不想更改 ssh 客户端,我只需要一个通用的解决方案。


例如,在下面的代码中使用 paramiko 库,我可以创建一个 paramiko SSHClient,它有一个密码参数并且不提示任何内容。虽然我现在不能使用 paramiko,但我需要 SSHLirary 的其他东西来解决这个问题。

def send_ssh_command(device_ip , command):
    hostname = device_ip
    password = ""
    username = "usr1"
    port = 6022
    try:
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.WarningPolicy())
        client.connect(hostname, port=port, username=username, password=password)
        stdin , stdout , stderr = client.exec_command(command)
        command_return_val = stdout.read()
    finally:
        client.close()
    return command_return_val

谢谢。

为了弄清楚这一点,您寻找的唯一解决方案是将命令行上的密码传递给默认 OS ssh 客户端,然后 not/cannot 安装任何库(paramiko 等)可以帮助您通过其他方式达到相同的结果?

我问这个,因为机器人框架的SSHLibrary provides this out of the box; you already have the python's solution with paramiko; and the general linux solution is to install the sshpass package,并用它来传递值:

sshpass -p "YOUR_PASS" ssh -usr1@my.domain.io:6022

因此,如果所有这些都不是一个选项,那么您有两个选择 - 要么在 SSH_ASKPASS 周围破解一些东西 - 这是 an article 的示例,要么使用 expect通过它 - 这个是我更喜欢的两个。

这是一个非常好的 SO answer,其中 expect 脚本包装器围绕 ssh。在您的方法中,您必须首先创建一个包含其内容的文件,在其上设置一个可执行标志,然后在 check_output() 中调用该文件,将密码 'ssh' 及其所有参数作为参数传递.

为什么你需要使用 python ,我在 robotframework 中使用以下代码:

[Arguments]    ${host}=${APP_SERVER}    ${username}=${APP_USERNAME}    ${password}=${APP_PASSWORD}
Open Connection    ${host}    timeout=2m
Login    ${username}    ${password}
${out}  ${err}  ${rc}=   Execute Command   cd ${PATH};ctool dns | grep -v \"grep\" | awk \'{print }\' *    return_stdout=True   return_stderr=True   return_rc=True
Should Be Equal   ${rc}  [=10=]