This commit is contained in:
Gilles Mouchet 2024-06-02 12:14:06 +02:00
commit af274aea3e
5 changed files with 237 additions and 0 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"editor.fontSize": 14,
"terminal.integrated.fontSize": 14,
"window.zoomLevel": 1.4,
}

173
gest-hosts.sh Executable file
View File

@ -0,0 +1,173 @@
#!/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

BIN
hosts.sqlite Normal file

Binary file not shown.

58
test.sh Executable file
View File

@ -0,0 +1,58 @@
#!/bin/bash
# Fonction pour afficher l'aide
usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -h, --help Affiche ce message d'aide"
echo " -f, --file FILE Spécifie le fichier à utiliser"
echo " -v, --verbose Active le mode verbeux"
exit 1
}
# Mapping des options longues vers des options courtes
for arg in "$@"; do
shift
case "$arg" in
"--help") set -- "$@" "-h" ;;
"--file") set -- "$@" "-f" ;;
"--verbose") set -- "$@" "-v" ;;
*) set -- "$@" "$arg"
esac
done
# Utilisation de getopts pour les options courtes
while getopts ":hf:v" opt; do
case ${opt} in
h)
usage
;;
f)
file=$OPTARG
;;
v)
verbose=true
;;
\?)
echo "Option invalide: -$OPTARG" >&2
usage
;;
:)
echo "L'option -$OPTARG requiert un argument." >&2
usage
;;
esac
done
shift $((OPTIND -1))
# 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...

1
titi Normal file
View File

@ -0,0 +1 @@
dfasdfasd