0

I would like to run bash commands in python3. However, different commands have different types of output, and need to be called differently (as far as I know).

For example:

cat /etc/passwd | grep user
  and
cat /etc/passwd

subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')


alsamixer or nano or vi:

subprocess.call(command.split(" "))

What would be a better way to make it so that, regardless of the command, it gets handled correctly? (Show nano, or get the output of grep)?

pidgey
  • 245
  • 1
  • 3
  • 15
  • how does the code know whether you want to get the output or not? Or whether you want to run the command directly or in the shell? – jfs Oct 29 '16 at 13:09
  • That is indeed the thing I'd like to figure out. Because "whitelisting" a set of commands doesn't seem like the right approach. I was thinking, maybe there is a way to check if a command is "interactive", like nano, alsamixer, etc... Do you know if there is? – pidgey Oct 31 '16 at 09:49
  • I meant that computer can't (normally) read your mind. Why can't you run all given commands as bash commands: `call(command, shell=True, executable='/bin/bash')`? – jfs Oct 31 '16 at 09:54
  • How would I get the output of a command like netstat into a variable then? – pidgey Oct 31 '16 at 10:20
  • [`output = check_output(command, ...)`](http://stackoverflow.com/q/1996518/4279) – jfs Oct 31 '16 at 10:38
  • That only returns 0 (correct execution of the command) – pidgey Oct 31 '16 at 12:39
  • wrong. Read check_output docs. – jfs Oct 31 '16 at 12:43
  • Sorry, I tried that with call... Doing that with check_ouput breaks commands like nano, as you don't get the interface, it just returns the bytecodes that make up the screen – pidgey Oct 31 '16 at 13:44
  • again, your computer can't read your mind and decide for you whether you want to display the output in the terminal or to get the output "into a variable". Why do you need to capture the output as a string for some commands and not others? Why do you want to show the output on the screen for some commands and not others? – jfs Oct 31 '16 at 13:54
  • Commands like nano, less, alsamixer are interactive, while commands like cat, grep, netstat are not. This is why, for the non-interactive commands, I need the output, so I can handle it programmatically, while the others, I don't need the output, because I will have it displayed interactively – pidgey Nov 02 '16 at 08:33
  • You can use `cat`, `grep` interactively (it would be unusual but you can do it, just try it). Even if a program is used interactively as a rule; it may provide a batch mode e.g., emacs has `-batch` option that runs it noninteractively. How do you know whether you should run a program interactively or not? What is a larger context for your question? Perhaps, you want `script` utility, to save everything that is displayed in the terminal (sometimes, you need [`screen -L` to capture the output](http://stackoverflow.com/a/25968448/4279)). – jfs Nov 02 '16 at 10:14
  • The project I'm working on (https://github.com/NorthernSec/pybash) would combine python and bash. Bash commands with an output should be able to be piped to python directly, while interactive commands should not. – pidgey Nov 02 '16 at 12:45
  • bash does not know either when to capture the output in a variable, you have to use a special syntax e.g., `variable = $(some command)`. Take a look how xonsh, ipython, pymux run external bash commands. – jfs Nov 02 '16 at 14:14

0 Answers0