該腳本做爲練習只用,修改sudo文件還請使用visudo命令,防止修改錯誤形成安全事故!算法
--------------------------------------------shell
練習實如今/etc/sudoers文件中交互式的添加內容,要求能根據用戶需求建立所需的組安全
寫的很糾結~錯誤百出。而且是實現的算法有的太複雜了,先貼上原始的一;後期在進行修改了再附加。過程很少解釋。bash
方法(1)app
- #!/bin/bash
- #Author: Mo
- #Date & Time: 2011-01-02-20:55:40
- #Description:
- #Version : 1.0.1
- runas=root
- machine=ALL
- ualias=' '
- halias=' '
- mytrap(){
- echo -e "\nQuit...\n Clean all your input messsage..."
- exit 1
- }
- trap 'mytrap' SIGINT
- # user functions... #########################
- testuser() {
- if cut -d: -f1 /etc/passwd | grep "^$1$" &>/dev/null; then
- return 0
- else
- echo "No user $1,please check ."
- return 99
- fi
- }
- adduser(){
- while [ -z ${user} ] ;do
- read -p "Please input sudo user: " user
- testuser ${user}
- [ $? -eq 99 ] && user=''
- done
- uarray[${#uarray[*]}]=${user}
- user=' '
- }
- useralias() {
- read -p "Enter user alias name(upper):" ualias
- if echo $ualias | grep "[^a-zA-Z]" &>/dev/null ;then
- echo "Wrong input!"
- exit 1
- else
- ualias=`echo ${ualias} | tr 'a-z' 'A-Z'`
- fi
- flag1=y
- while [ "${flag1}" = "y" ] ;do
- adduser
- read -p "Wish to add another user(y/n): " flag1
- flag1=${flag1:=y}
- if echo "${flag1}"|grep "[YNyn]" &>/dev/null ;then
- [ "${flag1}" = "y" ] && uarray[${#uarray[*]}]="${user}," || uarray[${#uarray[*]}]=${user}
- else
- echo "Wrong Select!"
- exit 1
- fi
- done
- }
- # host funcitons...#######################
- addhost(){
- host=' '
- while [ -z ${host} ] ;do
- read -p "Please input host: " host
- done
- harray[${#harray[*]}]=${host}
- }
- hostalias() {
- read -p "Enter host alias name(upper):" halias
- if echo ${halias} | grep "[^a-zA-Z]" &>/dev/null ;then
- echo "Wrong Name!"
- exit 1
- else
- halias=`echo ${halias}| tr 'a-z' 'A-Z'`
- fi
- flag2=y
- while [ "${flag2}" = "y" ] ;do
- addhost
- read -p "Wish to add another host(y/n): " flag2
- flag2=${flag2:=y}
- if echo "${flag2}" | grep "[YNyn]" &>/dev/null ;then
- [ "${flag2}" = "y" ] && harray[${#harray[*]}-1]=${host}, || harray[${#harray[*]}-1]=${host}
- else
- echo "Wrong Select!"
- exit 1
- fi
- done
- }
- #command function...#######################
- addcomm(){
- while [ -z ${comm} ] &>/etc/null ;do
- #how to read a command line?
- read -p "Please input a sudo command: " comm1 comm2
- comm=`echo "${comm1} ${comm2}"`
- done
- carray[${#carray[*]}]=${comm}
- comm=' '
- }
- commalias() {
- read -p "Enter command alias name(upper):" calias
- if echo ${calias} | grep "[^a-zA-Z]" &>/dev/null ;then
- echo "Wrong input!"
- exit 1
- else
- calias=`echo ${calias} | tr 'a-z' 'A-Z'`
- fi
- flag3=y
- while [ "${flag3}" = "y" ] ;do
- addcomm
- read -p "Wish to add another command(y/n): " flag3
- flag3=${flag3:=y}
- if echo "${flag3}"|grep "[YNyn]" &>/dev/null ;then
- [ "${flag3}" = "y" ] && carray[${#carray[*]}]=${comm}, || carray[${#carray[*]}]=${comm}
- else
- echo "Wrong Select!"
- exit 1
- fi
- done
- }
- # user add... #############################
- cat <<EOF
- 1) Create user alias;
- 2) Add a single user;
- EOF
- read -p "Enter your select: " select1
- case ${select1} in
- 1)
- useralias
- ;;
- 2)
- adduser
- ;;
- *)
- echo "Wrong select!"
- exit 1
- ;;
- esac
- # host add... ###############################
- cat <<EOF
- 1) Create host alias;
- 2) Add a single host;
- EOF
- read -p "Enter your select: " select2
- case ${select2} in
- 1)
- hostalias
- ;;
- 2)
- addhost
- ;;
- *)
- echo "Wrong select!"
- exit 1
- ;;
- esac
- # command add...#################################
- cat <<EOF
- 1) Create command alias;
- 2) Add a single commond;
- EOF
- read -p "Enter your select: " select3
- case ${select3} in
- 1)
- commalias
- ;;
- 2)
- addcomm
- ;;
- *)
- echo "Wrong select!"
- exit 1
- ;;
- esac
- # appent thess informatin to file:/etc/sudoers
- # US HO = (root) COM
- if [ ${#uarray[*]} -gt 1 ];then
- echo "User_Alias ${ualias} = ${uarray[*]} " >> /etc/sudoers
- fi
- if [ ${#harray[*]} -gt 1 ];then
- echo "Host_Alias ${halias} = ${harray[*]} " >> /etc/sudoers
- fi
- if [ ${#carray[*]} -gt 1 ];then
- echo "Comm_Alias ${calias} = ${carray[*]} " >> /etc/sudoers
- fi
- LINE="`[ ${#uarray[*]} -gt 1 ] && echo -n "${ualias}" || echo -n "${uarray[*]}" `"
- LINE="${LINE} `[ ${#harray[*]} -gt 1 ] && echo -n "${halias}" || echo -n "${harray[*]}" ` "
- LINE="${LINE}=(root) "
- LINE="${LINE} `[ ${#carray[*]} -gt 1 ] && echo -n "${calias}" || echo -n "${carray[*]}" ` "
- echo "${LINE}" >> /etc/sudoers
- echo -e "\033[32mAll Done!\033[0m"
附註:如何從鍵盤讀入一行數據而且賦值給變量。ide
read用法:
read [ -p ][ -r ][ -s ][ -u [ n ] ] [ VariableName ]
read 命令從標準輸入中讀取一行,並把輸入行的每一個字段的值指定給 shell 變量,用 IFS(內部字段分隔符)變量中的字符做爲分隔符。VariableName 參數指定 shell 變量的名稱,shell 變量獲取輸入行一個字段的值。由VariableName 參數指定的第一個 shell 變量指定給每個字段的值,由 VariableName 參數指定的第二個 shell 變量指定給第二個字段的值,以此類推,直到最後一個字段。若是標準輸入行的字段比相應的由 VariableName 參數指定的 shell 變量的個數多,把所有餘下的字段的值賦給指定的最後的 shell 變量。若是比 shell 變量的個數少,則剩餘的 shell 變量被設置爲空字符串。ui
若是輸入的一個串中間有空格,read一個變量,該變量顯然只能截取並獲得串中第一個空格前的子串。未解決丟失,read時,用兩個變量,則第一個變量會獲取第一個空格前的子串,第二個變量會獲取其後的全部串。而後將兩個變量銜接便可spa
例如97行:orm