python 运行csh脚本时让python输入密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/230845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make python enter password when running a csh script
提问by darrickc
I'm writing a python script that executes a csh script in Solaris 10. The csh script prompts the user for the root password (which I know) but I'm not sure how to make the python script answer the prompt with the password. Is this possible? Here is what I'm using to execute the csh script:
我正在编写一个在 Solaris 10 中执行 csh 脚本的 python 脚本。csh 脚本提示用户输入 root 密码(我知道),但我不确定如何让 python 脚本用密码回答提示。这可能吗?这是我用来执行 csh 脚本的内容:
import commands
commands.getoutput('server stop')
回答by Federico A. Ramponi
Have a look at the pexpectmodule. It is designed to deal with interactive programs, which seems to be your case.
看看pexpect模块。它旨在处理交互式程序,这似乎是您的情况。
Oh, and remember that hard-encoding root's password in a shell or python script is potentially a security hole :D
哦,请记住,在 shell 或 python 脚本中硬编码 root 的密码可能是一个安全漏洞:D
回答by Douglas Mayle
Use subprocess. Call Popen() to create your process and use communicate() to send it text. Sorry, forgot to include the PIPE..
使用子流程。调用 Popen() 来创建您的进程并使用communication() 向其发送文本。抱歉,忘了包括 PIPE..
from subprocess import Popen, PIPE
proc = Popen(['server', 'stop'], stdin=PIPE)
proc.communicate('password')
You would do better do avoid the password and try a scheme like sudo and sudoers. Pexpect, mentioned elsewhere, is not part of the standard library.
您最好避免使用密码并尝试使用 sudo 和 sudoers 之类的方案。在别处提到的 Pexpect 不是标准库的一部分。
回答by darrickc
import pexpect
child = pexpect.spawn('server stop')
child.expect_exact('Password:')
child.sendline('password')
print "Stopping the servers..."
index = child.expect_exact(['Server processes successfully stopped.', 'Server is not running...'], 60)
child.expect(pexpect.EOF)
Did the trick! Pexpect rules!
成功了!期待规则!
回答by darrickc
Add input=
in proc.communicate()
make it run, for guys who like to use standard lib.
添加input=
在proc.communicate()
使其运行,谁喜欢使用标准库的家伙。
from subprocess import Popen, PIPE
proc = Popen(['server', 'stop'], stdin=PIPE)
proc.communicate(input='password')
回答by darrickc
To avoid having to answer the Password question in the python script I'm just going to run the script as root. This question is still unanswered but I guess I'll just do it this way for now.
为避免在 python 脚本中回答密码问题,我将以 root 身份运行脚本。这个问题仍然没有答案,但我想我现在就这样做。
回答by darrickc
This seems to work better:
这似乎效果更好:
import popen2
(stdout, stdin) = popen2.popen2('server stop')
stdin.write("password")
But it's not 100% yet. Even though "password" is the correct password I'm still getting su: Sorry back from the csh script when it's trying to su to root.
但这还不是 100%。即使“密码”是正确的密码,我仍然收到 su: 抱歉,当它试图从 csh 脚本 su 到 root 时。
回答by Owen
Should be able to pass it as a parameter. something like:
应该能够将其作为参数传递。就像是:
commands.getoutput('server stop -p password')