Форум сайта python.su
0
Есть файл .txt. В нем должен быть список с двумя столбцами: в первом - mac адреса, во втором - имена компьютеров. С помощью python-скрипта необходимо получить mac адрес машины, на которой был запущен скрипт и определить какому имени компьютера он соответствует.
Также, подскажите, пожалуйста, как следует организовать этот список в файле?
P.S.: Машины все на ubuntu, если это имеет значение.
Офлайн
857
Leviaphan
Машины все на ubuntu
[guest@localhost ~]$ ifconfig | awk '/ether/{print $2}'
6c:62:6d:5b:f0:0a
[guest@localhost ~]$Отредактировано py.user.next (Фев. 27, 2015 13:28:06)
Офлайн
103
python скрипт
from uuid import getnode as get_mac filename = 'mac_name' mac = ':'.join(("%012x" % get_mac())[i:i+2] for i in range(0, 12, 2)) with open(filename) as f: for i in f.readlines(): if i.startswith(mac): print(i[18:]) break else: print('Not found :(')
1a:bf:48:37:9c:dc Test
10:bf:48:37:9c:dc Andrey
1b:bf:48:37:9c:dc bbbb
Отредактировано terabayt (Фев. 27, 2015 20:10:11)
Офлайн
857
[guest@localhost ~]$ ifconfig | awk '/ether/{print $2}'
6c:62:6d:5b:f0:0a
[guest@localhost ~]$>>> from uuid import getnode as get_mac >>> filename = 'mac_name' >>> mac = ':'.join(("%012x" % get_mac())[i:i+2] for i in range(0, 12, 2)) >>> mac '9b:7d:3c:f1:86:f4' >>>
Отредактировано py.user.next (Фев. 27, 2015 23:38:59)
Офлайн
103
andrey@laptop:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 10:bf:48:37:9c:dc
UP BROADCAST MULTICAST MTU:1500 Metric:1
Прийнято пакунків:0 помилок передачі:0 пропущено:0 перерозхід:0 кадри:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
Прийнято байтів:0 (0,0 B) Передано байтів:0 (0,0 B)
wlan0 Link encap:Ethernet HWaddr 94:db:c9:b1:c9:fe
inet addr:10.3.1.245 Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::96db:c9ff:feb1:c9fe/64 Scope:Посилання
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Прийнято пакунків:90644 помилок передачі:0 пропущено:0 перерозхід:0 кадри:0
TX packets:55018 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
Прийнято байтів:128458418 (128,4 MB) Передано байтів:5809682 (5,8 MB)
andrey@laptop:~$ python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from uuid import getnode as get_mac >>> ':'.join(("%012x" % get_mac())[i:i+2] for i in range(0, 12, 2)) '94:db:c9:b1:c9:fe'
andrey@laptop:~$ python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from uuid import getnode as get_mac >>> ':'.join(("%012x" % get_mac())[i:i+2] for i in range(0, 12, 2)) '10:bf:48:37:9c:dc'
Отредактировано terabayt (Фев. 28, 2015 11:23:33)
Офлайн
857
[guest@localhost ~]$ ifconfig | grep ether
ether 6c:62:6d:5b:f0:0a txqueuelen 1000 (Ethernet)
[guest@localhost ~]$
>>> from uuid import getnode as get_mac >>> filename = 'mac_name' >>> mac = ':'.join(("%012x" % get_mac())[i:i+2] for i in range(0, 12, 2)) >>> mac 'b3:b9:3b:57:1d:db' >>>
>>> from uuid import getnode as get_mac >>> filename = 'mac_name' >>> mac = ':'.join(("%012x" % get_mac())[i:i+2] for i in range(0, 12, 2)) >>> mac '39:93:5c:ff:af:f9' >>>
Офлайн
103
andrey@laptop:~$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from uuid import getnode as get_mac
>>> mac = ':'.join(("%012x" % get_mac())[i:i+2] for i in range(0, 12, 2))
>>> mac
'10:bf:48:37:9c:dc'
>>>
andrey@laptop:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from uuid import getnode as get_mac
>>> mac = ':'.join(("%012x" % get_mac())[i:i+2] for i in range(0, 12, 2))
>>> mac
'10:bf:48:37:9c:dc'
>>>
andrey@laptop:~$ ifconfig | grep eth0
eth0 Link encap:Ethernet HWaddr 10:bf:48:37:9c:dc
andrey@laptop:~$ uname -a
Linux laptop 3.13.0-46-generic #76-Ubuntu SMP Thu Feb 26 18:52:13 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
If all attempts to obtain the hardware address fail, we choose a random 48-bit number with its eighth bit set to 1 as recommended in RFC 4122
Отредактировано terabayt (Март 1, 2015 00:47:47)
Офлайн
857
terabaytНе, это в uuid.py такая фигня.
может у Вас не может получить и
getters = [_unixdll_getnode, _ifconfig_getnode]
getters = [_ifconfig_getnode, _unixdll_getnode]
def _unixdll_getnode(): """Get the hardware address on Unix using ctypes.""" _buffer = ctypes.create_string_buffer(16) _uuid_generate_time(_buffer) return UUID(bytes=bytes_(_buffer.raw)).node
Отредактировано py.user.next (Март 1, 2015 05:17:04)
Офлайн