【发布时间】:2018-08-09 08:56:01
【问题描述】:
我正在循环访问多个主机(大约 400 个)以从系统中获取一些信息。他们中的大多数人都有 ssh 密钥,所以那里没有问题。只有少数人没有 ssh 密钥并返回密码。现在我想检测何时要求我输入密码,终止 ssh 进程并继续到下一个主机。
这是 ssh 部分的代码
def get_os_info(hostname, command, user=None):
remote = "%s" % hostname if user is None else "%s@%s" %(user, hostname)
ssh = subprocess.Popen(["ssh", remote, command],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print >>sys.stderr, "ERROR: %s" % error
return None
else:
return result
我无法使用 paramiko。
【问题讨论】:
标签: python ssh subprocess