put the output of a command in a python3 list

1

I researched and tried to do this in several ways. I execute the command:

ethers = []
ethers1 = os.system("ifconfig|grep eth|cut -c 1-4")
ethers2 = os.system("ifconfig|grep wla|cut -c 1-4") 
ethers3 = os.system("ifconfig|grep ath|cut -c 1-4")

I wanted the output of this command to be part of my empty list ethers = [] I tried with append but it did not work if you have another solution other than os.system.

EDIT 1

Good people, I was able to solve it like this:

import subprocess, os
p = os.popen('ifconfig | grep eth | cut -c 1-4')
s = p.readline()
p.close()
print("Interface(s) Disponíveis")
print(s)
interface_internet = input(" \n Digite a Interface de Internet: ")
if interface_internet in s:

After the "if" I will do my conditions ... I hope to help someone with such hugs information!

    
asked by anonymous 21.06.2017 / 20:29

1 answer

1

The std lib module subprocess can help you.

import subprocess 
comando = "ifconfig|grep docker|cut -c 1-4"
res = subprocess.check_output(comando, shell=True)

And in this case res:

print(res)
b'dock\n'

If you need to mount terminal command sequences, there are several libs that provide an api for this. I recently heard of Sultan that looks pretty cool and pythonic.

    
21.06.2017 / 23:09