Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

github - Using the ssh agent inside a python script

I'm pulling and pushing to a github repository with a python script. For the github repository, I need to use a ssh key.

If I do this manually before running the script:

eval $(ssh-agent -s)
ssh-add ~/.ssh/myprivkey

everything works fine, the script works. But, after a while, apparently the key expires, and I have to run those 2 cmds again

The thing is, if I do that inside the python script, with os.system(cmd), it doesn't work, it only works if I do that manually

I know this must be a messy way to use the ssh agent, but I honestly don't know how it works, and I just want the script to work, that's all

The script runs once an hour, just in case

question from:https://stackoverflow.com/questions/65848251/using-the-ssh-agent-inside-a-python-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While the normal approach would be to run your Python script in a shell where the ssh agent is already running, you can also consider an alternative approach with sshify.py

# This utility will execute the given command (by default, your shell)
# in a subshell, with an ssh-agent process running and your
# private key added to it. When the subshell exits, the ssh-agent
# process is killed.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...