details for this commit to come

master
Kebler Network System Administrator 2022-03-28 10:02:17 -07:00
parent 0d088cbcab
commit ef8afa39c7
7 changed files with 123 additions and 148 deletions

View File

@ -9,7 +9,9 @@ local NAME
DIR=$1 DIR=$1
NAME=$2 NAME=$2
# echo finding $NAME in $DIR # echo finding $NAME in $DIR
FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh)" # TODO use file instead for valid extensions
# allow explicit extension
FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
# echo files found $FILE # echo files found $FILE
COUNT=0 COUNT=0
if [ "$FILE" ]; then if [ "$FILE" ]; then
@ -79,7 +81,8 @@ FILE=$(module_find $1)
[ $? -ne 0 ] && echo no module $1 found && return 1 [ $? -ne 0 ] && echo no module $1 found && return 1
# source $FILE "$0" # source $FILE "$0"
# [[ $BASHPID -eq $$ ]] || echo $FILE # [[ $BASHPID -eq $$ ]] || echo $FILE
source $FILE shift 1
source $FILE "$@"
return 0 return 0
} }

View File

@ -30,12 +30,14 @@ usage()
echo "Options:" echo "Options:"
echo " -d DIRPERMS, directory permissions" echo " -d DIRPERMS, directory permissions"
echo " -f FILEPERMS, file permissions" echo " -f FILEPERMS, file permissions"
exit 1 return 1
} }
# Check if user entered arguments # Check if user entered arguments
if [ $# -lt 1 ] ; then if [ $# -lt 1 ] ; then
usage usage
return 1
fi fi
# Get options # Get options
@ -62,7 +64,7 @@ ROOT=$1
# Check if the root path is a valid directory # Check if the root path is a valid directory
if [ ! -d $ROOT ] ; then if [ ! -d $ROOT ] ; then
echo "$ROOT does not exist or isn't a directory!" ; exit 1 echo "$ROOT does not exist or isn't a directory!" ; return 2
fi fi
# Recursively set directory/file permissions based on the permission variables # Recursively set directory/file permissions based on the permission variables

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# export BASH_DEBUG=true # export BASH_DEBUG=true
# module_load debug module_load debug
module_load iecho module_load iecho
isFile() { isFile() {
@ -226,4 +226,4 @@ rm -f "$tmp_fn"
return 0 return 0
# [End] # [End]
} }

View File

@ -5,7 +5,7 @@ mounted () {
mountpoint "$1" &> /dev/null && echo yes || return 1 mountpoint "$1" &> /dev/null && echo yes || return 1
} }
# peals back sub-directories until if finds a mountpoing # peals back sub-directories until if finds a mountpoint
find_mountpoint () { find_mountpoint () {
local dir=$1 local dir=$1
if [[ ! $dir = "/" ]]; then if [[ ! $dir = "/" ]]; then

View File

@ -1,39 +0,0 @@
#!/bin/bash
# must be json as a string, depends on jq
get_prop_value () {
local value
# echo in $1 get $2
value=$(echo $1 | jq -r .$2)
echo $value
}
is_array() {
local variable_name=$1
[[ "$(declare -p $variable_name 2>/dev/null)" =~ "declare -a" ]]
}
filename() {
# passed entire path
echo $(basename "$1" | rev | cut -f 2- -d '.' | rev)
}
adirname() {
# passed entire path
echo "$(cd "$(dirname "$1")" >/dev/null 2>&1 ; pwd -P )"
}
user_exists() {
id -u $1 > /dev/null 2>&1
[[ $? == 1 ]] || echo $1
}
chmod_dirs() {
# passed entire path
local usesudo
[[ $1 == -s ]] && usesudo="sudo" && shift 2
$usesudo find $1 -type f -exec chmod $2 {} +
}

View File

@ -1,23 +0,0 @@
#!/bin/bash
# get full absolute bath from a current or parent path
function abs-path {
local target="$1"
if [ "$target" == "." ]; then
echo "$(pwd)"
elif [ "$target" == ".." ]; then
echo "$(dirname "$(pwd)")"
else
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
fi
}
isAbsPath() {
if [[ "${1:0:1}" == / || "${1:0:2}" == ~[/a-z] ]]
then
echo "true"
return 0
else
return 1
fi
}

View File

@ -4,109 +4,141 @@
function clone_user_ () { function clone_user_ () {
module_load confirm module_load confirm
type confirm
echo "=============" echo "============="
echo "this script will create a new user" echo "this script will create a new user"
echo "based on an existing user's data" echo "based on an existing user's data"
echo echo
echo "You will be shown a list of users who can currently log on" echo "You will be shown a list of users who can currently log on"
echo "Remember which user you would like to clone." echo "Remember which user you would like to clone."
echo "You will be asked for the new user's name, their password" echo "You will be asked for the new user's name, their password"
echo "and the old user to clone". echo "and the old user to clone".
echo "=============" echo "============="
echo echo
echo -n "New user's name: " echo -n "New user's name: "
read newuser read newuser
echo -n "New user's password: " echo -n "New user's password: "
read newpassword read newpassword
echo echo
echo "Current users you can clone:" echo "Current users you can clone:"
echo "----" echo "----"
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
echo echo
echo -n "Old user to clone: " echo -n "Old user to clone: "
read olduser read olduser
echo "olduser uid is $(id -u $olduser)" echo "olduser uid is $(id -u $olduser)"
echo echo
echo "You have selected: " echo "You have selected: "
echo "----" echo "----"
echo "new user: $newuser" echo "new user: $newuser"
echo "new user password: $newpassword" echo "new user password: $newpassword"
echo "old user: $olduser" echo "old user: $olduser"
echo echo
olduser_GROUPS="$(id -Gn ${olduser} | sed "s/${olduser} //g" | sed "s/ ${olduser}//g" | sed "s/ /,/g"),$olduser" olduser_GROUPS="$(id -Gn ${olduser} | sed "s/${olduser} //g" | sed "s/ ${olduser}//g" | sed "s/ /,/g"),$olduser"
olduser_SHELL=$(awk -F : -v name=${olduser} '(name == $1) { print $7 }' /etc/passwd) olduser_SHELL=$(awk -F : -v name=${olduser} '(name == $1) { print $7 }' /etc/passwd)
echo "old user groups: " echo "old user groups: "
echo "----" echo "----"
echo $olduser_GROUPS echo $olduser_GROUPS
echo "olduser shell: " echo "olduser shell: "
echo $olduser_SHELL echo $olduser_SHELL
# read -rsp $'Press any key to continue or ctrl-c to exit...\n' -n1 key
# return
# confirm "ready to clone user, begin?" || return 1
# useradd --groups $olduser_GROUPS --shell $olduser_SHELL $newuser confirm "ready to clone user, begin?" || return 1
# echo $newuser:$newpassword | chpasswd useradd --groups $olduser_GROUPS --shell $olduser_SHELL $newuser
# read -rsp $'ready to make home direcoty -- ctrl-c to exit...\n' -n1 key echo $newuser:$newpassword | chpasswd
# mkdir /home/$newuser read -rsp $'ready to make home direcoty -- ctrl-c to exit...\n' -n1 key
# chown -R $newuser:$newuser /home/$newuser
# echo mkdir /home/$newuser
# echo "Script should be done now." chown -R $newuser:$newuser /home/$newuser
# echo
# echo "Do you see your new users name below?"
# echo
# awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
# echo echo
# echo "We are now going to copy the old user's home folder to the new user" echo "Script should be done now."
# echo "then change ownership to the new user" echo
# echo echo "Do you see your new users name below?"
# read -rsp $'Ready to copy home folder --- or ctrl-c to exit...\n' -n1 key echo
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
echo
echo "We are now going to copy the old user's home folder to the new user"
echo "then change ownership to the new user"
echo
read -rsp $'Ready to copy home folder --- or ctrl-c to exit...\n' -n1 key
rsync -aPv --exclude 'Downloads' /home/$olduser/. /home/$newuser/ rsync -aPv --exclude 'Downloads' /home/$olduser/. /home/$newuser/
chown -R --from=$olduser $newuser:$newuser /home/$newuser chown -R --from=$olduser $newuser:$newuser /home/$newuser
echo echo
echo "Now we are going to change the names of files and folders to the new user" echo "Now we are going to change the names of files and folders to the new user"
echo echo
grep -rlI $olduser /home/$newuser/ . | sudo xargs sed -i 's/$olduser/$newuser/g' grep -rlI $olduser /home/$newuser/ . | sudo xargs sed -i 's/$olduser/$newuser/g'
echo echo
echo "Done now." echo "Done now."
echo echo
read -rsp $'Press any key to exit...\n' -n1 key read -rsp $'Press any key to exit...\n' -n1 key
echo echo
echo echo
} }
clone_user () { clone_user () {
if [[ $EUID != 0 ]]; then if [[ $EUID != 0 ]]; then
clone=$(declare -f clone_user_) clone=$(declare -f clone_user_)
module_load confirm module_load confirm
confirm=$(declare -f confirm) confirm=$(declare -f confirm)
sudo bash -c "$confirm; $clone; clone_user_" sudo bash -c "$confirm; $clone; clone_user_"
else else
echo run as regular user with sudo privliges and it will elevate echo run as regular user with sudo privliges and it will elevate
fi fi
} }
# # if script was executed then call the function rename_user () {
(return 0 2>/dev/null) || clone_user
module_load confirm
local force; local name; local newname; local newhome
[[ $1 = "-f" ]] && { force=true; shift 1; }
[[ $1 = "-h" ]] && { newhome=$2; shift 2; }
# usage: < -f, -h newhome > name newname
# default new user home is /home/newname
name=$1
newname=$2
if [[ ! ($name && $newname) ]]; then
echo "============="
echo "this script will rename an existing user"
echo "user running this script must to root or have sudo priviledges to run"
echo "---- Available Users to Rename ---"
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
echo "============="
read -r -p "Enter an available user name: " name
read -r -p "Enter users new name: " newname
read -r -p "Enter users new home directory <enter for default /home/$newname>: " newhome
fi
newhome=${newhome:-/home/$newname}
if [[ ! $force ]]; then
echo "Changing $name to $newname with home $newhome"
echo sudo usermod -l $newname $name
echo sudo groupmod -n $newname $name
echo sudo usermod --d $newhome --m $name
confirm -s "These are the commands that will be run. Do you want to continue?" || return 1
fi
sudo usermod -l $newname $name
echo sudo groupmod -n $newname $name
echo sudo usermod --d $newhome --m $name
}