DHCP Client Info

Foto os

Anda telah memasang DHCP server dan sudah banyak PC client yang terhubung. Mungkin Anda ingin tahu nama-nama PC yang sudah terhubung dan berapa IP-nya.

Uji coba menggunakan Ubuntu Karmic yang sudah dipasang DHCP server:

# apt-get install dhcp3-server

dengan konfigurasi standar. Tentu saja seluruh PC client harus diset Dynamic IP.

Lalu buatlah file host.py berikut ini:

"""
Daftar DHCP Client
(c)2009 RAB Linux Indonesia
"""
 
import re
import commands
 
hosts = {}
ip, mac, hostname = None, None, None
f = open('/var/lib/dhcp3/dhcpd.leases','r')
for line in f.readlines():
    line = line.strip()
    match = re.compile('lease (.*) (.*)').search(line)
    if match:
        ip, mac, hostname = match.group(1), None, None
        continue
    match = re.compile('hardware ethernet (.*);').search(line)
    if match:
        mac = match.group(1)
        continue
    match = re.compile('client-hostname "(.*)";').search(line)
    if match:
        hostname = match.group(1)
        continue
    if ip and mac and hostname:
        hosts[ip] = {'mac': mac, 'name': hostname}
        ip, mac, hostname = None, None, None
f.close()
 
OK = {True: 'ON ', False: 'OFF'}
 
for ip in hosts:
    connected = False
    for line in commands.getoutput('ping -c 3 %s' % ip).splitlines():
        line = line.strip()
        match = re.compile(' (.*) received').search(line)
        if not match:
            continue
        connected = match.group(1) != '0'
    print OK[connected], ip.ljust(16), hosts[ip]['mac'].ljust(17), hosts[ip]['name']

Jalankan:

$ python host.py

Contoh hasilnya:

ON  192.168.0.29     00:25:b3:77:3e:99 compaq
ON  192.168.0.28     00:1d:72:0b:15:6b melly
ON  192.168.0.27     00:50:04:07:0d:0c home-e018f02492
ON  192.168.0.2      00:10:dc:7e:a1:fd NOVI
ON  192.168.0.31     00:50:04:1d:5f:23 ppmbgr
ON  192.168.0.6      00:50:ba:5f:de:2e ppm-5611034f548
ON  192.168.0.4      00:01:03:cf:27:ad kasir
ON  192.168.0.5      00:09:6b:26:30:c9 ppm-e7f2611adab
ON  192.168.0.8      00:1e:ec:19:b0:1b ppm
ON  192.168.0.9      00:00:1c:db:75:c3 FRONT3

File terlampir, selamat mencoba.

LampiranUkuran
host.py.txt1.13 KB