Last active
August 29, 2015 14:06
-
-
Save rerorero/5e8811379d1dce86fc76 to your computer and use it in GitHub Desktop.
hostsファイルをdnsmasq設定に変換する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# usage: | |
# $ sudo hosts2dnsmasq.sh /etc/hosts | |
source=$1 | |
tmphosts="$1.tmp" | |
maskhostsdir=/etc/dnsmasq.d | |
maskhostsfile=0hosts | |
dnsmasq_hosts="$maskhostsdir/$maskhostsfile" | |
dnsmasq_conf=/etc/dnsmasq.conf. | |
dnsmasq_resolv=/etc/resolv.dnsmasq.conf | |
namesrv1="8.8.8.8" | |
namesrv2="8.8.4.4" | |
if [ $# -ne 1 ]; then | |
echo "Too short arguments." | |
exit 1 | |
fi | |
cp -f $source $tmphosts | |
mkdir -p | |
if [ ! -e $maskhostsdir ]; then | |
mkdir -p $maskhostsdir | |
fi | |
if [ -e $dnsmasq_hosts ]; then | |
rm -f $dnsmasq_hosts | |
fi | |
if [ -e $dnsmasq_conf ]; then | |
rm -f $dnsmasq_conf | |
fi | |
if [ -e $dnsmasq_resolv ]; then | |
rm -f $dnsmasq_resolv | |
fi | |
# Remove comments and empty lines | |
sed -i -e "/^#.*$/d" $tmphosts | |
sed -i -e "/^$/d" $tmphosts | |
# convert ::1 | |
sed -i -e "s/^::1/0:0:0:0:0:0:0:1/g" $tmphosts | |
# reading per line | |
IFS_BK=$IFS | |
IFS=$'\n' | |
file=(`cat $tmphosts`) | |
for i in ${file[@]} | |
do | |
pos=0 | |
IFS=$IFS_BK | |
line=($i) | |
# delimit per space | |
for p in ${i[@]} | |
do | |
if [ $pos -eq 0 ] | |
then | |
addr=$p | |
else | |
map="address=\"/$p/$addr\"" | |
echo $map >> $dnsmasq_hosts | |
fi | |
pos=`expr $pos + 1` | |
done | |
IFS=$IFS_BK | |
done | |
#ちゃんと戻す | |
IFS=$IFS_BK | |
# dnsmasq configration | |
echo 'listen-address=127.0.0.1' >> $dnsmasq_conf | |
echo "resolv-file=$dnsmasq_resolv" >>$dnsmasq_conf | |
echo "conf-dir=$maskhostsdir" >>$dnsmasq_conf | |
echo 'user=root' >> $dnsmasq_conf | |
echo "nameserver $namesrv1" >> $dnsmasq_resolv | |
echo "nameserver $namesrv2" >> $dnsmasq_resolv | |
rm -f $tmphosts | |
# echo results | |
echo "[$dnsmasq_hosts]" | |
cat $dnsmasq_hosts | |
echo "[$dnsmasq_conf]" | |
cat $dnsmasq_conf | |
echo "[$dnsmasq_resolv]" | |
cat $dnsmasq_resolv | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment