[問題] 取得區網內電腦的 mac address (Windows)

457 views
Skip to first unread message

evenrain

unread,
Sep 5, 2012, 5:16:37 AM9/5/12
to pyth...@googlegroups.com
我想透過 IP 去取得該電腦的 mac address,第一個想法是透過 windows 的 nbstat,所以我寫了這樣的程式

info = os.popen("nbtstat -A 172.24.70.12").read()
print info

執行之後什麼事情都沒有發生,後來又查了一下資料發現 os.popen 不建議使用了,所以我改用 subprocess.Popen

process = subprocess.Popen(["nbtstat", "-A", ip], stdout=subprocess.PIPE)
process.wait()
result = process.stdout.read()
print(result)

執行之後出現錯誤訊息:WindowsError: [Error 2] 系統找不到指定的檔案。

不過如果改用 arp 的話,

p = subprocess.Popen(['arp', '-a'], stdout=subprocess.PIPE)
print p.stdout.read() 

雖然說可以取得 ARP 相關資訊,再解析結果應該也行,不過我的目標是已知的幾個固定 IP,

改用 ARP 的話好像又顯得有點多餘 (而且要用 ARP 還是得把 IP 全部 Ping 過一次會比較保險)

不知道各位前輩有沒有建議的方向?

GaryLee

unread,
Sep 5, 2012, 8:25:28 AM9/5/12
to pyth...@googlegroups.com
用wmi module

import wmi
print [nic.MACAddress for nic in wmi.WMI().Win32_NetworkAdapterConfiguration() if nic.MACAddress is not None]

evenrain於 2012年9月5日星期三UTC+8下午5時16分37秒寫道:

GaryLee

unread,
Sep 5, 2012, 8:28:44 AM9/5/12
to pyth...@googlegroups.com
要比對IP Address的話,可以用nic.IPAddress == 'xxx.xxx.xxx.xxx'。

GaryLee於 2012年9月5日星期三UTC+8下午8時25分28秒寫道:

evenrain

unread,
Sep 5, 2012, 9:35:09 PM9/5/12
to pyth...@googlegroups.com
我試跑了一下,這應該是抓自己本機電腦的 mac address?

我是想抓同一個 subnet 裡其他電腦的 mac address

> --
> 您已訂閱「Google 網上論壇」的「python.tw」群組,因此我們特別傳送這封郵件通知您。
> 如要在網路上查看這項討論,請造訪 https://groups.google.com/d/msg/pythontw/-/z5_Qz_rPh-8J
>
> 如要在此群組張貼留言,請傳送電子郵件至 pyth...@googlegroups.com
> 如要取消訂閱此群組,請傳送電子郵件至 pythontw+u...@googlegroups.com
> 如需更多選項,請造訪此群組:http://groups.google.com/group/pythontw?hl=zh-TW

Apua A.Aa

unread,
Sep 5, 2012, 11:28:26 PM9/5/12
to pyth...@googlegroups.com
我用一模一樣的程式碼,很順利的得到 result 了,跟在 cmd 執行 nbtstat -A $IP 結果完全一樣。

原因應該是別的,再嘗試看看吧?

evenrain

unread,
Sep 5, 2012, 11:37:06 PM9/5/12
to pyth...@googlegroups.com
唔 這也太神奇了吧 orz
我還是一直出現錯誤訊息

Traceback (most recent call last):
  File "E:\workspace\test\src\ipmac2.py", line 4, in <module>
    p = subprocess.Popen(args, stdout=subprocess.PIPE)
  File "C:\Python27\Lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\Lib\subprocess.py", line 896, in _execute_child
    startupinfo)
WindowsError: [Error 2] 系統找不到指定的檔案。

完全摸不著頭緒 orz
我的程式是這樣寫的

#-*- coding: utf-8 -*-
import subprocess
args = ['nbtstat', '-A', '172.24.70.12']
p = subprocess.Popen(args, stdout=subprocess.PIPE)
p.wait()
result = p.stdout.read()
print result
> --
> 您已訂閱「Google 網上論壇」的「python.tw」群組,因此我們特別傳送這封郵件通知您。

evenrain

unread,
Sep 6, 2012, 12:00:37 AM9/6/12
to pyth...@googlegroups.com
剛剛查 Google 有人有類似的錯誤訊息,有網友建議加上 Shell=True,所以我改成
#-*- coding: utf-8 -*-
import subprocess
args = ['nbtstat', '-A', '172.24.70.12']
p = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)

p.wait()
result = p.stdout.read()
print result

結果執行之後出現
'nbtstat' 不是內部或外部命令、可執行的程式或批次檔。
但是我在命令列裡打是正常的啊 orz 

Keith Yang

unread,
Sep 6, 2012, 12:03:57 AM9/6/12
to pyth...@googlegroups.com
找出它的位置給固定 path?例:args = ['C:\where\the\file\nbtstat.exe', …] ?

Keith ")

evenrain

unread,
Sep 6, 2012, 1:11:50 AM9/6/12
to pyth...@googlegroups.com
我有想過可能是執行環境的 PATH 不同導致
所以剛剛把程式改成

