用Linux Shell指令碼輕鬆管理Radius伺服器
公司的無線環境採用mac地址認證的方式,mac地址被繫結到Radius的users配置檔案中,將註冊了的mac地址作為使用者名稱和密碼。為了方便的管理這些mac地址,自己寫了一個shell指令碼來管理。
shell指令碼所特有的強大文字處理能力和各種命令函式的組合,使得管理員的工作能輕鬆不少。
下面就列出該指令碼的功能以示參考:
- 新增mac地址
- 刪除mac地址
- 查詢mac地址
- 去除重複mac地址
- 檢查mac地址合法性
- TODO,匯入匯出mac地址,新增註釋
其中用到的Shell指令碼技術包括但不限於:
- 文字檔案的列處理和行處理,如sed、awk等命令
- 字串查詢、過濾、大小寫轉換,bash和grep等命令
- 獲取、計算、比較字串長度,bash和wc等命令
- mac地址正規表示式的處理和型別轉換
- shell程式設計操作、包括檔案包含、函式、引數傳遞、返回值等
- 其他
程式碼示例:
#!/bin/bash # # Source function library. . /etc/rc.d/init.d/functions RADIUSD=/usr/sbin/radiusd LOCKF=/var/lock/subsys/radiusd CONFIG=/etc/raddb/radiusd.conf USERCONFIG=/etc/raddb/users [ -f $RADIUSD ] || exit 0 [ -f $CONFIG ] || exit 0 [ -f $USERCONFIG ] || exit 0 RETVAL=0 OPERATION=$1 MACADDRESS=$2 function help() { clear echo $"" 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" #TODO echo $"Usage: $0 {import|export|debug}" echo $"" echo $"For more information please contract dgdenterprise@gmail.com" echo $"====================================================================================" echo $"" exit 1 } function mac() { if [ -z $MACADDRESS ];then echo $"no mac address is signed! " echo $"\$2 is $MACADDRESS" exit 1 else if [[ "${#MACADDRESS}" != "12" ]] && [[ "${#MACADDRESS}" != "17" ]] ;then echo "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 -` ]];then PROMAC=`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 :` ]];then PROMAC=`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'` else PROMAC=`echo $MACADDRESS | tr '[:upper:]' '[:lower:]'` fi echo $PROMAC fi } function find() { 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 REVAL=$? else echo $"Can not find $MAC in file $USERCONFIG! " echo exit 1 REVAL=$? fi } function add() { MAC=`mac` echo $"accepted mac is $MAC" #find $MAC LINENUM=`grep -n "Cleartext-Password :='" users | grep -v \# | head -n1 | awk -F ":" '{print $1}'` SEDOPERATION=$LINENUM"a" sed -i "$SEDOPERATION $MAC Cleartext-Password :='$MAC'" $USERCONFIG find $MAC restart } 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 #TODO echo $"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 $MAC remove $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 fi TOREMOVE="$MAC Cleartext-Password :='$MAC'" sed -i "/^$TOREMOVE$/d" $USERCONFIG add $MAC fi } function restart() { service radiusd restart } function reload() { service radiusd reload } function status() { service radiusd status } case "$1" in find) find RETVAL=$? ;; add) add RETVAL=$? ;; modify) modify RETVAL=$? ;; delete) delete RETVAL=$? ;; check) check RETVAL=$? ;; remove) remove RETVAL=$? ;; start) start RETVAL=$? ;; stop) stop RETVAL=$? ;; status) status RETVAL=$? ;; restart) restart RETVAL=$? ;; reload) reload RETVAL=$? ;; *) help exit 1 ;; esac
相關文章
- 輕鬆使用“Explain Shell”指令碼來理解 Shell 命令AI指令碼
- 使用shell指令碼在Linux中管理Java應用程式指令碼LinuxJava
- 用於管理應用程式得shell指令碼指令碼
- 輕鬆部署 Laravel 應用 | 《02. 一鍵指令碼》Laravel指令碼
- 有點用的linux shell 指令碼Linux指令碼
- Linux Shell指令碼Linux指令碼
- Linux shell 指令碼Linux指令碼
- 用Shell指令碼來解決DOS安全Linux伺服器生產指令碼Linux伺服器
- 多裝置相容指令碼,輕鬆拿捏指令碼
- Linux shell 指令碼分享Linux指令碼
- 輕鬆部署 Laravel 應用 | 《03. 探尋一鍵指令碼》Laravel指令碼
- 設定Linux伺服器IP地址的shell指令碼Linux伺服器指令碼
- Shell指令碼應用(三)指令碼
- linux shell小指令碼分享Linux指令碼
- linux常用的shell指令碼Linux指令碼
- 簡單的Shell指令碼實戰演練,測試菜鳥也可以輕鬆學會指令碼
- Linux/Unix shell 指令碼中呼叫SQL,RMAN指令碼Linux指令碼SQL
- 9個實用shell指令碼指令碼
- Linux基礎五(shell指令碼)Linux指令碼
- Linux 【Shell指令碼經典案例】Linux指令碼
- 【Linux】【Shell】主控指令碼實現Linux指令碼
- linux shell 指令碼攻略筆記Linux指令碼筆記
- Linux Shell指令碼系列之二Linux指令碼
- Linux Shell指令碼系列之一Linux指令碼
- Linux Shell 指令碼面試 25 問Linux指令碼面試
- Linux Shell指令碼面試25問Linux指令碼面試
- linux shell指令碼中 =~ 的作用Linux指令碼
- 用chkconfig輕鬆管理啟動程式
- Linux shell:執行shell指令碼的幾種方式Linux指令碼
- Linux Shell程式設計(3)——執行shell指令碼Linux程式設計指令碼
- shell指令碼指令碼
- Linux shell指令碼判斷伺服器網路是否可以上網Linux指令碼伺服器
- 幾例實用的Shell指令碼指令碼
- Shell指令碼應用兩個例子指令碼
- 用Shell指令碼&sqlloader做ETL指令碼SQL
- 輕鬆搭建Redis伺服器Redis伺服器
- Linux shell 指令碼基礎介紹Linux指令碼
- Linux命令和shell指令碼學習Linux指令碼