【问题标题】:Unable to truly background an SSH tunnel using Fabric and nohup无法使用 Fabric 和 nohup 真正为 SSH 隧道设置后台
【发布时间】:2012-01-09 05:51:24
【问题描述】:

我似乎无法让 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 挂起)。我很确定它没有正确分离,当 run 函数返回时,隧道进程立即死亡。

但是为什么呢?

这也发生在我代码的另一部分中的 python 命令中。

【问题讨论】:

  • 为什么使用nohup?如果你问我,那是多余的。
  • 嗯,您是否尝试过使用 -T 禁用 ssh 的 tty 分配,以及使用 pty=False 禁用任务的 tty 分配?
  • @sehe,为什么它是多余的?我还应该如何分离进程,以便 Fabric 可以在隧道不死的情况下返回?
  • @Edwardr 我不喜欢 Fabric,但是 ssh 在 -N 上作为背景,这足以让我能够退出 shell、注销、终止 XServer 而不会影响在其中启动的隧道时尚。
  • @favoretti 我尝试了这两个但没有骰子。谢谢。

标签: python bash ssh fabric nohup


【解决方案1】:

因此,经过一番争论之后,无论出于何种原因,我的设置似乎都无法做到这一点(默认 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()

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 2021-06-06
    • 2014-10-13
    • 1970-01-01
    • 2014-09-25
    • 2011-10-09
    • 2014-04-16
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多