<---- template headericclude ----->
Script: Connect and Debug to Samba NAS
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    44
    Posts
    4,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Script: Connect and Debug to Samba NAS

    Heyas

    As some might have recently, i've had some issues to connect to my NAS.
    Figured i made mistakes, but have learned a bit on the road, which the Fedora Community shall benefit from.
    At least i hope its helpful

    Features:
    * Create a credentials file
    * Connect to a single share on NAS, using cli.
    * Mount all directory to local environment
    * Easier debugging, collect data that helps you debugging.
    * Change the default variables in the script, so you can just hit enter for your hardcoded default values -> easier re-usage.


    For best results, and previous to first execution of the script:
    1. mkdir $HOME/bin
    2. chmod +x sea-samba.sh
    3. mv sea-samba.sh /$HOME/bin
    4. su
    5. yum install cifs-utils samba-client
    6. sea-samba


    Once the credentials are created, its quiete easy to use:
    Mounting all shares from NAS to local might look similar to this, whilst using sea-samba.sh mn
    Code:
    		sea's Samba script tool (0.5)
    Moutning 192.168.10.110 to /nas
    Mounting:	//192.168.10.110/priv    to /nas/priv   	 done   
    Mounting:	//192.168.10.110/pub     to /nas/pub    	 done   
    Mounting:	//192.168.10.110/TimeMac to /nas/TimeMachine     done   
    Mounting:	//192.168.10.110/Picture to /nas/Pictures        done   
    Mounting:	//192.168.10.110/Music   to /nas/Music  	 done   
    Mounting:	//192.168.10.110/Movies  to /nas/Movies 	 done   
    Mounting:	//192.168.10.110/Linux   to /nas/Linux  	 done   
    Mounting:	//192.168.10.110/Backups to /nas/Backups         done
    Where the 'main' menu looks like:
    Code:
    Domain=[OGOG] OS=[Unix] Server=[Samba 3.5.6]
    Domain=[OGOG] OS=[Unix] Server=[Samba 3.5.6]
    		sea's Samba script tool (0.5)
    1) Exit
    2) Single connection to 1 share on "ocean"
    3) Mount all shares on "ocean" to "/nas"
    4) Debug 
    5) Edit: /etc/samba/smb.cred
    6) Edit: /etc/samba/smb.conf
    7) Edit: /etc/samba/nas.conf
    #?
    Have to cut out the "help-command"-part as its too large...
    Code:
    for arg in $@
    do 	if [ "h" = "$arg" ] || [ "-h" = "$arg" ] || [ "--h" = "$arg" ] || [ "--help" = "$arg" ]  || [ "-help" = "$arg" ]
    then
    cat << EOF
        There are no arguments required.
        Simply edit the variables in the script and/or
        provide the information to create the configuration
        and credential files.
        
        These files will not only ease the use of this script,
        but are also more secure (credential) than using those
        values as plain text (hard coded) in your scripts.
        
        If you need root access, it is suggested to use a term like:
          *)       su -c /path/to/sea-samba
          *)       su -c sea-samba
        
        To perform action right from the command line use these arguments:
          *)	mn	= 	Mount Nas ; Connects to your NAS,
          				if not root a password is required
          *)	co	= 	Connect to Share
          				Reads the shares on the NAS and ask you to
          				which you want to connect.
        
        If you have any ideas to improve this script, 
        find bugs or a solution for them, feel free to send me an
        email to: < erat.simon AT gmail.com > ... with the subject "samba".
    
    EOF
    fi
    done
    Attached Files Attached Files
    Last edited by sea; 16th May 2012 at 10:55 AM.
    * Shell Wrapper and Runtime Modifier (SWARM): https://github.com/sri-arjuna/SWARM
    * Yet Another Simple Script Installer (YASSI): https://forums.fedoraforum.org/showthread.php?323099
    * EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
    * Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

  2. #2
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    44
    Posts
    4,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Script: Connect and Debug to Samba NAS

    The source code of the script, so you know what you might be downloading
    NOTE: The PHP tag coding is only for better readability

    PHP Code:
    #!/bin/bash
    # |
    # | Samba Connect & Debug Script, 2012.Mai.01 by sea
    # | To create the 'config' files, it needs to be run as root
    # |    That way, the credentials may only be access with sufficant rights
    # | To debug only, it could be run as any user.
    # |
    # | This script is written with best intention to help, 
    # | but there is NO warranty, garanty and I deny ANY responsiblity
    # | that may or may not affect your system.
    # |
    # | Lisence:        GPL v3
    # | Author:         Simon A. Erat (sea)
    # | Release Date:   2012 May 2nd
        
    script_version="0.5"
    # | Resource URL:   http://forums.fedoraforum.org/showthread.php?t=279170
    # | Release URL:    http://forums.fedoraforum.org/showthread.php?p=1573548
    # |
    # | Help
        # code too hughe
    # |
    # | Default Directory & File
        
    MOUNTPOINT=/mnt/nas
        readonly SAMBA_LOGDIR
    =/var/log/samba
        readonly SAMBA_CONF
    =/etc/samba/smb.conf
        readonly SAMBA_CRED
    =/etc/samba/smb.cred
        readonly NAS_CONFIG
    =/etc/samba/nas.conf
        readonly this_file
    =$(basename $0)
        
    #
        # Change your personal default values here if you like
        #
        
    readonly SMB_DEFAULT_IP=192.168.1.10
        readonly SMB_DEFAULT_NAME
    =MYNAS
        readonly SMB_DEFAULT_DOMAIN
    =WORKGROUP
        test 
    root $USER && readonly SMB_DEFAULT_USR=$USER
        
    #
        # Reload the script if no config was found, as the creation of the config requires root
        #
        
    test ! -"$NAS_CONFIG&& test "0" "$UID&& echo "No configuration ($NAS_CONFIG) found, log into root:" && su -c $this_file && exit 1
        test 
    -"$NAS_CONFIG&& source "$NAS_CONFIG"
    # |
    # | Variables
        
    readonly SMBC="$(which smbclient)"
        
    #
        # str_smb is only for the debug config file
        # while smb_str_* will be for the real smb.conf (much, much later, if ever) 
        #
        
    readonly STR_SMB_IP="samba_ip"
        
    readonly STR_SMB_NAME="samba_device_name"
        
    readonly STR_SMB_USR="username"
        
    readonly STR_SMB_DOM="samba_domain"
        
    readonly STR_SMB_PW="password"
        
    #
        # Other variables
        #
        
    TERMINAL_WIDTH=$(tput cols)
        
    MENU_A="Single connection to 1 share on \"$samba_device_name\""
        
    MENU_B="Debug "
        
    MENU_C="Mount all shares on \"$samba_device_name\" to \"$MOUNTPOINT\""
        
    MENU_D="Edit: $SAMBA_CRED"
        
    MENU_E="Edit: $SAMBA_CONF"
        
    MENU_F="Edit: $NAS_CONFIG"
        
        
    MENU_Z="Exit"
    # |
    # | Subs
        # Prints the header menu
        
    smb_header() { printf "\r\t\tsea's Samba script tool ($script_version)\n"; }
        
        
    # Simply print a fixed lenth of dashes
        
    liner() { printf "\r";for ((i=1;i<=$TERMINAL_WIDTH;i++)); do printf "-" doneprintf "\n"; }
        
        
    # Simply show and perform: Enter to continue
        
    press() { liner;read -"Press [ENTER] to continue" buffer; }

        
    # Shows the content of the provided credentials path/file
        
    Show_SMB_cred() { # FILENAME
            
    printf "\r\tSamba Credentials:\t$1\n"
            
    while read line
            
    do      var=$(echo $line|awk '{print $1}')
                    
    val=$(echo $line|awk '{print $3}')
                    
    printf "\r\t$var\t\t$val\n"
            
    done "$1"
        
    }
            
        
    # Write the Samba Credentials file
        
    WriteSMBcred(){ # OUTPUTFILE USER DOMAIN PASSWORD
            
    test -$&& echo "Usuage: WriteSMBcred OUTPUTFILE USER DOMAIN PASSWORD"
            
    printf "username = $2\n"   "$1"
            
    printf "  domain = $3\n"  >> "$1"
            
    printf "password = $4\n"  >> "$1"
        
    }
        
        
    # Write the dummy file so you dont have to provide the same information to the script
        
    WriteDebug() { # OUTPUTFILE IP NAME USER DOMAIN PASSWORD MOUNTPOINT
            
    printf "$STR_SMB_IP=$2\n"    "$1"
            
    printf "$STR_SMB_NAME=$3\n" >> "$1"
            
    printf "$STR_SMB_USR=$4\n"  >> "$1"
            
    printf "$STR_SMB_DOM=$5\n"  >> "$1" 
            
    printf "$STR_SMB_PW=$6\n"   >> "$1" 
            
    printf "MOUNTPOINT=$7\n"    >> "$1" 
        
    }
        
        
    # Lists an array of entries found on NAS
        
    SMB_List_Shares() { # NAS-NAME || NAS-IP
            
    nas_name="$1"
            
    tmp_str=$($SMBC -"$nas_name-grep Disk|awk '{print $1}')
            echo 
    "${tmp_str}"
        
    }
        
        
    # Ask a question and return 0 if answer is yes, 1 otherwise
        
    ask() { # QUESTION
            
    read -n1 -"        $1 (y/n)" answer ;        printf "\n"
            
    case $answer in
                y
    |o|j|s)    # First letter of the meaning "yes" in these languages:
                            # English, Français, Deutsch, Italiano, Espagnol
                            
    return 0    ;;
                *)          return 
    1    ;;
            
    esac
        
    }
        
        
    # Mount the folders on the NAS to local environment
        
    NAS_Mount() { # NAS-NAME|NAS-IP  MOUNT-DIR
            
    test  -$&& echo "Usage: Mount_NAS NAS-NAME|NAS-IP  MOUNT-DIR" && return 1
            
    echo "Moutning $1 to $2"
            
    [ ! -"$2" ] && sudo mkdir -"$2"
            
    test "root" "$USER&& sudo chown $USER "$2"
            
    for each in $shares
            
    do      test ! -"$2/$each&& sudo mkdir -"$2/$each"
                    
    tab="\t\t\t\t"
                    
    str_mount="\rMounting:\t//$1/$each \r\t$tab to $2/$each"
                    
    cmds="mount -t cifs //$1/$each $2/$each -o rw,soft,credentials=$NAS_CONFIG"
                    
    #echo "Running: $cmds"
                    
    printf "$str_mount \r$tab$tab working"
                    
    sudo $cmds && printf "\r$tab$tab done   \n" || printf "\r$tab$tab fail   \n"
                    
    #printf "\n"
                    
    sleep 2
            done
        
    }

        
    # Retrieve a list of folders on the NAS and connect to one of them
        
    SMB_Connect() { # NAS-NAME|NAS-IP
            
    select share in ${sharesback
            
    do  test $share back && break
                
    $SMBC //$samba_device_name/$share -A $SAMBA_CRED
            
    done
        
    }
        
        
    # Shows a bunch of diffrent value
        
    SMB_Debug() { #
            
    echo echo "Testing $samba_ip"
            
    ping -c 1 $samba_ip
            press

            
    echo
            echo 
    "Testing Firewall"
            
    sudo iptables --n
            press

            fus
    ="samba_share_fusefs"
            
    fusefs=$(getsebool $fus|awk '{print $3}')
            echo
            echo 
    "Testing: $fus"
            
    echo -"\tThe SELinux $fus setting is currently\t\t$fusefs"
            
    if ask "Do you want to toggle it?" 
            
    then    fus_toggle="yes"
            
    else    fus_toggle="no"
            
    fi
            
    #echo $fus_toggle
            
    test "$fus_toggle"yes" && test "on" "$fusefs&& sudo setsebool $fus 0 || sudo setsebool $fus 1
            press

            
    echo
            echo 
    "Testing: $SAMBA_CONF using testparm"
            
    test ask "Edit $SAMBA_CONF before testing" && vi "$SAMBA_CONF"
            
    testparm
            press

            
    echo
            echo 
    "List services for Samba server (NOT required for NAS)"
            
    for service in smbd nmbd
            
    do ps -grep $service || echo -"\tService:\t$service\tnot found"
            
    done
            systemd
    -analyze blame|grep smb
            press

            
    echo
            echo 
    "View logfiles in $SAMBA_LOGDIR"
            
    if [ "0" "$UID]
            
    then    logs=$(ls "$SAMBA_LOGDIR-r)
                    if [ ! 
    "" "$logs]
                    
    then    for log in $logs back
                            
    do      test "$log"="back" && break
                                    
    test ask "View log: $logs&& vi "$log"
                            
    done
                    fi
            
    else    echo "You need to be root to view or edit the logs."
            
    fi
            press
        
    }
    # |
    # | Pre-requisites
        
    if [ ! -"$NAS_CONFIG]
        
    then    smb_header
                
    echo "The configuration file for this script was not found at $NAS_CONFIG"
                
    echo "This is required for this script to work, and ease its usage."
                
    read -"$STR_SMB_IP? ($SMB_DEFAULT_IP): "      var_smb_ip
                read 
    -"$STR_SMB_NAME? ($SMB_DEFAULT_NAME): "  var_smb_name
                read 
    -"$STR_SMB_USR? ($SMB_DEFAULT_USR): "    var_smb_usr
                read 
    -"$STR_SMB_DOM? ($SMB_DEFAULT_DOMAIN): " var_smb_dom
                
    #
                # Check for empty variables, and fill with default values
                #
                
    test "" "$var_smb_ip"     && var_smb_ip=$SMB_DEFAULT_IP
                test 
    "" "$var_smb_usr"    && var_smb_usr=$SMB_DEFAULT_USR
                test 
    "" "$var_smb_name"   && var_smb_name=$SMB_DEFAULT_NAME
                test 
    "" "$var_smb_dom"    && var_smb_dom=$SMB_DEFAULT_DOMAIN
                
    #
                # Now write the config file and the credentials
                #
                
    echo "Asking for your password which you had set on the NAS"
                
    echo "Showing the pw as you type to avoid typos, and i dont know another way to handle..."
                
    echo "Leave empty and press enter to change the password later manualy."
                
    read -"Your password: " tmp_pw
                test 
    "" "$tmp_pw&& tmp_pw="PASSWORD"
                
    for each in cred nas
                
    do  case $each in
                    
    "cred")     WriteSMBcred "$SAMBA_CRED"                                "$var_smb_usr" "$var_smb_dom" "$tmp_pw"           ;;
                    
    "nas")      WriteDebug   "$NAS_CONFIG" "$var_smb_ip"  "$var_smb_name" "$var_smb_usr" "$var_smb_dom" "$tmp_pw$MOUNTPOINT   ;;  
                    
    esac
                done
                
    echo "$var_smb_ip        $var_smb_name>> /etc/hosts
                source 
    "$NAS_CONFIG"
        
    else    source "$NAS_CONFIG"
        
    fi
        shares
    =$(SMB_List_Shares "$samba_device_name")  
    # |
    # | Output
        
    smb_header
        
    for arg in $@
        do     case 
    $arg in
                mn
    )    NAS_Mount   "$samba_ip" "$MOUNTPOINT"
                    
    exit ;;
                
    co)    SMB_Connect "$samba_device_name"
                    
    exit ;;
            
    esac
        done
        select entry in 
    "$MENU_Z" "$MENU_A" "$MENU_C" "$MENU_B" "$MENU_D" "$MENU_E" "$MENU_F
        
    do  case "$entryin
            
    "$MENU_A")  SMB_Connect "$samba_device_name"        press smb_header ;;
            
    "$MENU_B")  SMB_Debug                               press smb_header ;;
            
    "$MENU_C")  NAS_Mount   "$samba_ip" "$MOUNTPOINT"      press smb_header ;;
            
    "$MENU_D")  sudo vi "$SAMBA_CRED"                    press smb_header ;;
            
    "$MENU_E")  sudo vi "$SAMBA_CONF"                   press smb_header ;;
            
    "$MENU_F")  sudo vi "$NAS_CONFIG"                   press smb_header ;;
            
    "$MENU_Z")  break ;;
            
    esac
        done 
    Last edited by sea; 16th May 2012 at 10:59 AM.
    * Shell Wrapper and Runtime Modifier (SWARM): https://github.com/sri-arjuna/SWARM
    * Yet Another Simple Script Installer (YASSI): https://forums.fedoraforum.org/showthread.php?323099
    * EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
    * Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

  3. #3
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    44
    Posts
    4,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Script: Connect and Debug to Samba NAS

    Update:
    Reduced script size, at least it fits into 1 post
    Fixed wrong variable assignment while writing config file.
    Mounting all shares from nas to local, fixed permission issue.

    As always, i hope this helps someone.
    * Shell Wrapper and Runtime Modifier (SWARM): https://github.com/sri-arjuna/SWARM
    * Yet Another Simple Script Installer (YASSI): https://forums.fedoraforum.org/showthread.php?323099
    * EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
    * Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

  4. #4
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    44
    Posts
    4,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Script: Connect and Debug to Samba NAS

    Since i have added argument support, the help code area became too hughe, thus had to cut it out for pasting..
    It is available in the attached file anyhow.

    Fixed some errors, such as one may toggle now: samba_share_fs
    Made the script using the IP rather than the NAS name, to reduce chances of conflicts (or missing settings).

    Arguments that may be passed now:
    mn = mount nas
    co = connect to a share, it lists the available shares to let you choose from
    * Shell Wrapper and Runtime Modifier (SWARM): https://github.com/sri-arjuna/SWARM
    * Yet Another Simple Script Installer (YASSI): https://forums.fedoraforum.org/showthread.php?323099
    * EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
    * Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

Similar Threads

  1. Simple script problem OR need to debug WPA?
    By Dunhausen in forum Using Fedora
    Replies: 0
    Last Post: 17th December 2006, 12:00 PM
  2. cannot connect to mysql with php script?
    By shams in forum Using Fedora
    Replies: 3
    Last Post: 29th April 2006, 02:02 AM
  3. MYSQL won't let me connect from PHP script
    By Sail@ in forum Servers & Networking
    Replies: 13
    Last Post: 16th October 2004, 01:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
[[template footer(Guest)]]