| 1 | #!/bin/bash |
| 2 | #Experimental BASH script to compile DHCP options |
| 3 | |
| 4 | dec_to_hex() { |
| 5 | echo 'obase=16; '"$1" | bc |
| 6 | } |
| 7 | |
| 8 | dec_to_hex_right() { |
| 9 | echo -n $(while true; do echo -n 0; done | head -c "$2")$(dec_to_hex "$1") | tail -c "$2" |
| 10 | } |
| 11 | |
| 12 | escape() { |
| 13 | while read -n 2 i; do |
| 14 | [ -n "$i" ] && echo -n '\x'"$i"; |
| 15 | done |
| 16 | } |
| 17 | |
| 18 | dnsmasq() { |
| 19 | sed -e 's/^\\x//g' | sed -e 's/\\x/:/g' |
| 20 | } |
| 21 | |
| 22 | dhcp_option() { |
| 23 | separator='\x' |
| 24 | option_id="$1" |
| 25 | option_data="$2" |
| 26 | |
| 27 | len=$(echo -n "$option_data" | wc -c) |
| 28 | dec_to_hex_right $option_id 2 | escape |
| 29 | dec_to_hex_right $len 2 | escape |
| 30 | printf "$option_data" | xxd -ps -c 256 | escape |
| 31 | } |
| 32 | |
| 33 | pd() { |
| 34 | option_id=25 |
| 35 | iaid='\x00\x00\x00\x00' |
| 36 | t1='\x00\x00\x01\x2C' |
| 37 | t2='\x00\x00\x01\x2C' |
| 38 | ia_pd_opts='' |
| 39 | dhcp_option $option_id "$iaid$t1$t2$id_pd_opts" |
| 40 | } |
| 41 | |
| 42 | pd | dnsmasq |
| 43 | echo |