Im new to python and Im trying to get the IP Address of my network card using the following:
import sys
import os
ip_address = os.system('/sbin/ifconfig ens33 | grep "inet" |awk '/inet / { print $2 }' | cut -d":" -f2')
However it returns the following error:
ip_address = os.system('/sbin/ifconfig ens33 | grep "inet" |awk '/inet / { print $2 }' | cut -d":" -f2')
^
SyntaxError: invalid syntax
If I just have in up to here it get some of the output:
ip_address = os.system('/sbin/ifconfig ens33 | grep "inet" ')
inet 192.168.130.130 netmask 255.255.255.0 broadcast 192.168.130.255 inet6 fe80::97b9:2816:c3a3:e02e prefixlen 64 scopeid 0x20
Is there a way to do this using os and sys ?
os.system()does not return the output of the command. You're going to want to look at thesubprocessmodule.""". However, why do you need to feed the results ofifconfigthroughgrepandawk? You can just process it in Python...os.sytem, use thesubprocessmodule