【发布时间】:2018-07-06 05:18:39
【问题描述】:
在重新映像工作站后,我正在使用 python 脚本来管理 ssh 指纹问题。
我尝试连接,如果我收到“远程主机标识已更改!”错误,然后脚本删除旧指纹,扫描新指纹并添加它。
这一切都很好,直到我收到这样的消息:
Warning: the ECDSA host key for 'workstation-1-s' differs from the key for the IP address '192.168.1.132'
Offending key for IP in /home/me/.ssh/known_hosts:16
Matching host key in /home/me/.ssh/known_hosts:60
Are you sure you want to continue connecting (yes/no)?
脚本在继续并删除有问题的键之前等待用户输入。
我怎样才能让脚本通过,或者输入“否”以便脚本可以继续其指纹修复工作?
下面是相关方法:
def ssh_fingerprint_changed(node):
"""
Checks if a node's ssh fingerprint has changed or an old key is found, which can occur when a node is reimaged.
It does this by attempting to connect via ssh and inspecting stdout for an error message.
:param node: the ip or hostname of the node
:return: True if the node's fingerprint doesn't match the client's records. Else False.
"""
cmd = ["ssh", "-q", ADMIN_USER + "@" + node, "exit"]
completed = subprocess.run(cmd, stdout=subprocess.PIPE, universal_newlines=True)
if completed.stdout.find("REMOTE HOST IDENTIFICATION HAS CHANGED!") == -1:
print("REMOTE HOST IDENTIFICATION HAS CHANGED!")
return True
elif completed.stdout.find("Offending key") == -1:
print("Offending key found.") # need to type "no" before this prints
return True
return False
【问题讨论】:
-
您需要
Popen重定向标准输入并在遇到消息时提供no。请注意,我已经破解了我的plink版本以摆脱一些互动内容。但这并不安全。 -
你能举一个
Popen的例子吗?
标签: python ssh subprocess