用Linux Shell脚本轻松管理Radius服务器
生活随笔
收集整理的這篇文章主要介紹了
用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/functionsRADIUSD=/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?0RETVAL=0OPERATION=$1 MACADDRESS=$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?dgdenterprise@gmail.com"echo?$"===================================================================================="echo?$""exit?1 }function?mac() {if?[?-z?$MACADDRESS?];thenecho?$"no?mac?address?is?signed!?"echo?$"\$2?is?$MACADDRESS"exit?1elseif?[[?"${#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?:='"?$USERCONFIG?|?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`?]];?thenMACLINE=`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#fiecho?$"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?$MACechoREVAL=$?elseecho?$"Can?not?find?$MAC?in?file?$USERCONFIG!?"echoREVAL=$?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`?]];?thenMACLINE=`grep?-n?$MAC?$USERCONFIG?|?awk?-F?':'?'{print?$1}'`#echo?$MACLINEMACLINECOUNT=$(echo?$MACLINE?|?wc?-w)#echo?$MACLINECOUNTif?[[?"$MACLINECOUNT"?==?"1"?]];thenecho?$"WARNNING,?this?mac?$MAC?is?good?record,?no?duplicate?record?has?found!?"exit?0fiTOREMOVE="$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其中有一些可以改進的地方,比如換一種方法或者增強用戶的使用體驗都是可以的,歡迎大家提出意見。
總結
以上是生活随笔為你收集整理的用Linux Shell脚本轻松管理Radius服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS多线程GCD 研究
- 下一篇: ubuntu创建、删除文件及文件夹,强制