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 ! -f "$NAS_CONFIG" && test ! "0" = "$UID" && echo "No configuration ($NAS_CONFIG) found, log into root:" && su -c $this_file && exit 1
test -f "$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 "-" ; done; printf "\n"; }
# Simply show and perform: Enter to continue
press() { liner;read -p "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 -z $4 && 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 -L "$nas_name" -N | grep Disk|awk '{print $1}')
echo "${tmp_str}"
}
# Ask a question and return 0 if answer is yes, 1 otherwise
ask() { # QUESTION
read -n1 -p " $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 -z $2 && echo "Usage: Mount_NAS NAS-NAME|NAS-IP MOUNT-DIR" && return 1
echo "Moutning $1 to $2"
[ ! -d "$2" ] && sudo mkdir -p "$2"
test ! "root" = "$USER" && sudo chown $USER "$2"
for each in $shares
do test ! -d "$2/$each" && sudo mkdir -p "$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 ${shares} back
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 -L -n
press
fus="samba_share_fusefs"
fusefs=$(getsebool $fus|awk '{print $3}')
echo
echo "Testing: $fus"
echo -e "\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 -e | grep $service || echo -e "\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 [ ! -f "$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 -p "$STR_SMB_IP? ($SMB_DEFAULT_IP): " var_smb_ip
read -p "$STR_SMB_NAME? ($SMB_DEFAULT_NAME): " var_smb_name
read -p "$STR_SMB_USR? ($SMB_DEFAULT_USR): " var_smb_usr
read -p "$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 -p "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 "$entry" in
"$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