06-07-2015, 11:16 AM
Hm, this adds some dependancy but the folder selection dialog is limited.
Can we use this Jerry?
Can we use this Jerry?
Code:
#!/bin/bash
#Samba share settings GUI for Linux Lite
#Licence GPLv2
#Run with: /usr/bin/su-to-root -X -c /path/to/cnss-gui
#Depends: python3-tk zenity
#
#Check if python3-tk is installed
tk=`python3 <<END
'''Tries to import tkinter'''
try:
import tkinter
from tkinter import filedialog
print ("Y")
except ImportError:
print ("N")
END`
case $tk in
N) zenity --error --text="Package python3-tk is missing\nPlease install it.\napt-get install python3-tk"
exit 1
;;
# Y) echo "python3-tk found" ;;
esac
TITLE="Lite Shares"
function first_time() {
if ! zenity --question --title="$TITLE" --text="First time setup should be only runned once!\nDo you want to continue?"; then
return
fi
#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))
#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --print-column=2 --column="Select" --column="Username" --width=325 --height=300 "${LL_LIST_H[@]}")"
# Cancel
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
return
fi
#Exit variable for the return status from the smbpasswd that will be runing in the subshell
exit_status=$(mktemp /tmp/XXXXXX)
echo -e "$(zenity --forms --title="$TITLE" --text="Enter the password twice:" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )" | (smbpasswd -a -s $USER || echo 1 >> $exit_status)
#If exit satus is 1, passwords are mismatched or no password was typed
if [ "$(cat $exit_status )" == "1" ]; then
zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
rm -f $exit_status
unset LL_LIST_H
return
fi
#Changes default user to the username selected
echo '<'$user'> = "<'$user'>"' > /etc/samba/smbusers
unset LL_LIST_H
rm -f $exit_status
zenity --question --title "$TITLE" --text "Your current /etc/samba/smb.conf will be backed up,
\n \nDo you want to continue?"
if [ $? = 1 ];
then
return
fi
#Copies /etc/samba/smb.conf to /etc/samba/smb.conf.backup
cp -f /etc/samba/smb.conf /etc/samba/smb.conf.backup
#Removes original
rm -f /etc/samba/smb.conf
#Creates new empty file
touch /etc/samba/smb.conf
#Workgroup selection dialog. On cancel selects the default value
group=$(zenity --title="$TITLE" --entry --entry-text=WORKGROUP --text="Enter the name of the workgroup:" --cancel-label="Use Default")
if [ $? = 1 ]; then
group="WORKGROUP"
fi
#Hostname selection dialog. On cancel selects the default value
hostname=$(zenity --title="$TITLE" --entry-text=lite --entry --text="Enter the name of your computer (hostname):" --cancel-label="Use Default")
if [ $? = 1 ]; then
hostname="lite"
fi
#Writing to smb.conf starts here
echo "#======================= Global Settings ====================================" >> /etc/samba/smb.conf
echo [global] >> /etc/samba/smb.conf
echo workgroup = "$group" >> /etc/samba/smb.conf
echo server string = Linux Lite Shares >> /etc/samba/smb.conf
echo netbios name = "$hostname" >> /etc/samba/smb.conf
echo "security = user" >> /etc/samba/smb.conf
echo "encrypt passwords = true" >> /etc/samba/smb.conf
echo "username map = /etc/samba/smbusers" >> /etc/samba/smb.conf
echo "map to guest = bad user" >> /etc/samba/smb.conf
echo "guest account = nobody" >> /etc/samba/smb.conf
echo "dns proxy = no" >> /etc/samba/smb.conf
echo "#======================= Share Definitions ===================================" >> /etc/samba/smb.conf
if zenity --question --title="$TITLE" --text="Do you want to add a share?"; then
add_share
else
restart_network
fi
restart_network
}
function add_user() {
#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))
#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --column="Select" --column="Username" --width=325 --height=300 "${LL_LIST_H[@]}")"
# Cancel
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
return
fi
#Exit variable for the return status from the smbpasswd that will be runing in the subshell
exit_status=$(mktemp /tmp/XXXXXX)
echo -e "$(zenity --forms --title="Enter Password" --text="Enter the password twice:" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )" | (smbpasswd -a -s $user || echo 1 >> $exit_status)
if [ "$(cat $exit_status )" == "1" ]; then
zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
rm -f $exit_status
unset LL_LIST_H
return
fi
rm -f $exit_status
#Adds user to the list in /etc/samba/smbusers
echo '<'$user'> = "<'$user'>"' >> /etc/samba/smbusers
unset LL_LIST_H
return
}
#Function that adds share
function add_share() {
#Share description input
share=$(zenity --title="$TITLE" --entry --text="Enter a short name for the share with no blank spaces in the name. \n Examples: music or my-music ")
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
return
fi
#If the user entered special chars, abort
if [[ "$share" =~ [^0-9a-z-] ]]; then
zenity --warning \
--title="" --text="Share name can contain alphanumeric characters only\nShare can not be added!"
return
fi
cd /home
#Path selection directory for the share
path=`python3 <<END
import tkinter
from tkinter import filedialog
root = tkinter.Tk()
root.withdraw()
dirname = filedialog.askdirectory(parent=root,initialdir="/home",title='Pick a directory')
print (dirname)
END`
#Check if the output is a path
echo $path | grep "/"
if [ $? -ne 0 ]; then
zenity --error --text="You must select a valid directory"
return
fi
#Check if the directory exists
if [ ! -d $path ]; then
zenity --error --text="You must select a valid directory"
return
fi
# Starts making changes after user input
# Marks the begining of share for easy handling
echo "#share_start $share" >> /etc/samba/smb.conf
echo ["$share"] >> /etc/samba/smb.conf
echo path = "$path" >> /etc/samba/smb.conf
echo "available = yes" >> /etc/samba/smb.conf
echo "valid users = %U %G" >> /etc/samba/smb.conf
echo "write list = %U" >> /etc/samba/smb.conf
echo browsable = yes >> /etc/samba/smb.conf
echo public = no >> /etc/samba/smb.conf
if zenity --question --title="$TITLE" --text="Do you want to allow recording and change on the files?"; then
echo writable = yes >> /etc/samba/smb.conf
else
echo writable = no >> /etc/samba/smb.conf
fi
echo "guest ok = no" >> /etc/samba/smb.conf
echo "read only = no" >> /etc/samba/smb.conf
echo "printable = no" >> /etc/samba/smb.conf
echo "locking = no" >> /etc/samba/smb.conf
echo "strict locking = no" >> /etc/samba/smb.conf
# Marks the end of share for easy handling
echo "#share_end $share" >> /etc/samba/smb.conf
return
}
function restore_prev() {
#If the file exists offers to restore previous network share settings
if [ -f /etc/samba/smb.conf.backup ]; then
if zenity --question --title="$TITLE" --text="This will restore your previous network share settings.\n\nIf you proceed your current settings will be removed.\n\nDo you want to continue?"; then
rm -f /etc/samba/smb.conf
mv -f /etc/samba/smb.conf.backup /etc/samba/smb.conf
return
else
return
fi
else
zenity --info --title="$TITLE" --text="No previous settings found."
return
fi
}
function restart_network() {
#Restarts network services
stdbuf -oL /bin/bash \-c '(sudo service smbd restart && sudo service nmbd restart && sleep 3 )' 2>&1 |
stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' | #sed 's/^\(.\{128\}\).*/\1/' Display only the first 128 characters
zenity --progress --title="Restarting Network Share Services..." --pulsate \
--width=600 --auto-close
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
zenity --error \
--title="Error" --text="$CONFIGURE has failed."
return
fi
PROCEED=$(zenity --info --title="$CONFIGURE" --window-icon=/usr/share/icons/zenity-llcc.png --width="280" --text="Your settings have been applied."; echo $?)
if [ ${PROCEED} -eq 1 ]; then
zenity --info --title="Finished" --window-icon="${INSTALL_ICON}" --text="Your settings have been applied."
return;
else
return
fi
}
function rem_share() {
#Check if the share doesn't exists
if [[ -z "$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf)" ]];then
zenity --warning --title="$TITLE" --text="Nothing was found for removal\n#share_start not found!"
return
fi
#Share names selection
sharename=$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf | sed 's/\[/FALSE\n/g;s/\]//g' | zenity --list --radiolist --column=Select --column="Share Name" --text="Select the shares you whish to remove.")
if [ $? = 1 ]; then
return
fi
if [ -z $sharename ]; then
zenity --warning --title="$TITLE" --text="Nothing was selected for removal!"
return
fi
#remove shares
for k in $sharename; do sudo sed -i '/^\#share_start '$k'/,/^\#share_end '$k'/d' /etc/samba/smb.conf; done
return
}
while (true); do
CHOICE=$(zenity --list --radiolist --title="$TITLE" --width=350 --height=500 --column=Select --column=Task \
FALSE "Run first time setup" \
TRUE "Show Shares" \
FALSE "Add a User" \
FALSE "Add a Share" \
FALSE "Remove a Share" \
FALSE "Edit the config file" \
FALSE "Restart Network Services" \
FALSE "Restore Previous Settings" \
FALSE "Exit" )
#If canceled exit
if [ $? = 1 ]; then
exit 0
fi
case $CHOICE in
"Run first time setup") first_time
;;
"Show Shares") zenity --text-info --width=500 --height=600 --title="Show Samba Shares" </etc/samba/smb.conf
;;
"Add a User" )add_user
;;
"Add a Share") add_share
;;
"Remove a Share") rem_share
;;
"Edit the config file" ) exo-open /etc/samba/smb.conf
;;
"Restart Network Services") restart_network
;;
"Restore Previous Settings") restore_prev
;;
"Exit") exit 0
;;
esac
done
exit 0

![[Image: 5ZWpRu4.png]](http://i.imgur.com/5ZWpRu4.png)
![[Image: 4vIbaoT.png]](http://i.imgur.com/4vIbaoT.png)
![[Image: x5ymB9A.png]](http://i.imgur.com/x5ymB9A.png)
![[Image: X5qGkCg.png]](https://imgur.com/X5qGkCg.png)
![[Image: 0op1GNe.png]](https://i.imgur.com/0op1GNe.png)
![[Image: LgJ2mtP.png]](https://i.imgur.com/LgJ2mtP.png)
![[Image: vLZcFUE.png]](https://imgur.com/vLZcFUE.png)
![[Image: lrUHro3.jpg]](https://i.imgur.com/lrUHro3.jpg)
![[Image: z08yIah.png]](http://i.imgur.com/z08yIah.png)