无法使用Fabric和nohup真正背景SSH隧道

时间:2011-11-29 22:06:48

标签: python bash ssh fabric nohup

我似乎无法让Fabric在背景我使用nohup的过程中发挥出色。 。 。应该可以提供各种信息,包括herehere

def test():
h = 'xxxxx.compute-1.amazonaws.com'    
ports = [16646, 9090, 6666]

with settings(host_string = h):
    tun_s = "ssh  -o StrictHostKeyChecking=no -i ~/.ssh/kp.pem %s@%s " % (env.user, h)      

    for port in ports:
        p_forward = "-L %d:localhost:%d" % (port, port)
        tun_s = "%s %s" % (tun_s, p_forward)

    tun_s = "%s -N" % tun_s
    # create the tunnel. . .
    print "creating tunnel %s" % tun_s
    run("nohup '%s' >& /dev/null < /dev/null &" % tun_s)
    print "fin"

缩写输出:

ubuntu@domU-xxx:~/deploy$ fab test
executing on tunnel ssh  -o StrictHostKeyChecking=no -i ~/.ssh/kp.pem ubuntu@xxx  -L 16646:localhost:16646 -L 9090:localhost:9090 -L 6666:localhost:6666 -N
[xxx.compute-1.amazonaws.com] run: nohup 'ssh  -o StrictHostKeyChecking=no -i ~/.ssh/kp.pem ubuntu@xxx.compute-1.amazonaws.com  -L 16646:localhost:16646 -L 9090:localhost:9090 -L 6666:localhost:6666 -N' >& /dev/null < /dev/null &
fin

Done.
Disconnecting from xxxx

我知道隧道命令本身没有问题,因为如果我去除nohup的东西它工作正常(但显然Fabric挂起)。我很确定它没有正确分离,当运行函数返回时隧道进程立即死亡。

但为什么?

在我的代码的另一部分中使用python命令也会发生这种情况。

1 个答案:

答案 0 :(得分:0)

所以,经过多次争论后,无论出于何种原因我的设置都不可能(默认的Ubuntu安装在EC2实例上)。根据各种消息来源,我不知道为什么以及它似乎是可能的。

我通过使用Paramiko代替Fabric修复了我的特定问题,用于需要在后台运行的调用。以下是:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privkey = paramiko.RSAKey.from_private_key_file('xxx.pem')
ssh.connect('xxx.compute-1.amazonaws.com', username='ubuntu', pkey=privkey)
stdin, stdout, stderr = ssh.exec_command("nohup ssh  -f -o StrictHostKeyChecking=no -i     ~/.ssh/xxx.pem ubuntu@xxx.compute-1.amazonaws.com -L 16646:localhost:16646 -L -N >& /dev/null < /dev/null &")
ssh.close()