From a4978b7e27b58b3e314b917a1c0fdc8ac8fb219b Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Thu, 26 Dec 2013 13:33:07 +0100 Subject: [PATCH] Started working on Zenity-version of exercise 3, lab 7 --- Labb7/ovning3_zenity.sh | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 Labb7/ovning3_zenity.sh diff --git a/Labb7/ovning3_zenity.sh b/Labb7/ovning3_zenity.sh new file mode 100755 index 0000000..30405a5 --- /dev/null +++ b/Labb7/ovning3_zenity.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +# Jack-Benny Persson +# LX13 +# Excercise 3, lab 7 (Code a script to help admins create new users) + +# Define some variables + +# Binaries +Chpasswd="/usr/sbin/chpasswd" +Useradd="/usr/sbin/useradd" +Zenity="/usr/bin/zenity" +Grep="/bin/grep" +Cat="/bin/cat" +Rm="/bin/rm" + +# Functions +ask() +{ + $Zenity --title "Create new user" --forms \ + --add-entry="Username" \ + --add-entry="Full path of shell" \ + --add-entry="Optional comment (Full name etc)" \ + --add-password="Password" \ + > /tmp/createuser + Input=`$Cat /tmp/createuser` + if [ $? -eq 1 ]; then + echo "Aborting, user hit cancel..." + exit 2 + elif [ $? -ne 0 ]; then + echo "Aborting, something went wrong" + exit 2 + fi +} + + +# Sanity checks +if [ $EUID -ne 0 ]; then + echo "You need to run this script as root" + exit 2 +fi + +for bin in $Chpasswd $Useradd $Zenity $Grep $Cat $Rm; do + if [ ! -x $bin ]; then + echo "Can't execute $bin" + exit 2 + fi +done + +# Main +ask "Enter username of the new user" +Username=$Input + +# Get a list of all avaliable shells on the system +Shell=`$Cat /etc/shells | $Grep /bin` +ShellList=`printf "$Shell" | sed 's/ / shell /g' | sed 's/$/ shell/'` + +HowMany=`echo $Shell | wc -w` # How many shells are avaliable? + +$Zenity --backtitle "Create new user"\ + --menu "Choose a shell for your new user" 14 60 $HowMany $ShellList\ + 2> /tmp/createuser +Input=`$Cat /tmp/createuser` +UserShell=$Input + +ask "Enter a password for the new user" +Password=$Input + +# Remove the temp file (it contains the password of latest created user) +$Rm /tmp/createuser + +# Create the user and set the password +$Useradd -m -s $UserShell $Username +echo "${Username}:${Password}" | $Chpasswd + +exit 0