简体   繁体   English

从python运行nohup和eval

[英]Run nohup and eval from python

I need run two commands (from shell looks like) 我需要运行两个命令(从shell看起来像)

$ eval `ssh-agent -s`
Agent pid 8631
$ ssh-add ~/.ssh/user

Note : The key is in /home/user/.ssh/user and is correct 注意 :密钥在/home/user/.ssh/user中,并且是正确的

Run is as bash script works as charm: 运行就像bash脚本一样具有魅力:

#!/bin/bash
nohup `eval `ssh-agent -s`` >/dev/null 2>&1 &
ssh-add ~/.ssh/$user

but from python I can't get working, ssh-agent is started but ssh-add returns: 但是从python我无法正常工作, ssh-agent已启动,但ssh-add返回:
Could not open a connection to your authentication agent. 无法打开与身份验证代理的连接。

import os
os.system('nohup \'eval `ssh-agent -s`\' &')
os.system('ssh-add /home/user/.ssh/user')

I've tried: 我试过了:

import os
system('nohup sh -c \'eval `ssh-agent -s`\' &')

same problem ssh-agent started, but somehow I can't connected 相同的问题ssh-agent启动了,但是以某种方式我无法连接

What os.system does it to launch a different shell for each command. os.system作用是为每个命令启动不同的shell。 And what eval `ssh-agent -s` does is to launch a new ssh-agent and store in current shell some environment variables needed to communicate with it. eval `ssh-agent -s`作用是启动一个新的ssh-agent并在当前shell中存储一些与其通信所需的环境变量。 Finally, ssh-add uses that variables to access the ssh-agent agent to add a new key to it. 最后, ssh-add使用该变量访问ssh-agent代理以向其中添加新密钥。

So you need to execute both commands on the same shell. 因此,您需要在同一外壳上执行这两个命令。 You can do this with a unique call to os.system calling both commands sequentially: 您可以通过对os.system的唯一调用来依次执行以下两个命令:

os.system("eval `ssh-agent -s`; ssh-add /home/user/.ssh/user")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM