Skip to content

Instantly share code, notes, and snippets.

@tykling
Created September 28, 2012 15:08
Show Gist options
  • Save tykling/3800422 to your computer and use it in GitHub Desktop.
Save tykling/3800422 to your computer and use it in GitHub Desktop.
hosts file to bind zone file
#!/bin/sh
while read line; do
ip=$(echo $line | cut -d " " -f 1);
hosts=$(echo $line | cut -d " " -f 2-);
zone=$(echo $hosts | tr " " "\n" | awk '{print length"\t"$0}'|sort -n|cut -f2- | head -1);
echo "processing zone: $zone (ip $ip - hosts $hosts)";
echo "\$ORIGIN $zone." > $zone.db;
echo "\$TTL 1h" >> $zone.db;
echo "$zone. IN SOA ns.$zone. username.$zone. ( 2007120710; 1d; 2h; 4w; 1h )" >> $zone.db;
for host in $hosts; do
if [ "$host" = "$zone" ]; then
echo "@ A $ip" >> $zone.db;
else
echo "$host A $ip" >> $zone.db;
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment