如何使用Python Fabric检测过期密码?

时间:2016-06-06 20:46:41

标签: python ssh fabric

使用以下代码,我无法让Fabric在登录时检测到过期的密码提示。会话没有超时,abort_on_prompts参数似乎没有触发。如何配置Fabric来检测这种状态?

from fabric.api import env, run, execute
from fabric import network
from fabric.context_managers import settings

def host_type():
    with settings(abort_on_prompts=True):
        print ("Using abort mode %(abort_on_prompts)s" % env)
        result = run('uname -s')
return result

if __name__ == '__main__':
    print ("Fabric v%(version)s" % env)
    env.user = 'myuser'
    env.password = 'user67user'
    env.hosts = ['10.254.254.143']
    host_types = execute(host_type)

执行此脚本会导致挂起的脚本,如下所示:


    Fabric v1.11.1.post1
    [10.254.254.143] Executing task 'host_type'
    Using abort mode True
    [10.254.254.143] run: uname -s
    [10.254.254.143] out: WARNING: Your password has expired.
    [10.254.254.143] out: You must change your password now and login again!
    [10.254.254.143] out: Changing password for myuser.
    [10.254.254.143] out: (current) UNIX password: 

1 个答案:

答案 0 :(得分:0)

Fabric包含一种回答提示问题的方法。

您可以在prompts上使用with settings字典。在字典中,每个键都是您要回答的标准输出中的问题,值是您的答案。

所以在你的例子中:

from fabric.api import env, run, execute
from fabric import network
from fabric.context_managers import settings

def host_type():
    with settings(prompts={"(current) UNIX password": "new_password"}):
        result = run('uname -s')
return result

if __name__ == '__main__':
    print ("Fabric v%(version)s" % env)
    env.user = 'myuser'
    env.password = 'user67user'
    env.hosts = ['10.254.254.143']
    host_types = execute(host_type)