From: Tim Arnold on
Hi, I'm writing a script to capture a command on the commandline and run it
on a remote server.
I guess I don't understand subprocess because the code below exec's the
user's .cshrc file even though by default shell=False in the Popen call.

Here's the code. I put a line in my .cshrc file: echo 'testing' which
appears when I run this script on the remote host.
------------------------
import os,sys,subprocess,shlex

def main():
if action:
action.insert(0,'rsh my_remotehost')
p = subprocess.Popen(shlex.split(' '.join(action)))
p.wait()

if __name__ == '__main__':
action = sys.argv[1:] or list()
main()
------------------------

Since the shell is executing in the child process anyway, is the only
difference when using shell=True is that environment variables can be
expanded in the command to be executed?

thanks,
--Tim Arnold


From: Gabriel Genellina on
En Wed, 21 Oct 2009 12:24:37 -0300, Tim Arnold <tim.arnold(a)sas.com>
escribi�:

> Hi, I'm writing a script to capture a command on the commandline and run
> it
> on a remote server.
> I guess I don't understand subprocess because the code below exec's the
> user's .cshrc file even though by default shell=False in the Popen call.

Do you mean it execs the .cshrc file in your *local* system or the
*remote* one?
Popen controls what happens on the local system only.

> action.insert(0,'rsh my_remotehost')
> p = subprocess.Popen(shlex.split(' '.join(action)))
> p.wait()
>
> Since the shell is executing in the child process anyway, is the only
> difference when using shell=True is that environment variables can be
> expanded in the command to be executed?

Note that in this case, "the child process" is rsh on the local system.
Popen has no control over what happens once rsh starts.

--
Gabriel Genellina

From: Nobody on
On Wed, 21 Oct 2009 11:24:37 -0400, Tim Arnold wrote:

> Hi, I'm writing a script to capture a command on the commandline and run it
> on a remote server.
> I guess I don't understand subprocess because the code below exec's the
> user's .cshrc file even though by default shell=False in the Popen call.

Using shell=True causes Popen to explicitly invoke the command via a
shell, i.e. "/bin/sh -c <command>" on Unix or "cmd /c <command>" on
Windows. If you use shell=False (the default), it uses fork()+exec() or
CreateProcess().

If the command which you are running ends up invoking a shell (as rsh
probably does), that's nothing to do with Popen.

In particular, if csh is being invoked, that isn't Popen's doing. It will
use either /bin/sh on Unix or %COMSPEC% (which is typically cmd.exe) on
Windows.

> Since the shell is executing in the child process anyway, is the only
> difference when using shell=True is that environment variables can be
> expanded in the command to be executed?

The difference is that the shell gets involved, so you can use environment
variables, backticks, redirections, pipelines, shell-builtins etc. It also
means that you need to quote or escape spaces and shell metacharacters
within arguments if you want them to be treated literally.

From: Tim Arnold on
"Gabriel Genellina" <gagsl-py2(a)yahoo.com.ar> wrote in message
news:mailman.1840.1256202325.2807.python-list(a)python.org...
> En Wed, 21 Oct 2009 12:24:37 -0300, Tim Arnold <tim.arnold(a)sas.com>
> escribi�:
>
>> Hi, I'm writing a script to capture a command on the commandline and run
>> it
>> on a remote server.
>> I guess I don't understand subprocess because the code below exec's the
>> user's .cshrc file even though by default shell=False in the Popen call.
>
> Do you mean it execs the .cshrc file in your *local* system or the
> *remote* one?
> Popen controls what happens on the local system only.
>
>> action.insert(0,'rsh my_remotehost')
>> p = subprocess.Popen(shlex.split(' '.join(action)))
>> p.wait()
>>
>> Since the shell is executing in the child process anyway, is the only
>> difference when using shell=True is that environment variables can be
>> expanded in the command to be executed?
>
> Note that in this case, "the child process" is rsh on the local system.
> Popen has no control over what happens once rsh starts.
>
> --
> Gabriel Genellina

Thanks, I see my mistake now. Arggg, I keep forgetting that one.
thanks,
--Tim Arnold


 | 
Pages: 1
Prev: umlauts
Next: unicode and dbf files