Create hosts file for script on linux

When creating a script to retrieve information from network nodes, you need a list of node to retrieve information from. Maintaining a hostfile manually is tedious and cumbersome work.

Therefore I choose to get information from dns. Provided dns is a information source which can be trusted to be up-to-date.

The code that I used is :

#!/bin/bash

function syntax() {
        echo "syntax $(basename $0) <no parameters>"
        echo "parameters are;"
        echo "- ups"
        echo "- xs"
        echo " "
        exit
}

action="help"
[[ $1 == "--help" || $1 == "-h" || $# -eq 0 ]] && action="help"
[[ $1 == "ups" ]] && action="ups"
[[ $1 == "xs" ]] && action="xs"

case $action in
        "help")
                syntax
                ;;

        "xs")   echo "- creating hosts file for : XS switches"
                host -l net.domain.bla |
                        egrep "^xs" |
                        grep -v IPv6 |
                        grep -v "\-test\-" |
                        grep -v "server" |
                        awk '{print $1}' > hosts_xs_${timestamp2}.txt
                ;;
        "ups")
                echo "- creating hosts file for : UPS's"
                # omit information as many information to create a clean list.
                host -l watt.domain.bla |
                        egrep "^ups" |
                        grep -v mer |
                        grep -v "server" |
                        awk '{print $4}' > hosts_ups_${timestamp}.txt

                        # 1st field = hostname
                        # 4th field = ip adres
                ;;
esac

The hostfile created has a timestamp included in the filename. So I know the validity of the hostfile. This file can be used for other bash/tcl scripts.

This entry was posted in CCNP. Bookmark the permalink.