我的測試套件有問題。我使用機器人框架和python。我在python中創建了一個函數,該函數在遠程Linux客戶端中執行控制臺命令。def purge_default_dns(device_ip): ssh_val = "usr1@" + device_ip command = "ctool dns | grep -v \"grep\" | awk \'{print $2}\'" test = check_output(["ssh", ssh_val, "-p", "6022", command])該check_output()功能連接device_ip并執行命令。如果我嘗試使用完全限定的域名(例如my.domain.io)進行連接,則會提示輸入密碼(該密碼為空)。我按Enter鍵,命令會定期執行。是否有任何參數傳遞,例如出現密碼提示時輸入Enter?我嘗試過ssh -eswitch,我不想更改ssh client,我只需要一個通用的解決方案。例如,在下面的代碼中使用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
2 回答

慕斯709654
TA貢獻1840條經驗 獲得超5個贊
為什么需要使用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 $2}\' * return_stdout=True return_stderr=True return_rc=True
Should Be Equal ${rc} ${0}
添加回答
舉報
0/150
提交
取消