开发自动化系统管理脚本(shell)

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://5ydycm.blog.51cto.com/115934/389631

[背景]因想自动化来设置一些系统的配置,所以就写了一个小工具来满足自己的需求,

此工具具有如下功能:

(一)用户管理
1、修改root密码
2、删除用户帐号
3、添加用户帐号
(二)服务管理
1、开启服务
2、关闭服务
(三)防火墙/ssh认证管理
1、关闭默认防火墙,开启自定防火墙脚本(自定义脚本分为:公司环境下,以及互联网环境下)
2、修改ssh认证配置文件(采用publickey认证登录)
(四)自动设置
1、自动添加"互联网环境下"的防火墙
2、采用publickey认证登录
(五)重启功能
所有这些功能都是以函数块做的,大家可以根据自己的需求做出相应的调整以适应自己公司的需求.
开发os:centos5.2
脚本:shell
功能已经基本测试OK,不过。还需要其他朋友挖Bug...

贴些图让大家更直观点:

开发自动化系统管理脚本(shell)_第1张图片

AutoSetSystem.sh

  1. #!/bin/bash
  2. #########################################################################
  3. #
  4. # File:         autosetsystem.sh
  5. # Description:  
  6. # Language:     GNU Bourne-Again SHell
  7. # Version:  1.1
  8. # Date:     2010-9-6
  9. # Corp.:    tiancity
  10. # Author:   zhuzhengjun
  11. # WWW:      http://www.tiancity.com
  12. ###############################################################################
  13.  
  14. zzj_key='zzjkey'
  15.  
  16. general_iptable_content="/sbin/iptables -F\n/sbin/depmod -a\n/sbin/modprobe ip_tables\n/sbin/modprobe ip_conntrack\n/sbin/iptables -A INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT\n/sbin/iptables -A INPUT -i lo -j ACCEPT\n/sbin/iptables -P INPUT DROP"
  17. public_ip="ip1 ip2 ip3"
  18. private_ip="ip1 ip2 ip3 ip4"
  19.  
  20. MainMenu()
  21. {
  22. clear
  23. echo
  24. echo "-------------------------------------------------------------------------"
  25. tput cup 2
  26. time=`date +"%Y-%m-%d"`
  27. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time"
  28. echo
  29. tput cup 3
  30. echo  "-------------------------------------------------------------------------"
  31. tput cup 4 20
  32. echo -e "1:Manage User;"
  33. tput cup 5 20
  34. echo -e "2:Manage Services;"
  35. tput cup 6 20
  36. echo -e "3:Manage Firewall/SSH;"
  37. tput cup 7 20
  38. tput bold
  39. echo -e "4:AutoSet;"
  40. tput sgr0
  41. tput cup 8 20
  42. echo -e "5:Reboot;"
  43. tput cup 9 20
  44. echo -e "6:Quit;"
  45. tput cup 10 
  46. echo  "--------------------------------------------------------------------------"
  47. echo -n "You choice [1,2,3,4,5,6]:"
  48. read AA
  49. case $AA in
  50. 1)
  51. ManageUser
  52. ;;
  53. 2)
  54. ManageServices
  55. ;;
  56. 3)
  57. ManageFirewall
  58. ;;
  59. 4)
  60. AutoSet
  61. ;;
  62. 5)
  63. echo -n "Are you sure reboot system[y|n]?"
  64. read answer
  65. if [ $answer == "y" ];then
  66. shutdown -r now
  67. exit 0
  68. else
  69. echo -n "You forego reboot system!"
  70. sleep 2
  71. fi
  72. ;;
  73. *)
  74. Quit
  75. ;;
  76. esac
  77. }
  78. AutoSet(){
  79. EnableOutFirewall
  80. echo -e "\n"
  81. PublickeyAuthenticate
  82. }
  83.  
  84. AddUser(){
  85. echo -n "Please input add user name:"
  86. read username
  87. (awk -F':' '{print $1}' /etc/passwd|grep ^$username$) && (echo "Add user faild because user exists!"&&sleep 2)||(useradd $username&&passwd $username&&sleep 2)}
  88.  
  89. DeleteUser(){
  90. echo -n "Please input delete user name:"
  91. read username
  92. echo -n "Are you sure delete $username[y|n]?"
  93. read answer
  94. if [ $answer == "y" ];then
  95. (awk -F':' '{print $1}' /etc/passwd|grep ^$username$)&&(userdel $username&&echo "user delete sucessfull!"&&sleep 2)||(echo "Delete user faild because user account not exists!"&&sleep 2)
  96. else
  97. echo -n "You forego delete $username account!"
  98. sleep 2
  99. fi
  100. }
  101.  
  102. ModifyRootpwd(){
  103. echo -n "Are you sure modify root password[y|n]?"
  104. read answer
  105. if [ $answer == "y" ];then
  106. passwd root
  107. sleep 2
  108. else
  109. echo -n "You forego modify root password!"
  110. sleep 2
  111. fi
  112. }
  113.  
  114. ViewUser(){
  115. more /etc/passwd
  116. tput bold 
  117. echo "Wait 8 sec!"
  118. sleep 8 
  119. tput sgr0 
  120. }
  121.  
  122. ManageUserMenu(){
  123. clear
  124. echo
  125. echo "-------------------------------------------------------------------------"
  126. tput cup 2
  127. time=`date +"%Y-%m-%d"`
  128. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time"
  129. echo
  130. tput cup 3
  131. echo  "-------------------------------------------------------------------------"
  132. tput cup 4 20
  133. echo -e "1:Add User;"
  134. tput cup 5 20
  135. echo -e "2:Delete User;"
  136. tput cup 6 20
  137. echo -e "3:Modify root password;"
  138. tput cup 7 20
  139. echo -e "4:View User;"
  140. tput cup 8 20
  141. echo -e "5:Quit;"
  142. tput cup 9 
  143. echo  "--------------------------------------------------------------------------"
  144. echo -n "You choice [1,2,3,4,5]:"
  145. read BB 
  146. case $BB in
  147. 1)
  148. AddUser
  149. ;;
  150. 2)
  151. DeleteUser
  152. ;;
  153. 3)
  154. ModifyRootpwd
  155. ;;
  156. 4)
  157. ViewUser
  158. ;;
  159. *)
  160. echo "Quit"
  161. break
  162. ;;
  163. esac
  164. }
  165.  
  166. EnableServices(){
  167. echo -n "Please input enable service name:"
  168. read servicename
  169. echo -n "Are you sure enable $servicename[y|n]?"
  170. read answer
  171. if [ $answer == "y" ];then
  172. (chkconfig --list|awk '{print $1}'|grep ^$servicename$)&&(chkconfig --level 345 $servicename on&&echo "service enable sucessfull!"&&sleep 2)||(echo "service enable faild because service not exists!"&&sleep 2)
  173. else
  174. echo -n "You forego enable $servicename!"
  175. sleep 2
  176. fi
  177. }
  178.  
  179. DisableServices(){
  180. echo -n "Please input disable service name:"
  181. read servicename
  182. echo -n "Are you sure disable $servicename[y|n]?"
  183. read answer
  184. if [ $answer == "y" ];then
  185. (chkconfig --list|awk '{print $1}'|grep ^$servicename$)&&(chkconfig --level 345 $servicename off&&echo "service diable sucessfull!"&&sleep 2)||(echo "service disable faild because service not exists!"&&sleep 2)
  186. else
  187. echo -n "You forego disable $servicename!"
  188. sleep 2
  189. fi
  190. }
  191.  
  192. ViewServices(){
  193. chkconfig --list
  194. tput bold
  195. echo "Wait 8 sec!"
  196. sleep 8
  197. tput sgr0
  198. }
  199.  
  200. ManageServicesMenu(){
  201. clear
  202. echo
  203. echo "-------------------------------------------------------------------------"
  204. tput cup 2
  205. time=`date +"%Y-%m-%d"`
  206. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time"
  207. echo
  208. tput cup 3
  209. echo  "-------------------------------------------------------------------------"
  210. tput cup 4 20
  211. echo -e "1:Enable Services;"
  212. tput cup 5 20
  213. echo -e "2:Disable Services;"
  214. tput cup 6 20
  215. echo -e "3:View Services;"
  216. tput cup 7 20
  217. echo -e "4:Quit;"
  218. tput cup 8 
  219. echo  "--------------------------------------------------------------------------"
  220. echo -n "You choice [1,2,3,4]:"
  221. read CC 
  222. case $CC in
  223. 1)
  224. EnableServices
  225. ;;
  226. 2)
  227. DisableServices
  228. ;;
  229. 3)
  230. ViewServices
  231. ;;
  232. *)
  233. echo "Quit"
  234. break
  235. ;;
  236. esac
  237. }
  238.  
  239. EnableCompanyFirewall(){
  240. echo -n "Are you sure enable firewall[y|n]?"
  241. read answer
  242. if [ $answer == "y" ];then
  243. if [ ! -d /scripts ];then
  244. mkdir /scripts
  245. fi
  246. echo -e $general_iptable_content >/scripts/start_firewall.sh
  247. for ip in $private_ip
  248. do
  249. echo "/sbin/iptables -A INPUT -s $ip -p tcp --dport 22 -j ACCEPT" >>/scripts/start_firewall.sh
  250. done
  251. echo "sh /scripts/start_firewall.sh" >>/etc/rc.local
  252. chmod +x /scripts/start_firewall.sh
  253. sh /scripts/start_firewall.sh
  254. echo "Enable Firewall sucessful!"
  255. sleep 3
  256. else
  257. echo -n "You forego enable firewall!"
  258. sleep 2
  259. fi
  260. }
  261. EnableOutFirewall(){
  262. echo -n "Are you sure enable firewall[y|n]?"
  263. read answer
  264. if [ $answer == "y" ];then
  265. if [ ! -d /scripts ];then
  266. mkdir /scripts
  267. fi
  268. echo -e $general_iptable_content >/scripts/start_firewall.sh
  269. for ip in $public_ip
  270. do
  271. echo "/sbin/iptables -A INPUT -s $ip -p tcp --dport 22 -j ACCEPT" >>/scripts/start_firewall.sh
  272. done
  273. echo "sh /scripts/start_firewall.sh" >>/etc/rc.local
  274. chmod +x /scripts/start_firewall.sh
  275. sh /scripts/start_firewall.sh
  276. echo "Enable Firewall sucessful!"
  277. sleep 3
  278. else
  279. echo -n "You forego enable firewall!"
  280. sleep 2
  281. fi
  282. }
  283.  
  284. FirewallEnvMenu(){
  285. clear
  286. echo
  287. echo "-------------------------------------------------------------------------"
  288. tput cup 2
  289. time=`date +"%Y-%m-%d"`
  290. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time"
  291. echo
  292. tput cup 3
  293. echo  "-------------------------------------------------------------------------"
  294. tput cup 4 20
  295. echo -e "1:Enable Company Env Firewall;"
  296. tput cup 5 20
  297. echo -e "2:Enable Out Env Firewall;"
  298. tput cup 6 20
  299. echo -e "3:Quit;"
  300. tput cup 7 
  301. echo  "--------------------------------------------------------------------------"
  302. echo -n "You choice [1,2,3]:"
  303. read EE 
  304. case $EE in
  305. 1)
  306. EnableCompanyFirewall
  307. ;;
  308. 2)
  309. EnableOutFirewall
  310. ;;
  311. *)
  312. echo "Quit"
  313. break
  314. ;;
  315. esac
  316. }
  317. CustomizeFirewall()
  318. {
  319. while true
  320. do
  321. FirewallEnvMenu
  322. done
  323. }
  324. PublickeyAuthenticate()
  325. {
  326. echo -n "Are you sure enable publickey auth[y|n]?"
  327. read answer
  328. if [ $answer == "y" ];then
  329. if [ ! -d /root/.ssh ];then
  330. mkdir /root/.ssh
  331. fi
  332. touch /root/.ssh/authorized_keys
  333. echo $zzj_key >/root/.ssh/authorized_keys
  334. cp /etc/ssh/sshd_config /tmp/sshd_config_bak
  335. sed 's/^PasswordAuthentication yes$/PasswordAuthentication no/' /etc/ssh/sshd_config > /etc/ssh/tmp_sshd_config
  336. sed 's/^#PubkeyAuthentication yes$/PubkeyAuthentication yes/' /etc/ssh/tmp_sshd_config > /etc/ssh/tmp1_sshd_config
  337. sed 's/^#AuthorizedKeysFile/AuthorizedKeysFile/' /etc/ssh/tmp1_sshd_config > /etc/ssh/tmp_sshd_config
  338. rm -fr /etc/ssh/sshd_config
  339. rm -fr /etc/ssh/tmp1_sshd_config
  340. mv /etc/ssh/tmp_sshd_config /etc/ssh/sshd_config
  341. kill -HUP `cat /var/run/sshd.pid`
  342. echo "Please use public key try login agains!"
  343. sleep 5 
  344. else
  345. echo -n "You forego publickey auth!"
  346. sleep 2
  347. fi
  348. }
  349.  
  350. ManagerFirewallMenu(){
  351. clear
  352. echo
  353. echo "-------------------------------------------------------------------------"
  354. tput cup 2
  355. time=`date +"%Y-%m-%d"`
  356. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time"
  357. echo
  358. tput cup 3
  359. echo  "-------------------------------------------------------------------------"
  360. tput cup 4 20
  361. echo -e "1:Enable Customize Firewall;"
  362. tput cup 5 20
  363. echo -e "2:Enable Publickey Authenticate;"
  364. tput cup 6 20
  365. echo -e "3:Quit;"
  366. tput cup 7 
  367. echo  "--------------------------------------------------------------------------"
  368. echo -n "You choice [1,2,3]:"
  369. read DD 
  370. case $DD in
  371. 1)
  372. CustomizeFirewall
  373. ;;
  374. 2)
  375. PublickeyAuthenticate
  376. ;;
  377. *)
  378. echo "Quit"
  379. break
  380. ;;
  381. esac
  382. }
  383.  
  384. ManageUser ()
  385. {
  386. while true
  387. do
  388. ManageUserMenu
  389. done
  390. }
  391.  
  392.  
  393. ManageServices(){
  394. while true
  395. do
  396. ManageServicesMenu
  397. done
  398. }
  399. ManageFirewall()
  400. {
  401. while true
  402. do
  403. ManagerFirewallMenu
  404. done
  405. }
  406.  
  407. Quit()
  408. {
  409. echo "Quit"
  410. break
  411. }
  412.  
  413. while true 
  414. do
  415. MainMenu
  416. done

你可能感兴趣的:(开发,shell,脚本,休闲,自动化系统)