Python Fabric:abort_on_prompts不起作用

时间:2015-08-03 14:11:30

标签: python fabric ssh-keys

我在与Fabric的SSH连接期间尝试跳过提示。像Python Fabric: Skip logins needing passwords一样。我在文档中读到abort_on_prompts选项应该这样做。但我无法使其发挥作用。

#!/usr/bin/python

from fabric.api import *

env.abort_on_prompts=True

def remote_cmd(server_name):
  with settings(hide('output','running','warnings'),
                host_string=server_name,
                user = 'john',
                key_filename = '/home/john/id_rsa',
                warn_only=True):
  return run('ls /data/')


server_name = 'server01'
ls_result = remote_cmd(server_name)

此代码一直询问我server01的密码(因为它没有公钥),而我想跳过它。

2 个答案:

答案 0 :(得分:2)

为了使用python fabric.api env.abort_on_prompts = True 并管理中止事件,必须将它与 try / except SystemExit <一起使用/ strong>声明。下面是一个简单的示例 abort_on_promt_test.py ,在您的localhost中测试它,定义执行的本地角色。

from fabric.api import settings, env, run
from termcolor import colored

env.roledefs = {
    'local': ['localhost'],
}


def command(cmd):
    """
Run a command in the host/s
    :param cmd: bash command to be executed
     eg: fab -R local command:"hostname"
     eg: fab -R local command:"ls -ltra"
    """

    env.abort_on_prompts = True
    try:
        with settings(warn_only=False):
            run(cmd)
    except SystemExit:
        print colored('===============================================', 'red')
        print colored('HOST: ' + env.host_string + ' aborted on prompt', 'red')
        print colored('===============================================', 'red')

这是测试执行输出

delivery@delivery-E5450$ fab -f abort_on_promt_test.py -R local command:"hostname"
[localhost] Executing task 'command'
[localhost] run: hostname

Fatal error: Needed to prompt for a connection or sudo password (host: localhost), but abort-on-prompts was set to True

Aborting.
===============================================
HOST: localhost aborted on prompt
===============================================

Done.

答案 1 :(得分:1)

感谢FunkySayu,我终于找到了解决方案,出了什么问题。 我在Debian 6上使用了Python 2.6.6和Fabric 0.9。 所以我在Debian 8上用Python 2.7.9和Fabric 1.10试了一下它工作得很好!

abort_on_prompts 退出脚本,但我想跳过它。这是我找到的解决方案:

#!/usr/bin/python

from fabric.api import *

env.abort_on_prompts=True

def remote_cmd(server_name):
  with settings(hide('output','running','warnings'),
            host_string=server_name,
            user = 'john',
            key_filename = '/home/john/id_rsa',
            warn_only=True):
    return run('ls /data/')


servers = (('server01',), ('server02',))
for row in servers:
  server_name = row[0]

  print "Connection to ", server_name

  try:
    result_ls = remote_cmd(server_name)
    print result_ls
  except SystemExit:
    print server_name," doesn't have the key"

在此示例中, server01 在authorized_key文件中没有公钥。但没有坏处,脚本将继续,打印消息,然后在 server02 上运行命令。 我希望很清楚:)

me@myserver:~$ ./test_fabric.py
Connection to  server01

Fatal error: Needed to prompt for a connection or sudo password (host: server01), but abort-on-prompts was set to True

Aborting.
server01  doesn't have the key

Connection to server02
[we see the results of ls command]