Welcome, guest | Sign In | My Account | Store | Cart

get the mac address from ip address work only in linux system

Python, 8 lines
1
2
3
4
5
6
7
8
import os,re
file=open('/root/info.txt','w+')

r=raw_input('ip\n\r')
os.system('ping -c 1 '+r+' > /root/info.txt')
os.system("arp -a "+r+" | awk '{print $4}'  > /root/info.txt")
data=file.read()
print data

3 comments

zetah 8 years, 4 months ago  # | flag

How is this Python recipe?

You are using three different programs and Python just to read their output, while you should have used bash or else.

jkereako 8 years, 4 months ago  # | flag

@zetah

It's an example of how to execute system commands in a Python script. This is useful for Python beginners, like myself. As they say, there is more than 1 way to skin a cat.

Rajendra 8 years, 4 months ago  # | flag

Code to extract MAC address frm raw text.(works on windows)

import os,re
r=input('ip\n\r')
os.system('ping '+r+' > info.txt')
os.system("arp -a "+r+" > info.txt")
file=open('info.txt','r+')
data=file.read()
X = '([a-fA-F0-9]{2}[:|\-]?){6}' 
a = re.compile(X).search(data)
if a:
    print (data[a.start(): a.end()])