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)

sal_x_sal
  • 75
  • 1
  • 2
  • 7
  • And why would that be needed at all? Inquiring minds want to know! – Martijn Pieters Jul 14 '14 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. – Lyndsy Simon Jul 14 '14 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. – Martijn Pieters Jul 14 '14 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. – sal_x_sal Jul 14 '14 at 19:36
  • @MartijnPieters - actually, I think he does want to keep the process command line secret. – tdelaney Jul 14 '14 at 19:44
  • Why was this closed? Poster clearly shows that output is already redirected to results.out. He wants to hide the process from other users (from ps, top, etc...). – tdelaney Jul 14 '14 at 19:47
  • @tdelaney: then I invite the OP to clarify that in an edit, after which it could perhaps be reopened. There are probably *other* posts this is then a dupe of, however. – Martijn Pieters Jul 14 '14 at 19:47
  • @tdelaney: in any case, my close vote was actually for 'unclear what you are asking' for that very reason; ambiguity as to what the intention is here. – Martijn Pieters Jul 14 '14 at 19:48
  • There are ways to hide the process. I googled ("selinux hide process parameters") and got [this useful post](http://unix.stackexchange.com/questions/17164/how-to-make-a-process-invisible-to-other-users) among others. – tdelaney Jul 14 '14 at 19:51
  • thanks tdelaney! @MartijnPieters I tried to edit the post for some clarification. It seems what I am thinking of doing is tougher than I thought. I thought there would be some kind of a "hide"/or visible=off command that I could use whenever I do a system call within my script. I will try to look into what tdelaney has shared but if it is too complicated I will just keep using the os.system. Thanks! – sal_x_sal Jul 14 '14 at 20:05

3 Answers3

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).

  • 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. – dano Jul 14 '14 at 20:13
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 )
etr
  • 1,252
  • 2
  • 8
  • 15
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.

Colin Phipps
  • 908
  • 5
  • 8