-1

I'm trying to read the output of the ~$ ifconfig command with a python script and grab the private IP-address. this is for example the output of the command:

~$ ifconfig  
vmnet1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.216.1  netmask 255.255.255.0  broadcast 172.16.216.255
        inet6 fe80::250:56ff:fec0:1  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:c0:00:01  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 213  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vmnet8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.37.1  netmask 255.255.255.0  broadcast 172.16.37.255
        inet6 fe80::250:56ff:fec0:8  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:c0:00:08  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 213  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

so then if we go ahead and direct the output of the command to this python script...

import sys
import re

data = sys.stdin.read()
x = re.search(r"^vmnet[2-9]:\s?.*(\n.*){7}", data)
print(x)
~$ ifconfig | ./script.py

the output should be:

vmnet8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.37.1  netmask 255.255.255.0  broadcast 172.16.37.255
        inet6 fe80::250:56ff:fec0:8  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:c0:00:08  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 213  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

but instead i simply get:

None
mmdz
  • 1
  • 4

1 Answers1

-1
import sys
import re

data = sys.stdin.read()
match = re.search( r'(?m)^vmnet[2-9]:\s?.*(\n.*){7}', data )
if match:
    print match.group()

The typo has already been mentioned in the comments.

You also had to use group() or group(0) with the Match object returned by .search().

Also note the (?m) flag.

Alexander Mashin
  • 3,892
  • 1
  • 9
  • 15