The problem with executing a script using Python and subprocces.call still works in Bash

For the first time, I ask for a little help, since I'm more of a ServerFault person.

I am writing scripts in Python and still love this language, but I have this little problem that makes my script not work.

Here is the line of code 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 tried the same with os.popen. All variables are set correctly.

When I execute this command in my regular Linux shell, it works fine, but when I run it using my Python scripts, I get strange errors. I even replaced subprocess.call () with a print function to make sure I use the exact output of the command.

I looked at the environment variables of my shell, but they are almost the same ... I will send the error that I get, but I'm not sure if this is relevant to my problem.

Using the uninitialized value of $ lines [0] in the substitution (s ///) in / usr / share / perl 5 / Config / IniFiles.pm line 614. Using the uninitialized value of $ _ according to the pattern (m //) on the page / usr / share / perl 5 / Config / IniFiles.pm line 628.

I am not a Python expert, so most likely something is missing here.

Thank you in advance for your help,

Antoine


EDIT

Following miax's advice, I stopped using shell = True. Instead, I looked at the Python documentation for subprocess and used the following code snippet:

 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) 

Unfortunately, this does not change anything ...


EDIT2

I used the hint given by miax, but I still get the above error ... Here is the code I 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 weird ... The exact command works fine when I run it in a regular shell ...

+4
source share
4 answers

Is xen-create-image script created using hashbang? That is, the first line is similar to

 #!/bin/sh 

? This is one thing to check. Another is that you can try calling your command like:

 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 type cmd to make sure that this is the command you intend to execute (i.e. check the substitutions).

0
source

You (in most cases) do not want to use a subprocess with shell=True . Pass it a list of command arguments. it

  • more secure: imagine that the user manages to pass foo; rm -rf /; echo foo; rm -rf /; echo foo; rm -rf /; echo as some of the meanings.
  • more reliable: suppose one of the lines contains $ or something else - it will be expanded by the shell and replaced with the contents of this environment variable.

Without knowing your code and xen-create-image , I assume this is causing your problem.

PS: Be sure to see if the command exit code is zero, and act accordingly if not. (If you are sure that it will always be zero, use check_call , which is raised if it is not, and you will at least have certain behavior if it is not executed.)

+2
source

In the Edit2 example, which does not work, you consider that you specify the following xen-create-image parameters:

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

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

  • --hostname space
  • space --memory space
  • 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 remove the extra spaces:

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

You need to print command you use:

 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.

0
source

Source: https://habr.com/ru/post/1310915/


All Articles