Discussion:
how to run shell command like "<<EOT .... EOT"
叶佑群
2012-09-28 07:45:58 UTC
Permalink
Hi, all,

I have the shell command like this:

sfdisk -uM /dev/sdb << EOT
,1000,83
,,83
EOT


I have tried subprocess.Popen, pexpect.spawn and os.popen, but none
of these works, but when I type this shell command in shell, it is works
fine. I wonder how to emulate this type of behavior in python , and if
someone can figure out the reason why?

The sample code of subprocess.Popen is:

command = ["sfdisk", "-uM", target, "<<EOT", "\r\n",
",", 1000, ",", "83", "\r\n",
",", ",", "83", "\r\n", "EOT", "\r\n"]

pobj = subprocess.Popen (command, bufsize=1, \
stderr=subprocess.PIPE, stdout=subprocess.PIPE)

res = pobj.stderr.readline ()
if res is not None and pobj.returncode != 0:
observer.ShowProgress (u"??? %s ?????" % target)
return False

and pexpect code is:

child = pexpect.spawn ("sfdisk -uM /dev/sdb <<EOT")
child.sendline (....)
child.sendline (....)
child.sendline (....)

and os.popen like this:

os.popen ("sfdisk -uM /dev/sdb <<EOT\n,1000,83\n,,83\nEOT\n")

I tried "\r\n", and it doesn't work either.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120928/6c064dde/attachment-0001.html>
Kushal Kumaran
2012-09-28 08:16:10 UTC
Permalink
Post by 叶佑群
Hi, all,
sfdisk -uM /dev/sdb << EOT
,1000,83
,,83
EOT
I have tried subprocess.Popen, pexpect.spawn and os.popen, but none of
these works, but when I type this shell command in shell, it is works fine.
I wonder how to emulate this type of behavior in python , and if someone can
figure out the reason why?
command = ["sfdisk", "-uM", target, "<<EOT", "\r\n",
",", 1000, ",", "83", "\r\n",
",", ",", "83", "\r\n", "EOT", "\r\n"]
pobj = subprocess.Popen (command, bufsize=1, \
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
res = pobj.stderr.readline ()
observer.ShowProgress (u"??? %s ?????" % target)
return False
The "<<EOT" syntax (called a here-document) just provides input to the
command. If you use the communicate method, you can provide input as
an argument:

command = ["sfdisk", "-uM", target ]
instructions = """
,1000,83
,,83
"""
pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errors) = pobj.communicate(instructions)
Post by 叶佑群
child = pexpect.spawn ("sfdisk -uM /dev/sdb <<EOT")
child.sendline (....)
child.sendline (....)
child.sendline (....)
os.popen ("sfdisk -uM /dev/sdb <<EOT\n,1000,83\n,,83\nEOT\n")
I tried "\r\n", and it doesn't work either.
--
regards,
kushal
叶佑群
2012-09-29 00:48:03 UTC
Permalink
Post by Kushal Kumaran
Post by 叶佑群
Hi, all,
sfdisk -uM /dev/sdb<< EOT
,1000,83
,,83
EOT
I have tried subprocess.Popen, pexpect.spawn and os.popen, but none of
these works, but when I type this shell command in shell, it is works fine.
I wonder how to emulate this type of behavior in python , and if someone can
figure out the reason why?
command = ["sfdisk", "-uM", target, "<<EOT", "\r\n",
",", 1000, ",", "83", "\r\n",
",", ",", "83", "\r\n", "EOT", "\r\n"]
pobj = subprocess.Popen (command, bufsize=1, \
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
res = pobj.stderr.readline ()
observer.ShowProgress (u"??? %s ?????" % target)
return False
The "<<EOT" syntax (called a here-document) just provides input to the
command. If you use the communicate method, you can provide input as
command = ["sfdisk", "-uM", target ]
instructions = """
,1000,83
,,83
"""
pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errors) = pobj.communicate(instructions)
I tried this, but it is still not work.
Post by Kushal Kumaran
Post by 叶佑群
child = pexpect.spawn ("sfdisk -uM /dev/sdb<<EOT")
child.sendline (....)
child.sendline (....)
child.sendline (....)
os.popen ("sfdisk -uM /dev/sdb<<EOT\n,1000,83\n,,83\nEOT\n")
I tried "\r\n", and it doesn't work either.
Kushal Kumaran
2012-09-29 11:53:31 UTC
Permalink
Post by 叶佑群
Post by Kushal Kumaran
Post by 叶佑群
Hi, all,
sfdisk -uM /dev/sdb<< EOT
,1000,83
,,83
EOT
I have tried subprocess.Popen, pexpect.spawn and os.popen, but none of
these works, but when I type this shell command in shell, it is works fine.
I wonder how to emulate this type of behavior in python , and if someone can
figure out the reason why?
command = ["sfdisk", "-uM", target, "<<EOT", "\r\n",
",", 1000, ",", "83", "\r\n",
",", ",", "83", "\r\n", "EOT", "\r\n"]
pobj = subprocess.Popen (command, bufsize=1, \
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
res = pobj.stderr.readline ()
observer.ShowProgress (u"??? %s ?????" % target)
return False
The "<<EOT" syntax (called a here-document) just provides input to the
command. If you use the communicate method, you can provide input as
command = ["sfdisk", "-uM", target ]
instructions = """
,1000,83
,,83
"""
pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errors) = pobj.communicate(instructions)
I tried this, but it is still not work.
What do you mean by "not work"?

- If you get an exception, copy the entire traceback into an email

- If you do not get an exception, print out the value of the "errors"
variable to see why the command failed. You can also check
pobj.returncode for the exit status of the subprocess.

