0

I have some questions about the subprocess function in python 2.7 I want to build an script that forms an if clause like:

Import os
Import subprocess
process = subprocess ('uname -a')
if process == 'ESXi':
    print ('yey')

I am sorry that the code has many mistakes and that my english is so poorly (motherlanguage greek)

John1024
  • 109,961
  • 14
  • 137
  • 171
Merrand
  • 3
  • 2
  • What is your question? –  Jan 10 '18 at 19:48
  • How to check if the output value contains 'ESXi' – Merrand Jan 10 '18 at 19:51
  • You want a substring match? https://stackoverflow.com/q/3437059/1531971 –  Jan 10 '18 at 19:52
  • Yes, just compared with the output value of the shell function uname -a – Merrand Jan 10 '18 at 19:54
  • You should say this in the question body: I want to run a subprocess and get the results from "uname -a" as a string. I then want to test whether or not this result contains the string "ESXi". –  Jan 10 '18 at 19:56
  • I am very sorry, i am not very good in english – Merrand Jan 10 '18 at 19:57
  • Also, this code doesn't seem to be valid Python. Remember: https://stackoverflow.com/help/mcve –  Jan 10 '18 at 19:58
  • You probably need to use `subprocess.check_output()` and then `in` for testing the result. See: https://docs.python.org/2/library/subprocess.html –  Jan 10 '18 at 20:05

1 Answers1

0

to get the output of your command

word = 'ESXi'
process = subprocess.check_output(['uname','-a'])  
if word in str(process):   
   print ('yey')

result of your command will be in process

for more info (python doc) and example code

also, refer otheres question

WSMathias9
  • 669
  • 8
  • 15