0

我正在运行 Fabric 1.6.0(paramiko 1.10.1)。我有以下脚本:

from fabric.api import env, output, run, sudo

user='your-user'
host='your-server'
port='your-port'
command = 'ps -ef'

output['running']  = False     # Avoid fabric to output what it is doing behind the scenes
output['stdout']   = False     # Do not show stdout
output['stderr']   = False     # Do not show stderr
output['status']   = False     # Prevent fabric from using print in some situations (at least in disconnect_all)
output['warnings'] = False     # Avoid fabric from showing messages about failed commands

def run_it(command, user, host, port, keyfile):
    env.host_string = "%s@%s:%s" % (user, host, port)
    env.key_filename = keyfile
    try:
        res = run(command, pty=False, shell=True)
        print "SUCCESS: return_code=%s" % (return_code)
    except Exception, e:
        print "ERROR  : %s" % (e)
        stdout, return_code =  None, None
    return stdout, return_code

run_it(command, user, host, port, '/bad/keyfile')
run_it(command, user, host, port, '/home/gonvaled/.ssh/id_rsa')
run_it(command, user, host, port, '/bad/keyfile')

这输出:

ERROR  : [Errno 2] No such file or directory: '/bad/keyfile'
SUCCESS: return_code=0
SUCCESS: return_code=0

但我期望:

ERROR  : [Errno 2] No such file or directory: '/bad/keyfile'
SUCCESS: return_code=0
ERROR  : [Errno 2] No such file or directory: '/bad/keyfile'

为什么会这样?似乎很好的密钥文件被记住了?为什么?这很烦人,因为它表明我无法即时设置密钥文件,所以我不确定正在使用哪个:我设置的第一个,我设置的第二个?选择它的标准是什么?有多少人记得?...

我使用fabric 作为ssh 库(不在fabfiles 中),所以我用不同的参数调用它。我依靠env将这些参数传递给fabric。这大部分工作正常,但key_filename似乎是一个例外。

4

2 回答 2

1

查看代码,我猜这是因为连接一旦成功就会被缓存。这意味着将记住第一次成功的尝试,并且不会尝试新的密钥文件。

于 2013-05-22T15:21:49.123 回答
0

我认为这是一个 ssh 问题,而不是结构问题。在第二次尝试连接成功后,密钥将存储在 中~/.ssh/known_hosts,在第三次尝试时,它再次使用存储的密钥进行连接。我会尝试设置这两个选项之一,看看它是否再次发生:

env.no_keys = True
env.use_ssh_config = False
于 2013-05-22T15:25:41.183 回答