A possibility is that you have to replace "sfdisk" with the full path
to the binary, if it cannot be located on the PATH. So you will
replace it with "/usr/sbin/sfdisk", or "/sbin/sfdisk", or wherever the
file actually is.

<from your other email>
Post by 叶佑群
If I want to read the output line by line and not put all output to memory buffer in one
time, how to write the code?
You can read line by line by calling pobj.stdout.readline() and
pobj.stderr.readline(). You can send input to the process by calling
pobj.stdin.write(). If you manage this interaction "by hand", you
should not call communicate(). Also, you should be aware of the
problem mentioned in the subprocess documentation:

"Use communicate() rather than .stdin.write, .stdout.read or
.stderr.read to avoid deadlocks due to any of the other OS pipe
buffers filling up and blocking the child process."

Is there any reason why you need to read line-by-line? You could use
communicate(), and then call stdout.splitlines() to get a list of
lines, if that's all you need.
--
regards,
kushal
叶佑群
2012-10-08 00:53:46 UTC
Permalink
Post by Kushal Kumaran
Post by 叶佑群
Post by Kushal Kumaran
Post by 叶佑群
Hi, all,
sfdisk -uM /dev/sdb<< EOT
,1000,83
,,83
EOT
I have tried subprocess.Popen, pexpect.spawn and os.popen, but none of
these works, but when I type this shell command in shell, it is works fine.
I wonder how to emulate this type of behavior in python , and if someone can
figure out the reason why?
command = ["sfdisk", "-uM", target, "<<EOT", "\r\n",
",", 1000, ",", "83", "\r\n",
",", ",", "83", "\r\n", "EOT", "\r\n"]
pobj = subprocess.Popen (command, bufsize=1, \
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
res = pobj.stderr.readline ()
observer.ShowProgress (u"??? %s ?????" % target)
return False
The "<<EOT" syntax (called a here-document) just provides input to the
command. If you use the communicate method, you can provide input as
command = ["sfdisk", "-uM", target ]
instructions = """
,1000,83
,,83
"""
pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errors) = pobj.communicate(instructions)
I tried this, but it is still not work.
What do you mean by "not work"?
Sorry for replying so late, these days are long vocation in china.

If I type command in shell line by line, the command will run as
expected, but when I invoke the command in python, it is always failed.
Which is what I mean "not work".
Post by Kushal Kumaran
- If you get an exception, copy the entire traceback into an email
No exception occured.
Post by Kushal Kumaran
- If you do not get an exception, print out the value of the "errors"
variable to see why the command failed. You can also check
pobj.returncode for the exit status of the subprocess.
I solved this problem as below:

fop = os.popen ("sfdisk -uM %s <<EOT\n,%d,83\n,,83\nEOT\n" % (target, fps))

But when I run it with subprocess.Popen (), it doesn't work as os.popen
even use the code as:

command = ["sfdisk", "-uM", target ]
instructions = """
,1000,83
,,83
"""

pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errors) = pobj.communicate(instructions)

stderr.readline () always return the error message means "sfdisk won't accept more than one device parameter except -s or -l is specified", it seems that some parameters were incorrectly treated
Post by Kushal Kumaran
A possibility is that you have to replace "sfdisk" with the full path
to the binary, if it cannot be located on the PATH. So you will
replace it with "/usr/sbin/sfdisk", or "/sbin/sfdisk", or wherever the
file actually is.
<from your other email>
Post by 叶佑群
If I want to read the output line by line and not put all output to memory buffer in one
time, how to write the code?
You can read line by line by calling pobj.stdout.readline() and
pobj.stderr.readline(). You can send input to the process by calling
pobj.stdin.write(). If you manage this interaction "by hand", you
should not call communicate(). Also, you should be aware of the
"Use communicate() rather than .stdin.write, .stdout.read or
.stderr.read to avoid deadlocks due to any of the other OS pipe
buffers filling up and blocking the child process."
Is there any reason why you need to read line-by-line? You could use
communicate(), and then call stdout.splitlines() to get a list of
lines, if that's all you need.
叶佑群
2012-09-29 00:34:04 UTC
Permalink
Post by Kushal Kumaran
Post by 叶佑群
Hi, all,
sfdisk -uM /dev/sdb<< EOT
,1000,83
,,83
EOT
I have tried subprocess.Popen, pexpect.spawn and os.popen, but none of
these works, but when I type this shell command in shell, it is works fine.
I wonder how to emulate this type of behavior in python , and if someone can
figure out the reason why?
command = ["sfdisk", "-uM", target, "<<EOT", "\r\n",
",", 1000, ",", "83", "\r\n",
",", ",", "83", "\r\n", "EOT", "\r\n"]
pobj = subprocess.Popen (command, bufsize=1, \
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
res = pobj.stderr.readline ()
observer.ShowProgress (u"??? %s ?????" % target)
return False
The "<<EOT" syntax (called a here-document) just provides input to the
command. If you use the communicate method, you can provide input as
command = ["sfdisk", "-uM", target ]
instructions = """
,1000,83
,,83
"""
pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errors) = pobj.communicate(instructions)
If I want to read the output line by line and not put all output to
memory buffer in one time, how to write the code?
Post by Kushal Kumaran
Post by 叶佑群
child = pexpect.spawn ("sfdisk -uM /dev/sdb<<EOT")
child.sendline (....)
child.sendline (....)
child.sendline (....)
os.popen ("sfdisk -uM /dev/sdb<<EOT\n,1000,83\n,,83\nEOT\n")
I tried "\r\n", and it doesn't work either.
Loading...