0

I am trying to execute this bash command n python and try to evaluate the output, based on the output, I want to show my own-defined outputs.

The bash command I am trying to execute using the python is:

kubectl get pods --field-selector=status.phase=Failed -n kube-system

everything looks really good and only problem I am having is,

This outputs No resources found, means there are no resources matching the given criteria (i.e status.phase=Succeeded), but that is fine, but the problem is It prints in the terminal. What I want to do is, prints my own-defined output when the actual output is No resources found, but I can't do that since it already prints that output in the terminal. I even can't use the status_code to check, it always gives 0 even after the resources are found or not (which is correct) since code has executed successfully. Is there a way that I can filter the output during the executing of the bash command and gives a condition based on the output to print my own-defined output?

Here is my code snippet:

def subprocess_execute_arr(self):
    output = subprocess.call(self)
    return output

cmd_failed = ["kubectl", "get", "pods", "--field-selector=status.phase=Failed", "-n", "kube-system"]
failed = execute.subprocess_execute_arr(cmd_failed) //this is where it prints the output in the terminal

output:

No resources found.

PS: Here output is not an error, command has executed correctly but I don't need to print this output.

Any idea how to solve this?

Jananath
  • 53
  • 9
  • Does this answer your question? [Running shell command and capturing the output](https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output) – rivamarco Dec 16 '19 at 08:18
  • I looked into this, and it doesn't. What I want is I want to hide the output depending on the output it ought to give, so the output should be filtered during the execution of the command. – Jananath Dec 16 '19 at 08:23
  • If you do `result = subprocess.run(['echo', 'hello'], stdout=subprocess.PIPE)` the echo will not be printed. In `result` you have the captured output, you can check here if it is `No resources found.` and not print to stdout. – rivamarco Dec 16 '19 at 08:25
  • it gives the error **AttributeError: 'module' object has no attribute 'run'**. I am using `python 2.7` – Jananath Dec 16 '19 at 08:30
  • Sorry, but that's similar in 2.7. You can do `result = subprocess.check_output(['echo', 'hello'])`, you have your output in `result` – rivamarco Dec 16 '19 at 08:34
  • not still it doesn't capture the output, it gives an empty value even though the real out put is `No resource found` . I think this `No resource Found` is given from the kubernetes itself and that is why the reason. Any solution for this? – Jananath Dec 16 '19 at 08:40
  • Maybe it's on `stderr`? Try this: ```p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) output = p.stdout.read()``` with `cmd` as your command. – rivamarco Dec 16 '19 at 08:46
  • Thank you this works! But there is one problem. I am checking this output and based on that I give the output of my own. How I do this is by checking substrings in this output. But what if this output changes in future? I have to change my substrings too, which is hectic when I have hundreds to change. – Jananath Dec 16 '19 at 08:55
  • If the output changes, you have to change your substrings. You can use regex but if the output is completely different due to updates ecc you have to change it. – rivamarco Dec 16 '19 at 09:03
  • How can I use the regex? I am trying to use just `in` operator in python. How to do this using `regex`? – Jananath Dec 16 '19 at 09:06
  • I think it is better if you search on SO or ask for another question. But i think you can continue with `in` operator in your case. – rivamarco Dec 16 '19 at 10:52

0 Answers0