標籤:設定檔 使用者名稱 伺服器 linux 管理員
公司的無線環境採用mac地址認證的方式,mac地址被綁定到Radius的users設定檔中,將註冊了的mac地址作為使用者名稱和密碼。為了方便的管理這些mac地址,自己寫了一個shell指令碼來管理。
shell指令碼所特有的強大文本處理能力和各種命令函數的組合,使得管理員的工作能輕鬆不少。
下面就列出該指令碼的功能以示參考:
添加mac地址
刪除mac地址
尋找mac地址
去除重複mac地址
檢查mac地址合法性
TODO,匯入匯出mac地址,添加註釋
其中用到的Shell指令碼技術包括但不限於:
文字檔的列處理和行處理,如sed、awk等命令
字串尋找、過濾、大小寫轉換,bash和grep等命令
擷取、計算、比較字串長度,bash和wc等命令
mac地址Regex的處理和類型轉換
shell編程操作、包括檔案包含、函數、參數傳遞、傳回值等
其他
程式碼範例:
#!/bin/bash## Source function library.. /etc/rc.d/init.d/functionsRADIUSD=/usr/sbin/radiusdLOCKF=/var/lock/subsys/radiusdCONFIG=/etc/raddb/radiusd.confUSERCONFIG=/etc/raddb/users[ -f $RADIUSD ] || exit 0[ -f $CONFIG ] || exit 0[ -f $USERCONFIG ] || exit 0RETVAL=0OPERATION=$1MACADDRESS=$2function help(){clearecho $""echo $"===================================================================================="echo $"For Radius on Fedora/CentOS/RadHat Linux Server, Written by Chris"echo $"===================================================================================="echo $"A tool to manage Radius server"echo $""echo $"Usage: $0 {find|add|modify|delete|check|remove|start|stop|status|restart|reload} mac"#TODOecho $"Usage: $0 {import|export|debug}"echo $""echo $"For more information please contract [email protected].com"echo $"===================================================================================="echo $""exit 1}function mac(){if [ -z $MACADDRESS ];thenecho $"no mac address is signed! "echo $"\$2 is $MACADDRESS"exit 1else if [[ "${#MACADDRESS}" != "12" ]] && [[ "${#MACADDRESS}" != "17" ]] ;thenecho "mac length is ${#MACADDRESS}" echo "mac address is illegal! "exit 1# else# echo $"mac which you input is $MACADDRESS" fi#echo $MACADDRESS | sed -nr ‘/[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}/p‘#echo $MACADDRESS | sed -nr ‘/[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}/p‘#echo $MACADDRESS | sed -nr ‘/[A-Fa-f0-9]{12}/p‘if [[ `echo $MACADDRESS | grep -` ]];thenPROMAC=`echo $MACADDRESS | sed -nr ‘/[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}/p‘ | tr ‘[:upper:]‘ ‘[:lower:]‘ | sed ‘s/-//g‘`elif [[ `echo $MACADDRESS | grep :` ]];thenPROMAC=`echo $MACADDRESS | sed -nr ‘/[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}/p‘ | tr ‘[:upper:]‘ ‘[:lower:]‘ | sed ‘s/://g‘`elsePROMAC=`echo $MACADDRESS | tr ‘[:upper:]‘ ‘[:lower:]‘`fiecho $PROMACfi}function find(){MAC=`mac`echo $"accepted mac is $MAC"if [[ `grep $MAC $USERCONFIG` ]]; thenMACLINE=`grep -n $MAC $USERCONFIG | awk -F ‘:‘ ‘{print $1}‘`#echo $MACLINEMACLINECOUNT=$(echo $MACLINE | wc -w)#echo $MACLINECOUNTif [[ "$MACLINECOUNT" != "1" ]];thenecho $"ERROR, this mac $MAC has duplicate record, you should use $0 remove $MAC to remove duplicate record"exit 1fiecho $"Successfully find $MAC in $MACLINE line of file $USERCONFIG! "echoREVAL=$?elseecho $"Can not find $MAC in file $USERCONFIG! "echoexit 1REVAL=$?fi}function add(){MAC=`mac`echo $"accepted mac is $MAC"#find $MACLINENUM=`grep -n "Cleartext-Password :=‘" users | grep -v \# | head -n1 | awk -F ":" ‘{print $1}‘`SEDOPERATION=$LINENUM"a"sed -i "$SEDOPERATION $MAC Cleartext-Password :=‘$MAC‘" $USERCONFIGfind $MACrestart}function modify(){MAC=`mac`find $MAC#TODO}function delete(){ MAC=`mac` echo $"accepted mac is $MAC" if [[ `grep $MAC $USERCONFIG` ]]; then MACLINE=`grep -n $MAC $USERCONFIG | awk -F ‘:‘ ‘{print $1}‘` ##echo $MACLINE #MACLINECOUNT=$(echo $MACLINE | wc -w) ##echo $MACLINECOUNT #if [[ "$MACLINECOUNT" != "1" ]];then # echo $"ERROR, this mac $MAC has duplicate record, you should use $0 remove $MAC to remove duplicate record" # exit 1 #fi echo $"Successfully find $MAC in $MACLINE line of file $USERCONFIG! "echo $"It will be deleted! "sed -i "$MACLINE d" $USERCONFIG#TODOecho $"If you see ‘Can not find $MAC in file $USERCONFIG! ‘, it means successfully! "find $MAC echo REVAL=$? else echo $"Can not find $MAC in file $USERCONFIG! " echo REVAL=$? fi}function check(){MAC=`mac`find $MACremove $MAC}function remove(){MAC=`mac` echo $"accepted mac is $MAC"#TODO#echo $"backuped file to file $FILENAME" if [[ `grep $MAC $USERCONFIG` ]]; then MACLINE=`grep -n $MAC $USERCONFIG | awk -F ‘:‘ ‘{print $1}‘` #echo $MACLINE MACLINECOUNT=$(echo $MACLINE | wc -w) #echo $MACLINECOUNT if [[ "$MACLINECOUNT" == "1" ]];then echo $"WARNNING, this mac $MAC is good record, no duplicate record has found! " exit 0 fiTOREMOVE="$MAC Cleartext-Password :=‘$MAC‘"sed -i "/^$TOREMOVE$/d" $USERCONFIGadd $MACfi}function restart(){service radiusd restart}function reload(){service radiusd reload}function status(){service radiusd status}case "$1" in find)findRETVAL=$?;;add)addRETVAL=$?;;modify)modifyRETVAL=$?;;delete)deleteRETVAL=$?;;check)checkRETVAL=$?;;remove)removeRETVAL=$?;;start)startRETVAL=$?;;stop)stopRETVAL=$?;;status)statusRETVAL=$?;;restart)restartRETVAL=$?;;reload)reloadRETVAL=$?;;*)helpexit 1;;esac
其中有一些可以改進的地方,比如換一種方法或者增強使用者的使用體驗都是可以的,歡迎大家提出意見。
本文出自 “通訊,我的最愛” 部落格,請務必保留此出處http://dgd2010.blog.51cto.com/1539422/1567085
用Linux Shell指令碼輕鬆管理Radius伺服器