1

I have a python script (myscript.py) that I am running on the linux server like below:

python myscript.py

In my script I am using the system call, example below:

os.system("./myprogram.pl -x 5 > results.out")

When I run myscript.py on the server whenever there is os.system call, the command call is visible on the server. Is there a way to hide all the command calls so that they are not displayed when they are called within the program (myscript.py)? (This is not about hiding the output resulted in the command calls within the program)

10
  • And why would that be needed at all? Inquiring minds want to know! Commented Jul 14, 2014 at 19:29
  • 4
    You could overwrite sys.__stdout__ or other such hacks, but I think the better solution would be to use the subprocess module - or even a library like invoke. Commented Jul 14, 2014 at 19:31
  • Ah, I suspect you mean that the stdout of the command is visible. I for a moment thought you wanted to hide the process from the OS process list. That was not clear. Commented Jul 14, 2014 at 19:36
  • The server is used by a large number of people and I would like to keep my work private and this includes that tools that I use. Commented Jul 14, 2014 at 19:36
  • @MartijnPieters - actually, I think he does want to keep the process command line secret. Commented Jul 14, 2014 at 19:44

3 Answers 3

0

You can use a subprocess pipe instead of the os.system command:

import subprocess;

NewPipeObject = subprocess.Popen( [ './myprogram.pl -x 5 > results.out' ],
                                  stdout= subprocess.PIPE,
                                  stderr= subprocess.PIPE );

or you can take advantage of the list format of the arguments for the subprocess pipe (it adds a space between each list element) and present them as the following (which will be easier to generalize):

import subprocess;

NewPipeObject = subprocess.Popen( [ './myprogram.pl',
                                    '-x',
                                    '5',
                                    '>',
                                    'results.out' ],
                                  stdout= subprocess.PIPE,
                                  stderr= subprocess.PIPE );

Either way you're suppressing the outputs from stdout and stderr into the pipe object (NewPipeObject).

Sign up to request clarification or add additional context in comments.

1 Comment

You actually can't pass [ './myprogram.pl -x 5 > results.out' ] to Popen when using shell=False, which is the default for Popen. You have to pass each it as a list: Popen(['./myprogram.pl', '-x', '5']) instead. Redirection of stdout to results.out would then be done using the stdout keyword argument, since using '>' for stdout redirection is a shell-only feature. The other option would be to use shell=True, in which case you would pass the full command as a string.
0

Use the subprocess module to create a subprocess so you wont see any ouput from the process unless you read from the PIPE directly.

process = subprocess.Popen(['perl', 'myprogram.pl'], stdout = subprocess.PIPE )

Comments

0

You mean, can you conceal the command from showing up in ps, top and other such tools? No.

On some systems, myprogram.pl can itself hide its command line from appearing in ps; for instance in perl:

$0 = 'new program name';

That edits the ps entry rather than removing it. Since it is set by the new process there is a race condition where someone might see it before it is changed; and it may be possible (certainly possible for the superuser) to get the same info in other ways. There is no secure way, nor is there intended to be, to conceal your command line entirely from other users.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.