让python在运行csh脚本时输入密码

发布于 2024-07-07 09:42:26 字数 206 浏览 9 评论 0原文

我正在编写一个在 Solaris 10 中执行 csh 脚本的 python 脚本。csh 脚本提示用户输入 root 密码(我知道),但我不确定如何使 python 脚本使用密码回答提示。 这可能吗? 这是我用来执行 csh 脚本的内容:

import commands

commands.getoutput('server stop')

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:

import commands

commands.getoutput('server stop')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

入怼 2024-07-14 09:42:27

查看 pexpect 模块。 它旨在处理交互式程序,这似乎就是您的情况。

哦,请记住,在 shell 或 python 脚本中硬编码 root 的密码可能是一个安全漏洞:D

Have a look at the pexpect module. It is designed to deal with interactive programs, which seems to be your case.

Oh, and remember that hard-encoding root's password in a shell or python script is potentially a security hole :D

装纯掩盖桑 2024-07-14 09:42:27

使用子进程。 调用 Popen() 创建进程并使用communicate() 向其发送文本。 抱歉,忘记包含 PIPE。

from subprocess import Popen, PIPE

proc = Popen(['server', 'stop'], stdin=PIPE)

proc.communicate('password')

您最好避免使用密码并尝试 sudo 和 sudoers 等方案。 其他地方提到的 Pexpect 不是标准库的一部分。

Use subprocess. Call Popen() to create your process and use communicate() to send it text. Sorry, forgot to include the 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.

狼亦尘 2024-07-14 09:42:27
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)

成功了! 期待规则!

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!

人间不值得 2024-07-14 09:42:27

对于喜欢使用标准库的人来说,在 proc.communicate() 中添加 input= 使其运行。

from subprocess import Popen, PIPE
proc = Popen(['server', 'stop'], stdin=PIPE)
proc.communicate(input='password')

Add input= in proc.communicate() make it run, for guys who like to use standard lib.

from subprocess import Popen, PIPE
proc = Popen(['server', 'stop'], stdin=PIPE)
proc.communicate(input='password')
谎言 2024-07-14 09:42:27

应该能够将其作为参数传递。 就像是:

commands.getoutput('server stop -p password')

Should be able to pass it as a parameter. something like:

commands.getoutput('server stop -p password')
金兰素衣 2024-07-14 09:42:27

这似乎效果更好:

import popen2

(stdout, stdin) = popen2.popen2('server stop')

stdin.write("password")

但还不是 100%。 即使“password”是正确的密码,当它尝试 su 到 root 时,我仍然从 csh 脚本中得到 su: 抱歉。

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.

离笑几人歌 2024-07-14 09:42:27

为了避免必须回答 python 脚本中的密码问题,我将以 root 身份运行该脚本。 这个问题仍然没有答案,但我想我现在就这样做。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文