[Level 3] How to get option 43 from dchp server.

You can following the steps to implement get option 43 from dhcp server.

[server side]
1. install dhcp  server:
# apt-get install dhcp3-server

2. modify /etc/default/dhcp3-server:
(modify)
INTERFACES="eth0"

3. modify /etc/dhcp3/dhcpd.conf:
(add)
option space VendorInfo;
option VendorInfo.text code 10 = { text };

subnet 192.168.2.0 netmask 255.255.255.0 {
  range 192.168.2.2 192.168.2.9;
  filename "pxelinux.0";
  option subnet-mask 255.255.255.0;
  option broadcast-address 192.168.2.255;
  vendor-option-space VendorInfo;
  option VendorInfo.text "option43 text";
}

4. restart server:
# /etc/init.d/dhcp3-server restart

[client side]
1. modify /etc/dhcp3/dhclient.conf:
(add)
option space VendorInfo;
option VendorInfo.text code 10 = { text };
(modify)
request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, domain-search, host-name,
        netbios-name-servers, netbios-scope, interface-mtu,
        rfc3442-classless-static-routes, ntp-servers, VendorInfo.text;

2. restart dhcp client:
# dhclient eth0

3. create python script to get option43.
# cat /getOption43.py
#!/bin/env python
import os, sys
import re

def dec2hex(n):
    return "%X" % n

def hex2dec(s):
    return int(s, 16)

 
def getOption43(
numOption):
    sGrepCmd = '/bin/grep'
    sDhcpclientLeases = '/var/lib/dhcp3/dhclient.leases'
    sFilter = 'vendor-encapsulated-options'
    sOption43 = os.popen('%s %s %s 2>/dev/null | tail -1 | awk "{print(\$3)}"' % (sGrepCmd, sFilter, sDhcpclientLeases)).read()
    aOption43 = re.sub(';$','',sOption43.rstrip()).split(':')
    sVendorInfo = ''
    while True:
        if numOption == hex2dec(aOption43[0]):
            aOption43 = aOption43[2:hex2dec(aOption43[1])+2]
            for nAscii in aOption43:
                sVendorInfo = sVendorInfo + chr(hex2dec(nAscii))
            return sVendorInfo
        else:
            aOption43 = aOption43[hex2dec(aOption43[1])+2:]
            if len(aOption43) == 0: return ''


print getOption43(10)

4. test python
# /getOption43.py
option43 text
#

Wish this helps.
regards,
Stanley Huang

Comments

Popular posts from this blog

[Level 1] Rar tool for Solaris.

[Level 2] iif in Python