3
I’m new to the python language, but I started to develop a script to manage linux servers etc... I have a little problem in a code snippet:
def rotear():
print(" \n Disponível: \n"), os.system("ifconfig|grep eth|cut -c 1-4")
interface_internet = input(" \n Digite a Interface de Internet: ")
if interface_internet != ethers[0]:
print("Nao deu certo!")
Next, I listed the network interfaces, I put together this snippet of code, but I wanted to figure out a way to get the list of network interfaces, and make a condition true or false to proceed with the code
ex:
listed the interface with the ifconfig command | grep Eth | cut -c 1-4
eth0
eth1
if interface_internet is different from one of the above listings, do this otherwise, do that
I wanted a solution on this...
So I put the code:
def rotear():
ethers = ['eth0','eth1','eth2','wlan0','wlan1','ath0','ath1']
print(" \n Listando Interfaces de Rede(s)..."),time.sleep(1)
print(" \n Disponível: \n"), os.system("ifconfig|grep eth|cut -c 1-4")
interface_internet = input(" \n Digite a Interface de Internet: ")
for device in ethers:
if interface_internet == device:
header(" \n 1 - Habilitar Roteamento")
header(" 2 - Desabilitar Roteamento\n")
encaminhar = input(" Escolha uma Opção de Roteamento:")
if encaminhar == "1":
os.system("echo 1 > /proc/sys/net/ipv4/ip_forward")
os.system("iptables -t nat -A POSTROUTING -o %s -j MASQUERADE" % (interface_internet))
sucess(" \n Roteamento Habilitado Com Sucesso..."),tcl()
elif encaminhar == "2":
os.system("echo 0 > /proc/sys/net/ipv4/ip_forward")
sucess(" \n Roteamento Desabilitado Com Sucesso..."),tcl()
else:
fail(" \n Atenção: Valor Inválido. ")
only I’m in trouble, if I put the value: eth2 for example, he makes two loops, saying that the value ta invalido until right... how do I fix it now?
Please correct the code indentation
– Gabriel Belini
To use python for this purpose, consider using the package I suggest in my answer.
– Sidon