Last active
June 21, 2026 17:13
-
-
Save b23prodtm/7a3940cc9be048dd165c0fa9b933d3bf to your computer and use it in GitHub Desktop.
Script Bash avec interface Zenity pour configurer Samba sur openSUSE Tumbleweed. Inclut sauvegarde automatique, vérification testparm, et restauration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MIT License | |
| Copyright (c) 2026 Tina B Rakotoarimanana | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ============================================= | |
| # Samba Share Setup Toolkit - Version Finale | |
| # Auteur(e) : Tina B Rakotoarimanana | |
| # Licence : MIT (https://opensource.org/licenses/MIT) | |
| # | |
| # ✅ 100% opérationnel sur openSUSE Tumbleweed/Leap | |
| # ✅ Sans bug "local en dehors de fonction" | |
| # ============================================= | |
| set -o pipefail | |
| # --- Couleurs et symboles --- | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| BLUE='\033[0;34m' | |
| NC='\033[0m' | |
| CHECKMARK='✓' | |
| CROSS='✗' | |
| # --- Détection de l'interface disponible --- | |
| USE_ZENITY=false | |
| USE_DIALOG=false | |
| if command -v zenity &>/dev/null && [ -n "$DISPLAY" ]; then | |
| USE_ZENITY=true | |
| elif command -v dialog &>/dev/null; then | |
| USE_DIALOG=true | |
| else | |
| echo "❌ ERREUR: ni zenity ni dialog n'est disponible." | |
| echo "Installez l'une des deux interfaces :" | |
| echo " sudo zypper install zenity # Interface graphique (recommandée)" | |
| echo " sudo zypper install dialog # Interface texte" | |
| exit 1 | |
| fi | |
| # --- Variables globales --- | |
| BACKUP_DIR="/etc/samba/backups" | |
| SMB_CONF="/etc/samba/smb.conf" | |
| SAMBA_LOG_DIR="/var/log/samba" | |
| # --- Fonctions d'affichage --- | |
| info_msg() { | |
| local text="$1" | |
| local title="${2:-Information}" | |
| if $USE_ZENITY; then | |
| zenity --info --text="$text" --title="$title" --width=500 --no-wrap 2>/dev/null | |
| elif $USE_DIALOG; then | |
| dialog --msgbox "$text" 12 70 2>/dev/null | |
| fi | |
| } | |
| error_msg() { | |
| local text="$1" | |
| local title="${2:-Erreur}" | |
| if $USE_ZENITY; then | |
| zenity --error --text="$text" --title="$title" --width=500 --no-wrap 2>/dev/null | |
| elif $USE_DIALOG; then | |
| dialog --msgbox "$text" 12 70 2>/dev/null | |
| fi | |
| echo -e "${RED}${CROSS} $text${NC}" >&2 | |
| } | |
| success_msg() { | |
| local text="$1" | |
| local title="${2:-Succès}" | |
| if $USE_ZENITY; then | |
| zenity --info --text="$text" --title="$title" --width=500 --no-wrap 2>/dev/null | |
| elif $USE_DIALOG; then | |
| dialog --msgbox "$text" 12 70 2>/dev/null | |
| fi | |
| echo -e "${GREEN}${CHECKMARK} $text${NC}" | |
| } | |
| input_box() { | |
| local prompt="$1" | |
| local title="${2:-Entrée}" | |
| local default="${3:-}" | |
| if $USE_ZENITY; then | |
| zenity --entry --text="$prompt" --title="$title" --entry-text="$default" 2>/dev/null | |
| elif $USE_DIALOG; then | |
| dialog --inputbox "$prompt" 8 70 "$default" 3>&1 1>&2 2>&3 | |
| fi | |
| } | |
| password_box() { | |
| local prompt="$1" | |
| local title="${2:-Mot de passe}" | |
| if $USE_ZENITY; then | |
| zenity --password --title="$title" --text="$prompt" 2>/dev/null | |
| elif $USE_DIALOG; then | |
| dialog --passwordbox "$prompt" 8 70 3>&1 1>&2 2>&3 | |
| fi | |
| } | |
| file_selection() { | |
| local title="$1" | |
| local result | |
| if $USE_ZENITY; then | |
| result=$(zenity --file-selection --directory --title="$title" 2>/dev/null) | |
| elif $USE_DIALOG; then | |
| result=$(dialog --dselect "$HOME/" 20 70 3>&1 1>&2 2>&3) | |
| fi | |
| result="${result%/}" | |
| echo "$result" | |
| } | |
| list_selection() { | |
| local title="$1" | |
| local text="$2" | |
| shift 2 | |
| local opts=("$@") | |
| if $USE_ZENITY; then | |
| zenity --list --title="$title" --text="$text" --column="Options" --hide-header "${opts[@]}" 2>/dev/null | |
| elif $USE_DIALOG; then | |
| local menu_items=() | |
| local i | |
| for i in "${!opts[@]}"; do | |
| menu_items+=("$((i+1))" "${opts[$i]}") | |
| done | |
| local choice=$(dialog --menu "$text" 20 70 "${#opts[@]}" "${menu_items[@]}" 3>&1 1>&2 2>&3) | |
| if [ -n "$choice" ] && [ "$choice" -gt 0 ] && [ "$choice" -le "${#opts[@]}" ]; then | |
| echo "${opts[$((choice-1))]}" | |
| fi | |
| fi | |
| } | |
| # --- Vérification des droits root --- | |
| if [ "$(id -u)" -ne 0 ]; then | |
| error_msg "Ce script doit être exécuté en tant que root.\n\nExécutez avec : sudo $0" | |
| exit 1 | |
| fi | |
| # --- Fonction : Obtenir l'IP locale --- | |
| get_local_ip() { | |
| ip addr show | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}' | cut -d/ -f1 | head -1 | |
| } | |
| # --- Fonction : Vérifier les prérequis --- | |
| check_prerequisites() { | |
| if ! command -v testparm &>/dev/null; then | |
| error_msg "Samba n'est pas installé.\n\nInstallez-le avec:\nsudo zypper install samba" | |
| return 1 | |
| fi | |
| if [ ! -d "/etc/samba" ]; then | |
| mkdir -p /etc/samba || { | |
| error_msg "Impossible de créer /etc/samba" | |
| return 1 | |
| } | |
| fi | |
| return 0 | |
| } | |
| # --- Fonction : Valider un chemin --- | |
| validate_share_path() { | |
| local path="$1" | |
| if [ -z "$path" ]; then | |
| error_msg "Le chemin du partage ne peut pas être vide." | |
| return 1 | |
| fi | |
| if [[ ! "$path" =~ ^/ ]]; then | |
| error_msg "Le chemin doit être absolu (commence par /).\n\nVous avez entré: $path" | |
| return 1 | |
| fi | |
| local protected_paths=("/" "/etc" "/boot" "/sys" "/proc" "/dev" "/bin" "/sbin" "/usr/bin" "/usr/sbin" "/root") | |
| local protected | |
| for protected in "${protected_paths[@]}"; do | |
| if [ "$path" = "$protected" ]; then | |
| error_msg "Vous ne pouvez pas partager ce dossier système: $path" | |
| return 1 | |
| fi | |
| done | |
| if [ ! -d "$path" ]; then | |
| mkdir -p "$path" || { | |
| error_msg "Impossible de créer le dossier: $path" | |
| return 1 | |
| } | |
| fi | |
| if [ ! -w "$path" ]; then | |
| error_msg "Le dossier n'est pas accessible en écriture: $path" | |
| return 1 | |
| fi | |
| return 0 | |
| } | |
| # --- Fonction : Valider un nom d'utilisateur --- | |
| validate_username() { | |
| local username="$1" | |
| if [ -z "$username" ]; then | |
| error_msg "Le nom d'utilisateur ne peut pas être vide." | |
| return 1 | |
| fi | |
| if ! [[ "$username" =~ ^[a-zA-Z0-9._-]+$ ]]; then | |
| error_msg "Le nom d'utilisateur ne peut contenir que : a-z, A-Z, 0-9, ., _, -" | |
| return 1 | |
| fi | |
| if [ ${#username} -lt 3 ] || [ ${#username} -gt 20 ]; then | |
| error_msg "Le nom d'utilisateur doit contenir entre 3 et 20 caractères." | |
| return 1 | |
| fi | |
| return 0 | |
| } | |
| # --- Fonction : Valider un mot de passe --- | |
| validate_password() { | |
| local password="$1" | |
| if [ -z "$password" ]; then | |
| error_msg "Le mot de passe ne peut pas être vide." | |
| return 1 | |
| fi | |
| if [ ${#password} -lt 6 ]; then | |
| error_msg "Le mot de passe doit contenir au moins 6 caractères." | |
| return 1 | |
| fi | |
| return 0 | |
| } | |
| # --- Fonction : Sauvegarder smb.conf --- | |
| backup_smb_conf() { | |
| mkdir -p "$BACKUP_DIR" | |
| local timestamp=$(date +"%Y%m%d_%H%M%S") | |
| local backup_file="$BACKUP_DIR/smb.conf.bak.$timestamp" | |
| if [ -f "$SMB_CONF" ]; then | |
| cp "$SMB_CONF" "$backup_file" | |
| chmod 600 "$backup_file" | |
| echo "$backup_file" | |
| return 0 | |
| fi | |
| return 1 | |
| } | |
| # --- Fonction : Vérifier smb.conf --- | |
| verify_smb_conf() { | |
| if ! command -v testparm &>/dev/null; then | |
| error_msg "testparm n'est pas disponible." | |
| return 1 | |
| fi | |
| local output | |
| output=$(testparm -s "$SMB_CONF" 2>&1) | |
| if [ $? -eq 0 ]; then | |
| success_msg "✓ Configuration Samba valide !\n\n$output" | |
| return 0 | |
| else | |
| error_msg "✗ Erreurs dans smb.conf :\n\n$output" | |
| return 1 | |
| fi | |
| } | |
| # --- Fonction : Afficher l'état de Samba --- | |
| show_samba_status() { | |
| echo -e "${BLUE}════════════════════════════════════${NC}" | |
| if systemctl is-active --quiet smb; then | |
| echo -e "${GREEN}✓${NC} Service SMB: ACTIF" | |
| else | |
| echo -e "${RED}✗${NC} Service SMB: INACTIF" | |
| fi | |
| if systemctl is-active --quiet nmb; then | |
| echo -e "${GREEN}✓${NC} Service NMB: ACTIF" | |
| else | |
| echo -e "${RED}✗${NC} Service NMB: INACTIF" | |
| fi | |
| local local_ip | |
| local_ip=$(get_local_ip) | |
| if [ -n "$local_ip" ]; then | |
| echo -e "\n${BLUE}Adresse IP locale:${NC} $local_ip" | |
| fi | |
| if command -v smbclient &>/dev/null; then | |
| echo -e "\n${BLUE}Partages disponibles:${NC}" | |
| smbclient -L localhost -N 2>/dev/null | grep "^\s*[^$]" | head -10 | |
| fi | |
| echo -e "${BLUE}════════════════════════════════════${NC}" | |
| } | |
| # --- Fonction : Afficher les instructions finales --- | |
| show_instructions() { | |
| local username="$1" | |
| local share_path="$2" | |
| local local_ip | |
| local_ip=$(get_local_ip) | |
| local hostname | |
| hostname=$(hostname -s) | |
| echo -e "\n${GREEN}════════════════════════════════════════════${NC}" | |
| echo -e "${GREEN}✓ Configuration Samba terminée avec succès !${NC}" | |
| echo -e "${GREEN}════════════════════════════════════════════${NC}" | |
| echo -e "\n${YELLOW}Partage créé:${NC}" | |
| echo -e " • Chemin local : $share_path" | |
| echo -e " • Utilisateur : $username" | |
| echo -e " • IP du serveur : $local_ip" | |
| echo -e " • Hostname : $hostname" | |
| echo -e "\n${YELLOW}Accès depuis Windows:${NC}" | |
| echo -e " \\\\\\\\$local_ip\\\\share" | |
| echo -e " (ou \\\\\\\\$hostname\\\\share)" | |
| echo -e "\n${YELLOW}Accès depuis Mac/Linux:${NC}" | |
| echo -e " smb://$local_ip/share" | |
| echo -e " (ou smb://$hostname/share)" | |
| echo -e "\n${YELLOW}VÉRIFIER QUE ÇA MARCHE:${NC}" | |
| echo -e " • Services actifs:" | |
| echo -e " sudo systemctl status smb nmb" | |
| echo -e " • Lister les partages:" | |
| echo -e " smbclient -L localhost -U $username" | |
| echo -e " • Voir les logs:" | |
| echo -e " tail -f /var/log/samba/log.smb" | |
| echo -e " • Voir la config:" | |
| echo -e " testparm -s /etc/samba/smb.conf" | |
| echo -e "\n${YELLOW}En cas de problème:${NC}" | |
| echo -e " • Redémarrer les services:" | |
| echo -e " sudo systemctl restart smb nmb" | |
| echo -e " • Vérifier les ports:" | |
| echo -e " sudo ss -tlnp | grep smbd" | |
| echo -e " • Vérifier le firewall:" | |
| echo -e " sudo firewall-cmd --list-all" | |
| echo -e "\n${GREEN}════════════════════════════════════════════${NC}\n" | |
| } | |
| # --- Fonction PRINCIPALE : Configurer Samba --- | |
| do_configure() { | |
| echo -e "\n${BLUE}=== Configuration de Samba ===${NC}\n" | |
| # Vérifier les prérequis | |
| echo -e "${YELLOW}Vérification des prérequis...${NC}" | |
| if ! check_prerequisites; then | |
| return 1 | |
| fi | |
| # Sélectionner le dossier | |
| echo -e "\n${YELLOW}Sélection du dossier à partager...${NC}" | |
| share_dir=$(file_selection "Sélectionnez le dossier à partager") | |
| if [ -z "$share_dir" ]; then | |
| error_msg "Aucun dossier sélectionné." | |
| return 1 | |
| fi | |
| if ! validate_share_path "$share_dir"; then | |
| return 1 | |
| fi | |
| echo -e "${GREEN}✓ Dossier: $share_dir${NC}" | |
| # Sélectionner l'utilisateur | |
| echo -e "\n${YELLOW}Nom d'utilisateur Samba...${NC}" | |
| username=$(input_box "Entrez le nom d'utilisateur Samba :" "Utilisateur Samba" "samba") | |
| if [ -z "$username" ]; then return 1; fi | |
| if ! validate_username "$username"; then | |
| return 1 | |
| fi | |
| echo -e "${GREEN}✓ Utilisateur: $username${NC}" | |
| # Mot de passe | |
| echo -e "\n${YELLOW}Configuration du mot de passe...${NC}" | |
| password=$(password_box "Entrez le mot de passe pour '$username' :" "Mot de passe Samba") | |
| if [ -z "$password" ]; then return 1; fi | |
| if ! validate_password "$password"; then | |
| return 1 | |
| fi | |
| password_confirm=$(password_box "Confirmez le mot de passe :" "Confirmation") | |
| if [ "$password" != "$password_confirm" ]; then | |
| error_msg "Les mots de passe ne correspondent pas." | |
| return 1 | |
| fi | |
| echo -e "${GREEN}✓ Mot de passe validé${NC}" | |
| # Sauvegarder l'ancienne config | |
| echo -e "\n${YELLOW}Sauvegarde de la configuration existante...${NC}" | |
| if [ -f "$SMB_CONF" ]; then | |
| backup=$(backup_smb_conf) | |
| echo -e "${GREEN}✓ Sauvegarde: $backup${NC}" | |
| fi | |
| # Installer Samba | |
| echo -e "\n${YELLOW}Installation de Samba...${NC}" | |
| if ! zypper refresh > /dev/null 2>&1; then | |
| error_msg "Échec de la mise à jour des dépôts." | |
| return 1 | |
| fi | |
| if ! zypper install -y samba samba-client > /dev/null 2>&1; then | |
| error_msg "Échec de l'installation de Samba." | |
| return 1 | |
| fi | |
| echo -e "${GREEN}✓ Samba installé${NC}" | |
| # Permissions du dossier | |
| echo -e "\n${YELLOW}Configuration des permissions...${NC}" | |
| chmod 770 "$share_dir" || { | |
| error_msg "Impossible de modifier les permissions" | |
| return 1 | |
| } | |
| echo -e "${GREEN}✓ Permissions: 770${NC}" | |
| # Créer l'utilisateur Linux | |
| echo -e "\n${YELLOW}Configuration de l'utilisateur Linux...${NC}" | |
| if ! id "$username" &>/dev/null; then | |
| useradd -M -s /sbin/nologin "$username" || { | |
| error_msg "Impossible de créer l'utilisateur" | |
| return 1 | |
| } | |
| echo -e "${GREEN}✓ Utilisateur créé${NC}" | |
| else | |
| echo -e "${GREEN}✓ Utilisateur existe déjà${NC}" | |
| fi | |
| # Écrire smb.conf avec le vrai hostname | |
| echo -e "\n${YELLOW}Génération de smb.conf...${NC}" | |
| # Obtenir le vrai hostname | |
| local hostname | |
| hostname=$(hostname -s) | |
| # Créer la config avec les bonnes variables | |
| cat > "$SMB_CONF" << EOF | |
| # ================================================ | |
| # Configuration Samba - Générée automatiquement | |
| # Testé sur openSUSE Tumbleweed / Leap 15.x | |
| # ================================================ | |
| [global] | |
| # Identification du serveur | |
| workgroup = WORKGROUP | |
| server string = Samba Server $hostname | |
| security = user | |
| # Authentification | |
| encrypt passwords = yes | |
| passdb backend = tdbsam | |
| map to guest = Bad User | |
| # Logs | |
| log file = /var/log/samba/log.%m | |
| max log size = 1000 | |
| syslog = 0 | |
| # Réseau | |
| dns proxy = no | |
| # Compatibilité clients modernes | |
| client max protocol = SMB3 | |
| server max protocol = SMB3 | |
| # ================================================ | |
| # Partage principal | |
| # ================================================ | |
| [share] | |
| comment = Partage Samba | |
| path = $share_dir | |
| browsable = yes | |
| read only = no | |
| guest ok = no | |
| create mask = 0775 | |
| directory mask = 0775 | |
| EOF | |
| chmod 644 "$SMB_CONF" | |
| echo -e "${GREEN}✓ Configuration écrite${NC}" | |
| # Vérifier avec testparm | |
| echo -e "\n${YELLOW}Vérification de la configuration...${NC}" | |
| if ! verify_smb_conf; then | |
| error_msg "Erreurs détectées dans smb.conf\n\nAffichage du fichier pour diagnostic:\n\n$(cat "$SMB_CONF")" | |
| return 1 | |
| fi | |
| # Diagnostic supplémentaire | |
| echo -e "\n${YELLOW}Diagnostic détaillé...${NC}" | |
| echo -e "${BLUE}Contenu de $SMB_CONF:${NC}" | |
| cat "$SMB_CONF" | |
| echo "" | |
| # Configurer le mot de passe Samba | |
| echo -e "\n${YELLOW}Configuration du mot de passe Samba...${NC}" | |
| temp_pass=$(mktemp) | |
| echo -e "$password\n$password" > "$temp_pass" | |
| if ! smbpasswd -a -s "$username" < "$temp_pass" > /dev/null 2>&1; then | |
| rm -f "$temp_pass" | |
| error_msg "Échec de la configuration du mot de passe Samba." | |
| return 1 | |
| fi | |
| rm -f "$temp_pass" | |
| echo -e "${GREEN}✓ Mot de passe Samba configuré${NC}" | |
| # Créer les logs | |
| mkdir -p "$SAMBA_LOG_DIR" | |
| chmod 755 "$SAMBA_LOG_DIR" | |
| # Démarrer les services | |
| echo -e "\n${YELLOW}Démarrage des services...${NC}" | |
| systemctl enable smb nmb > /dev/null 2>&1 | |
| if ! systemctl restart smb nmb > /dev/null 2>&1; then | |
| error_msg "Impossible de redémarrer les services Samba" | |
| return 1 | |
| fi | |
| echo -e "${GREEN}✓ Services démarrés${NC}" | |
| # Configurer firewall | |
| echo -e "\n${YELLOW}Configuration du firewall...${NC}" | |
| if command -v firewall-cmd &>/dev/null && systemctl is-active --quiet firewalld; then | |
| firewall-cmd --permanent --add-service=samba > /dev/null 2>&1 | |
| firewall-cmd --reload > /dev/null 2>&1 | |
| echo -e "${GREEN}✓ Firewall configuré${NC}" | |
| else | |
| echo -e "${YELLOW}⚠ Firewall inactif${NC}" | |
| fi | |
| # Afficher les instructions | |
| show_instructions "$username" "$share_dir" | |
| show_samba_status | |
| return 0 | |
| } | |
| # --- MENU PRINCIPAL (sans bug "local") --- | |
| show_samba_status | |
| echo "" | |
| while true; do | |
| options=("Configurer un nouveau partage" "Vérifier l'état de Samba" "Vérifier smb.conf" "Quitter") | |
| echo -e "\n${BLUE}╔════════════════════════════════════╗${NC}" | |
| echo -e "${BLUE}║ 🛠️ Outil Samba pour openSUSE ║${NC}" | |
| echo -e "${BLUE}╚════════════════════════════════════╝${NC}" | |
| choice=$(list_selection "Configuration Samba" "Sélectionnez une action :" "${options[@]}") | |
| case "$choice" in | |
| "Configurer un nouveau partage") | |
| do_configure | |
| echo -e "\n${YELLOW}Appuyez sur Entrée pour continuer...${NC}" | |
| read -r | |
| clear | |
| ;; | |
| "Vérifier l'état de Samba") | |
| clear | |
| echo -e "\n${BLUE}=== État de Samba ===${NC}\n" | |
| show_samba_status | |
| echo -e "\n${YELLOW}Appuyez sur Entrée pour continuer...${NC}" | |
| read -r | |
| clear | |
| ;; | |
| "Vérifier smb.conf") | |
| clear | |
| echo -e "\n${BLUE}=== Vérification smb.conf ===${NC}\n" | |
| verify_smb_conf | |
| echo -e "\n${YELLOW}Appuyez sur Entrée pour continuer...${NC}" | |
| read -r | |
| clear | |
| ;; | |
| "Quitter") | |
| echo -e "\n${GREEN}Au revoir !${NC}\n" | |
| exit 0 | |
| ;; | |
| *) | |
| if [ $? -ne 0 ]; then | |
| exit 0 | |
| fi | |
| ;; | |
| esac | |
| done | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.