使用 Python 和 subprocces.call 执行脚本时出现问题,但在 Bash 中仍然有效

发布于 2024-09-03 03:22:24 字数 1651 浏览 2 评论 0原文

这是我第一次在这里寻求一些帮助,因为我更像是一个 ServerFault 人员。

我正在用 Python 编写一些脚本,到目前为止我一直很喜欢这门语言,但我遇到了一个小问题,它使我的脚本无法工作。

这是有问题的代码行:

subprocess.call('xen-create-image --hostname '+nom+' --memory '+memory+' --partitions=/root/scripts/part.tmp --ip '+ip+' --netmask '+netmask+' --gateway '+gateway+' --passwd',shell=True)

我用 os.popen 尝试过同样的事情。所有变量均已正确设置。

当我在常规 Linux shell 中执行相关命令时,它工作得很好,但是当我使用 Python 脚本执行它时,我会收到奇怪的错误。我什至用 print 函数替换了 subprocess.call() 以确保我使用的是命令的准确输出。

我查看了 shell 的环境变量,但它们几乎相同...我将发布我收到的错误,但我不确定它与我的问题相关。

在 /usr/share/perl5/Config/IniFiles.pm 第 614 行处使用未初始化值 $lines[0] 进行替换 (s///)。 在 /usr/share/perl5/Config/IniFiles.pm 第 628 行的模式匹配 (m//) 中使用未初始化值 $_。

我不是 Python 专家,所以我很可能在这里遗漏了一些东西。

预先感谢您的帮助,

Antoine


编辑

按照 miax 的建议,我停止使用 shell=True。相反,我查看了 subprocess 的 Python 文档,并使用了以下代码:

cmd = 'xen-create-image --hostname '+nom+' --memory '+memory+' --partitions=/root/scripts/part.tmp --ip '+ip+' --netmask '+netmask+' --gateway '+gateway+' --passwd'
args = shlex.split(cmd)
subprocess.call(args)

可悲的是,它没有改变任何东西...


EDIT2

我已经使用了 miax 给出的提示,但我仍然收到上述错误...这是我使用的代码。

cmd = ['xen-create-image', '--hostname', nom, '--memory', memory, '--partitions=/root/scripts/part.tmp', '--ip', ip, '--netmask', netmask, '--gateway', gateway, '--passwd']
subprocess.call(cmd)

这真的很奇怪...当我在常规 shell 中运行它时,确切的命令工作正常...

For the first time, I am asking a little bit of help over here as I am more of a ServerFault person.

I am doing some scripting in Python and I've been loving the language so far yet I have this little problem which is keeping my script from working.

Here is the code line in question :

subprocess.call('xen-create-image --hostname '+nom+' --memory '+memory+' --partitions=/root/scripts/part.tmp --ip '+ip+' --netmask '+netmask+' --gateway '+gateway+' --passwd',shell=True)

I have tried the same thing with os.popen. All the variables are correctly set.

When I execute the command in question in my regular Linux shell, it works perfectly fine but when I execute it using my Python scripts, I get bizarre errors. I even replaced subprocess.call() by the print function to make sure I am using the exact output of the command.

I went looking into environment variables of my shell but they are pretty much the same... I'll post the error I am getting but I'm not sure it's relevant to my problem.

Use of uninitialized value $lines[0] in substitution (s///) at /usr/share/perl5/Config/IniFiles.pm line 614.
Use of uninitialized value $_ in pattern match (m//) at /usr/share/perl5/Config/IniFiles.pm line 628.

I am not a Python expert so I'm most likely missing something here.

Thank you in advance for your help,

Antoine


EDIT

Following miax's advice, I stopped using shell=True. Instead I took a look at the Python documentation for subprocess and used the following piece of code :

cmd = 'xen-create-image --hostname '+nom+' --memory '+memory+' --partitions=/root/scripts/part.tmp --ip '+ip+' --netmask '+netmask+' --gateway '+gateway+' --passwd'
args = shlex.split(cmd)
subprocess.call(args)

Sadly, it doesn't change anything...


EDIT2

I have used the tip given by miax but I still get the above error... Here is the code that I have used.

cmd = ['xen-create-image', '--hostname', nom, '--memory', memory, '--partitions=/root/scripts/part.tmp', '--ip', ip, '--netmask', netmask, '--gateway', gateway, '--passwd']
subprocess.call(cmd)

This is really strange... The exact command works fine when I run it in the regular shell...

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

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

发布评论

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

评论(4

别理我 2024-09-10 03:22:24

您(在大多数情况下)不想将子进程与 shell=True 一起使用。
将参数列表传递给命令。这

  • 更安全:想象一下用户设法通过 foo; rm -rf /; echo 作为一些值。
  • 更可靠:想象其中一个字符串包含 $ 或其他内容 - 它将由 shell 扩展并替换为该环境变量的内容。

在不知道您的代码和 xen-create-image 的情况下,我认为这就是您的问题的原因。

PS:一定要检查命令的退出代码是否为零,如果不是零,请采取适当的措施。 (如果您确定它始终为零,请使用 check_call,如果不为零,则会引发;这样,如果失败,您至少会有一个定义的行为。)

You (in most cases) don't want to use subprocess with shell=True.
Pass it a list of arguments to the command. That is

  • more secure: Imagine a user manages to pass foo; rm -rf /; echo as some of the values.
  • more reliable: Imagine one of the strings contains a $ or something – it will be expanded by the shell and replaced by the content of that environment variable.

Without knowing your code and xen-create-image, I assume that is the cause of your problem.

PS: Be sure to look if the exit code of the command is zero, and act appropriately if not. (If you are certain that it will always be zero, use check_call, which raises if it does not; that way you'll at least have a defined behavior if it fails.)

分開簡單 2024-09-10 03:22:24

xen-create-image 脚本是否以 hashbang 开头?也就是说,第一行是这样的吗

#!/bin/sh

?这是需要检查的一件事。另一个是您可以尝试将命令调用为:

cmd = ['/bin/sh', '-c', 'xen-create-image --hostname %s --memory %s --partitions=/root/scripts/part.tmp --ip %s --netmask %s --gateway %s --passwd' % (nom, memory, ip, netmask, gateway)]
subprocess.call(cmd, shell=False)

您可能需要打印 cmd 来验证这是您打算运行的命令(即检查替换)。

Does the xen-create-image script start with a hashbang? That is, is the first line something like

#!/bin/sh

? That is one thing to check. Another is that you can try to call your command as:

cmd = ['/bin/sh', '-c', 'xen-create-image --hostname %s --memory %s --partitions=/root/scripts/part.tmp --ip %s --netmask %s --gateway %s --passwd' % (nom, memory, ip, netmask, gateway)]
subprocess.call(cmd, shell=False)

You might want to print cmd to verify this is the command you intend to run (i.e. check the substitutions).

真心难拥有 2024-09-10 03:22:24

在失败的 Edit2 示例中,您认为xen-create-image 提供了以下选项:

  • --hostname
  • --memory
  • --partitions=...
  • 等等

...但您实际上指定了以下选项:

  • --hostname >空间
  • 空间--内存空间
  • 空间--partitions= ...

你有这一行:

cmd = ['xen-create-image', '--hostname ', nom, ' --memory ', memory, ' --partitions=/root/scripts/part.tmp', ' --ip ', ip, ' --netmask ', netmask, ' --gateway ', gateway, ' --passwd']

但你需要去掉多余的空格:

cmd = ['xen-create-image', '--hostname', nom, '--memory', memory, '--partitions=/root/scripts/part.tmp', '--ip', ip, '--netmask', netmask, '--gateway', gateway, '--passwd']

In your Edit2 example which is failing, you think you are giving the following options to xen-create-image:

  • --hostname
  • --memory
  • --partitions=...
  • etc

... but you are actually specifying the following options:

  • --hostnamespace
  • space--memoryspace
  • space--partitions=...
  • etc

You have this line:

cmd = ['xen-create-image', '--hostname ', nom, ' --memory ', memory, ' --partitions=/root/scripts/part.tmp', ' --ip ', ip, ' --netmask ', netmask, ' --gateway ', gateway, ' --passwd']

But you need to take out the extra spaces:

cmd = ['xen-create-image', '--hostname', nom, '--memory', memory, '--partitions=/root/scripts/part.tmp', '--ip', ip, '--netmask', netmask, '--gateway', gateway, '--passwd']
人间☆小暴躁 2024-09-10 03:22:24

您需要打印您正在使用的命令:

cmd = 'xen-create-image --hostname '+nom+' --memory '+memory+' --partitions=/root/scripts/part.tmp --ip '+ip+' --netmask '+netmask+' --gateway '+gateway+' --passwd'
print "COMMAND:", cmd

然后将该命令粘贴到您的 shell 中以确保它完全相同。

You need to print the command you are using:

cmd = 'xen-create-image --hostname '+nom+' --memory '+memory+' --partitions=/root/scripts/part.tmp --ip '+ip+' --netmask '+netmask+' --gateway '+gateway+' --passwd'
print "COMMAND:", cmd

And then paste the command into your shell to make sure it is exactly the same.

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