commit af274aea3e7153312160f68c0e47d48d34cc7088 Author: Gilles Mouchet Date: Sun Jun 2 12:14:06 2024 +0200 pretest diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5ef2eb2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "editor.fontSize": 14, + "terminal.integrated.fontSize": 14, + "window.zoomLevel": 1.4, +} \ No newline at end of file diff --git a/gest-hosts.sh b/gest-hosts.sh new file mode 100755 index 0000000..f7af4aa --- /dev/null +++ b/gest-hosts.sh @@ -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 \ No newline at end of file diff --git a/hosts.sqlite b/hosts.sqlite new file mode 100644 index 0000000..299ccb5 Binary files /dev/null and b/hosts.sqlite differ diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..a58de94 --- /dev/null +++ b/test.sh @@ -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... + diff --git a/titi b/titi new file mode 100644 index 0000000..43ffc49 --- /dev/null +++ b/titi @@ -0,0 +1 @@ +dfasdfasd \ No newline at end of file