#-*- coding: utf-8 -*-
import subprocess
args = ['c:/Windows/System32/nbtstat.exe', '-A', ip]

p = subprocess.Popen( args, shell=True,stdout=subprocess.PIPE)
p.wait()
result = p.stdout.read()
print result

但是還是一膜一樣的錯誤訊息,一整個不了解 orz 

evenrain

unread,
Sep 6, 2012, 2:02:13 AM9/6/12
to pyth...@googlegroups.com
我剛想到一件事,因為某些緣故,我是用 32-bite 的 Python 在 64-bite 的 Win7 上跑

用檔案總管去找,nbtstat 是位於  C:\Windows\System32\nbtstat.exe

然後我寫了一小段 code

print os.path.exists(r'C:\Windows\System32\nbtstat.exe')

結果得到的是 False

所以可能是因為 32-bite 的 process 看不到 nbtstat 的關係?

如果是這樣的話 ,除了改用 64-bite 的 Python 外,有其他的解法嗎 orz

Keith Yang

unread,
Sep 6, 2012, 2:06:20 AM9/6/12
to pyth...@googlegroups.com

Tib

unread,
Sep 6, 2012, 2:11:26 AM9/6/12
to pyth...@googlegroups.com
剛剛試了一下

['arp', '-a', ip]

這樣就好啦 @@

2012/9/6 Keith Yang <keit...@gmail.com>:

evenrain

unread,
Sep 6, 2012, 2:12:16 AM9/6/12
to pyth...@googlegroups.com
我剛剛也找到類似的解法,

改成 C:\Windows\sysnative\nbtstat.exe 這樣就 ok 了,謝天謝地 orz

evenrain

unread,
Sep 6, 2012, 2:15:06 AM9/6/12
to pyth...@googlegroups.com
arp 指令有 cache 的問題,所以才想到要用 nbtstat @@

Keith Yang

unread,
Sep 7, 2012, 10:03:55 AM9/7/12
to pyth...@googlegroups.com
不是喔,r"" 表示這是個 raw string,可見:

Python Gossip: 字串型態 From Gossip@caterpillar

"r prefix is for raw strings" on udacity

On Thursday, September 6, 2012 2:02:35 PM UTC+8, evenrain wrote:
我剛想到一件事,因為某些緣故,我是用 32-bite 的 Python 在 64-bite 的 Win7 上跑

用檔案總管去找,nbtstat 是位於  C:\Windows\System32\nbtstat.exe

然後我寫了一小段 code

print os.path.exists(r'C:\Windows\System32\nbtstat.exe')

結果得到的是 False
 
這裡出現False是因為你用了r'' 吧,r''表明它是一個RegExp。
如要在網路上查看這項討論,請造訪 https://groups.google.com/d/msg/pythontw/-/Qi7tEddf82MJ

Tib

unread,
Sep 5, 2012, 11:54:57 PM9/5/12
to pyth...@googlegroups.com
先確定一下 PATH 吧 ...

2012/9/6 evenrain <even...@gmail.com>:

Tib

unread,
Sep 7, 2012, 10:09:37 AM9/7/12
to pyth...@googlegroups.com
x64 windows 的 x32 版 nbtstat.exe 會被 winsxs 放在另外的地方
在我的系統上是 C:\Windows\winsxs\amd64_microsoft-windows-nbtstat_31bf3856ad364e35_6.1.7600.16385_none_fa057619380ff901\nbtstat.exe

你要不要考慮自己用 raw socket 寫 arp 還是乾脆 call ping 再 call arp 算了 XD (嘴泡

2012/9/7 Keith Yang <keit...@gmail.com>:

GaryLee

unread,
Sep 8, 2012, 9:52:51 PM9/8/12
to pyth...@googlegroups.com
Tiberius Teng於 2012年9月7日星期五UTC+8下午10時09分38秒寫道:
x64 windows 的 x32 版 nbtstat.exe 會被 winsxs 放在另外的地方
在我的系統上是 C:\Windows\winsxs\amd64_microsoft-windows-nbtstat_31bf3856ad364e35_6.1.7600.16385_none_fa057619380ff901\nbtstat.exe

你要不要考慮自己用 raw socket 寫 arp 還是乾脆 call ping 再 call arp 算了 XD (嘴泡


可以用 c:\windows\sysnative 取代 c:\windows\system32 。這樣就可以存取的程式了。

evenrain

unread,
Sep 9, 2012, 9:43:45 PM9/9/12
to pyth...@googlegroups.com
後來的確是改成  sysnative 就可以正常執行了
不過因為對應的裝置不是一般 PC,後來問了一下對方沒有 NetBIOS...
所以還是得改成先 ping 再 arp 了 orz

--
您已訂閱「Google 網上論壇」的「python.tw」群組,因此我們特別傳送這封郵件通知您。
如要在網路上查看這項討論,請造訪 https://groups.google.com/d/msg/pythontw/-/4navnCEdJ6cJ
Reply all
Reply to author
Forward
0 new messages