173 lines
3.5 KiB
Bash
Executable File
173 lines
3.5 KiB
Bash
Executable File
#!/bin/bash
|
|
############################################################
|
|
# Decription: Preinst linux GMo cloned server from VMWare WS
|
|
# Author: Gilles Mouchet (gilles.mouchet@gmail.com)
|
|
# Creation Date: 29-Aug-2021
|
|
# Version: 3.1
|
|
|
|
#add entry
|
|
#modify entry
|
|
#delete entry
|
|
#display entry
|
|
|
|
version="0.1"
|
|
#functions
|
|
# usage
|
|
usage() {
|
|
cat << EOF
|
|
Usage: ./gest-host.sh options
|
|
Manage host file
|
|
Options:
|
|
-a, --add an entry (-i -n mandatory for this option)
|
|
-p, --print display all entry
|
|
-r, --remove remove an entry
|
|
-i, --ip last two or three digit of ip address
|
|
-H, --host host name
|
|
-h, --help display this help text
|
|
-v, --version version
|
|
EOF
|
|
}
|
|
#------------------------------------------------------------------------------
|
|
# check error
|
|
check_error()
|
|
{
|
|
if [ "$1" -ne "0" ]; then
|
|
echo "[ERROR] - $2"
|
|
fi
|
|
}
|
|
#------------------------------------------------------------------------------
|
|
# add host
|
|
add_host()
|
|
{
|
|
# check if ip already exit
|
|
echo "toto"
|
|
}
|
|
|
|
# db name
|
|
db_name=hosts.sqlite
|
|
|
|
# mapping from long options to short options
|
|
for arg in "$@"; do
|
|
shift
|
|
case "$arg" in
|
|
"--add") set -- "$@" "-a" ;;
|
|
"--ip") set -- "$@" "-i" ;;
|
|
"--host") set -- "$@" "-H" ;;
|
|
"--remove") set -- "$@" "-r" ;;
|
|
"--update") set -- "$@" "-u" ;;
|
|
"--domain") set -- "$@" "-d" ;;
|
|
"--help") set -- "$@" "-h" ;;
|
|
"--version") set -- "$@" "-v" ;;
|
|
*) set -- "$@" "$arg"
|
|
esac
|
|
done
|
|
|
|
# Utilisation de getopts pour les options courtes
|
|
while getopts ":a:i:rud:hv" opt; do
|
|
option=${opt}
|
|
case ${opt} in
|
|
a)
|
|
action=add
|
|
;;
|
|
d)
|
|
domain=$OPTARG
|
|
case ${domain} in
|
|
gmetech.net)
|
|
ip_root=192.168.1.
|
|
;;
|
|
gmolab.net)
|
|
ip_root=172.31.10.
|
|
;;
|
|
*)
|
|
echo "[ERROR] - $domain is not valid"
|
|
usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
;;
|
|
i)
|
|
ip_last_digit=$OPTARG
|
|
# check if it's a numbeer
|
|
re='^[0-9]+$'
|
|
if ! [[ $ip_last_digit =~ $re ]] ; then
|
|
echo "[ERROR] - Not a number"
|
|
exit 1
|
|
fi
|
|
if [[ $ip_last_digit -le 4 || $ip_last_digit -ge 255 ]]; then
|
|
echo "Bad ip"
|
|
exit 1
|
|
else
|
|
ip=${ip_root}${ip_last_digit}
|
|
fi
|
|
|
|
;;
|
|
r)
|
|
action=remove
|
|
;;
|
|
u)
|
|
action=update
|
|
;;
|
|
h)
|
|
usage
|
|
;;
|
|
f)
|
|
file=$OPTARG
|
|
;;
|
|
v)
|
|
cat << EOF
|
|
gest-hosts $version
|
|
Copyright (C) 2021-`date +"%Y"` - GMo Free Software.
|
|
Written by Gilles Mouchet (gilles.mouchet@gmail.com)
|
|
EOF
|
|
exit
|
|
;;
|
|
\?)
|
|
echo "Option invalide: -$OPTARG" >&2
|
|
usage
|
|
;;
|
|
:)
|
|
echo "L'option -$OPTARG requiert un argument." >&2
|
|
usage
|
|
;;
|
|
esac
|
|
done
|
|
shift $((OPTIND -1))
|
|
|
|
# resume
|
|
echo "Domain = $domain"
|
|
echo "IP = $ip"
|
|
|
|
# Exemple de traitement des options
|
|
#if [ "$verbose" = true ]; then
|
|
# echo "Mode verbeux activé"
|
|
#fi
|
|
|
|
#if [ -n "$file" ]; then
|
|
# echo "Fichier spécifié: $file"
|
|
#fi
|
|
|
|
# Le reste de votre script ici...
|
|
|
|
|
|
|
|
# check if template exist
|
|
|
|
if [[ -z "${domain}" && "${option}" != "h" && "${option}" != "v" ]]; then
|
|
echo "Domain is missing (-d)"
|
|
usage
|
|
exit 1
|
|
fi
|
|
|
|
# create sqlite db if not existe
|
|
if [ ! -f $db_name ]; then
|
|
sqlite3 $db_name "create table hosts (id INTEGER PRIMARY KEY,ip TEXT,host TEXT,fqdn TEXT);"
|
|
check_error $? "create database $db_name"
|
|
sqlite3 $db_name "INSERT INTO hosts (ip,host,fqdn) VALUES ('192.168.1.1','router','router.gmetech.net')"
|
|
fi
|
|
|
|
|
|
|
|
# select domain
|
|
|
|
|
|
#echo $domain |