put a command output in a python3 list

Asked

Viewed 238 times

1

I’ve researched it, and I’ve tried to make it work in many ways. execute the order:

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 departure of this command to be part of my empty list ethers = [] tried with the append but it didn’t work, if you have a solution other than the os.system.

EDIT 1

Good people, I managed to solve as follows:

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" there I will make my conditions... hope to help someone with this hugging information !

1 answer

1


The module of Std lib subprocess can help you.

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

And in case, res gets:

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

  • William Thank you very much for the answer, but in this case, you would have to have the empty output of the command, because it is a list of network interfaces, and you will have an "input" asked the interface that he wants to put, coincidentally I can figure out how to do, I will now edit my question and put the formula. BUT THANK YOU VERY MUCH FOR YOUR ATTENTION

  • well, on linux, it’s just os.listdir('/sys/class/net/') .

  • And then there’s the package netifaces which, along with the standard libs for networking seems to be pretty complete for this kind of problem

  • Thanks William, I will study about what you said, obg by the tips!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.