I want to extract ip address from 'arp -an' in a python script please.
In terminal, I can do
arp -an | grep '192.168.'
Returns ? (192.168.1.10) at so:me:ma:ca:dd:rs [ether] on wlan0
And then I can use wild cards to get the ip address in the brackets out.
So I tried in python
ip_substr = '192.168.1'
arp_command = ['arp', '-an', '|', 'grep', ip_substr]
arp_entry = subprocess.call(arp_command)
Which gave error
arp: grep: Host name lookup failure
(happened when subprocess calling that)
Question: what did I do wrong when passing the commands please? I also tried having ' | '
and ' grep '
, basically with spaces but no luck.
Or if this is not a feasible way, how should I be able to extract the ip address that matches '192.168.1' from the returned table 'arp -an'? I tried to process by row
for row in arp_result:
if ip_substr in row:
print 'found'
but it gave 'int objects not iterable' error.
Thanks.