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

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

此工具具有如下功能:

(一)用户管理
1、修改root密码
2、删除用户帐号
3、添加用户帐号
(二)服务管理
1、开启服务
2、关闭服务
(三)防火墙/ssh认证管理
1、关闭默认防火墙,开启自定防火墙脚本(自定义脚本分为:公司环境下,以及互联网环境下)
2、修改ssh认证配置文件(采用publickey认证登录)
(四)自动设置
1、自动添加"互联网环境下"的防火墙
2、采用publickey认证登录
(五)重启功能

所有这些功能都是以函数块做的,大家可以根据自己的需求做出相应的调整以适应自己公司的需求.

开发os:centos5.2
脚本:shell
功能已经基本测试OK,不过。还需要其他朋友挖Bug...

贴些图让大家更直观点:

 

 

 

 AutoSetSystem.sh

 
 
  1. #!/bin/bash 

  2. ######################################################################### 

  3. # File:         autosetsystem.sh 

  4. # Description:   

  5. # Language:     GNU Bourne-Again SHell 

  6. # Version:  1.1 

  7. Date:     2010-9-6 

  8. # WWW:      http://5ydycm.blog.51cto.com/

  9. ############################################################################### 

  10.  

  11. zzj_key='zzjkey' 

  12.  

  13. 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" 

  14. public_ip="ip1 ip2 ip3" 

  15. private_ip="ip1 ip2 ip3 ip4" 

  16.  

  17. MainMenu() 

  18. clear 

  19. echo 

  20. echo "-------------------------------------------------------------------------" 

  21. tput cup 2 

  22. time=`date +"%Y-%m-%d"

  23. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time" 

  24. echo 

  25. tput cup 3 

  26. echo  "-------------------------------------------------------------------------" 

  27. tput cup 4 20 

  28. echo -e "1:Manage User;" 

  29. tput cup 5 20 

  30. echo -e "2:Manage Services;" 

  31. tput cup 6 20 

  32. echo -e "3:Manage Firewall/SSH;" 

  33. tput cup 7 20 

  34. tput bold 

  35. echo -e "4:AutoSet;" 

  36. tput sgr0 

  37. tput cup 8 20 

  38. echo -e "5:Reboot;" 

  39. tput cup 9 20 

  40. echo -e "6:Quit;" 

  41. tput cup 10  

  42. echo  "--------------------------------------------------------------------------" 

  43. echo -n "You choice [1,2,3,4,5,6]:" 

  44. read AA 

  45. case $AA in 

  46. 1) 

  47. ManageUser 

  48. ;; 

  49. 2) 

  50. ManageServices 

  51. ;; 

  52. 3) 

  53. ManageFirewall 

  54. ;; 

  55. 4) 

  56. AutoSet 

  57. ;; 

  58. 5) 

  59. echo -n "Are you sure reboot system[y|n]?" 

  60. read answer 

  61. if [ $answer == "y" ];then 

  62. shutdown -r now 

  63. exit 0 

  64. else 

  65. echo -n "You forego reboot system!" 

  66. sleep 2 

  67. fi 

  68. ;; 

  69. *) 

  70. Quit 

  71. ;; 

  72. esac 

  73. AutoSet(){ 

  74. EnableOutFirewall 

  75. echo -e "\n" 

  76. PublickeyAuthenticate 

  77.  

  78. AddUser(){ 

  79. echo -n "Please input add user name:" 

  80. read username 

  81. (awk -F':' '{print $1}' /etc/passwd|grep ^$username$) && (echo "Add user faild because user exists!"&&sleep 2)||(useradd $username&&passwd $username&&sleep 2)} 

  82.  

  83. DeleteUser(){ 

  84. echo -n "Please input delete user name:" 

  85. read username 

  86. echo -n "Are you sure delete $username[y|n]?" 

  87. read answer 

  88. if [ $answer == "y" ];then 

  89. (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) 

  90. else 

  91. echo -n "You forego delete $username account!" 

  92. sleep 2 

  93. fi 

  94.  

  95. ModifyRootpwd(){ 

  96. echo -n "Are you sure modify root password[y|n]?" 

  97. read answer 

  98. if [ $answer == "y" ];then 

  99. passwd root 

  100. sleep 2 

  101. else 

  102. echo -n "You forego modify root password!" 

  103. sleep 2 

  104. fi 

  105.  

  106. ViewUser(){ 

  107. more /etc/passwd 

  108. tput bold  

  109. echo "Wait 8 sec!" 

  110. sleep 8  

  111. tput sgr0  

  112.  

  113. ManageUserMenu(){ 

  114. clear 

  115. echo 

  116. echo "-------------------------------------------------------------------------" 

  117. tput cup 2 

  118. time=`date +"%Y-%m-%d"

  119. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time" 

  120. echo 

  121. tput cup 3 

  122. echo  "-------------------------------------------------------------------------" 

  123. tput cup 4 20 

  124. echo -e "1:Add User;" 

  125. tput cup 5 20 

  126. echo -e "2:Delete User;" 

  127. tput cup 6 20 

  128. echo -e "3:Modify root password;" 

  129. tput cup 7 20 

  130. echo -e "4:View User;" 

  131. tput cup 8 20 

  132. echo -e "5:Quit;" 

  133. tput cup 9  

  134. echo  "--------------------------------------------------------------------------" 

  135. echo -n "You choice [1,2,3,4,5]:" 

  136. read BB  

  137. case $BB in 

  138. 1) 

  139. AddUser 

  140. ;; 

  141. 2) 

  142. DeleteUser 

  143. ;; 

  144. 3) 

  145. ModifyRootpwd 

  146. ;; 

  147. 4) 

  148. ViewUser 

  149. ;; 

  150. *) 

  151. echo "Quit" 

  152. break 

  153. ;; 

  154. esac 

  155.  

  156. EnableServices(){ 

  157. echo -n "Please input enable service name:" 

  158. read servicename 

  159. echo -n "Are you sure enable $servicename[y|n]?" 

  160. read answer 

  161. if [ $answer == "y" ];then 

  162. (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) 

  163. else 

  164. echo -n "You forego enable $servicename!" 

  165. sleep 2 

  166. fi 

  167.  

  168. DisableServices(){ 

  169. echo -n "Please input disable service name:" 

  170. read servicename 

  171. echo -n "Are you sure disable $servicename[y|n]?" 

  172. read answer 

  173. if [ $answer == "y" ];then 

  174. (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) 

  175. else 

  176. echo -n "You forego disable $servicename!" 

  177. sleep 2 

  178. fi 

  179.  

  180. ViewServices(){ 

  181. chkconfig --list 

  182. tput bold 

  183. echo "Wait 8 sec!" 

  184. sleep 8 

  185. tput sgr0 

  186.  

  187. ManageServicesMenu(){ 

  188. clear 

  189. echo 

  190. echo "-------------------------------------------------------------------------" 

  191. tput cup 2 

  192. time=`date +"%Y-%m-%d"

  193. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time" 

  194. echo 

  195. tput cup 3 

  196. echo  "-------------------------------------------------------------------------" 

  197. tput cup 4 20 

  198. echo -e "1:Enable Services;" 

  199. tput cup 5 20 

  200. echo -e "2:Disable Services;" 

  201. tput cup 6 20 

  202. echo -e "3:View Services;" 

  203. tput cup 7 20 

  204. echo -e "4:Quit;" 

  205. tput cup 8  

  206. echo  "--------------------------------------------------------------------------" 

  207. echo -n "You choice [1,2,3,4]:" 

  208. read CC  

  209. case $CC in 

  210. 1) 

  211. EnableServices 

  212. ;; 

  213. 2) 

  214. DisableServices 

  215. ;; 

  216. 3) 

  217. ViewServices 

  218. ;; 

  219. *) 

  220. echo "Quit" 

  221. break 

  222. ;; 

  223. esac 

  224.  

  225. EnableCompanyFirewall(){ 

  226. echo -n "Are you sure enable firewall[y|n]?" 

  227. read answer 

  228. if [ $answer == "y" ];then 

  229. if [ ! -d /scripts ];then 

  230. mkdir /scripts 

  231. fi 

  232. echo -e $general_iptable_content >/scripts/start_firewall.sh 

  233. for ip in $private_ip 

  234. do 

  235. echo "/sbin/iptables -A INPUT -s $ip -p tcp --dport 22 -j ACCEPT" >>/scripts/start_firewall.sh 

  236. done 

  237. echo "sh /scripts/start_firewall.sh" >>/etc/rc.local 

  238. chmod +x /scripts/start_firewall.sh 

  239. sh /scripts/start_firewall.sh 

  240. echo "Enable Firewall sucessful!" 

  241. sleep 3 

  242. else 

  243. echo -n "You forego enable firewall!" 

  244. sleep 2 

  245. fi 

  246. EnableOutFirewall(){ 

  247. echo -n "Are you sure enable firewall[y|n]?" 

  248. read answer 

  249. if [ $answer == "y" ];then 

  250. if [ ! -d /scripts ];then 

  251. mkdir /scripts 

  252. fi 

  253. echo -e $general_iptable_content >/scripts/start_firewall.sh 

  254. for ip in $public_ip 

  255. do 

  256. echo "/sbin/iptables -A INPUT -s $ip -p tcp --dport 22 -j ACCEPT" >>/scripts/start_firewall.sh 

  257. done 

  258. echo "sh /scripts/start_firewall.sh" >>/etc/rc.local 

  259. chmod +x /scripts/start_firewall.sh 

  260. sh /scripts/start_firewall.sh 

  261. echo "Enable Firewall sucessful!" 

  262. sleep 3 

  263. else 

  264. echo -n "You forego enable firewall!" 

  265. sleep 2 

  266. fi 

  267.  

  268. FirewallEnvMenu(){ 

  269. clear 

  270. echo 

  271. echo "-------------------------------------------------------------------------" 

  272. tput cup 2 

  273. time=`date +"%Y-%m-%d"

  274. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time" 

  275. echo 

  276. tput cup 3 

  277. echo  "-------------------------------------------------------------------------" 

  278. tput cup 4 20 

  279. echo -e "1:Enable Company Env Firewall;" 

  280. tput cup 5 20 

  281. echo -e "2:Enable Out Env Firewall;" 

  282. tput cup 6 20 

  283. echo -e "3:Quit;" 

  284. tput cup 7  

  285. echo  "--------------------------------------------------------------------------" 

  286. echo -n "You choice [1,2,3]:" 

  287. read EE  

  288. case $EE in 

  289. 1) 

  290. EnableCompanyFirewall 

  291. ;; 

  292. 2) 

  293. EnableOutFirewall 

  294. ;; 

  295. *) 

  296. echo "Quit" 

  297. break 

  298. ;; 

  299. esac 

  300. CustomizeFirewall() 

  301. while true 

  302. do 

  303. FirewallEnvMenu 

  304. done 

  305. PublickeyAuthenticate() 

  306. echo -n "Are you sure enable publickey auth[y|n]?" 

  307. read answer 

  308. if [ $answer == "y" ];then 

  309. if [ ! -d /root/.ssh ];then 

  310. mkdir /root/.ssh 

  311. fi 

  312. touch /root/.ssh/authorized_keys 

  313. echo $zzj_key >/root/.ssh/authorized_keys 

  314. cp /etc/ssh/sshd_config /tmp/sshd_config_bak 

  315. sed 's/^PasswordAuthentication yes$/PasswordAuthentication no/' /etc/ssh/sshd_config > /etc/ssh/tmp_sshd_config 

  316. sed 's/^#PubkeyAuthentication yes$/PubkeyAuthentication yes/' /etc/ssh/tmp_sshd_config > /etc/ssh/tmp1_sshd_config 

  317. sed 's/^#AuthorizedKeysFile/AuthorizedKeysFile/' /etc/ssh/tmp1_sshd_config > /etc/ssh/tmp_sshd_config 

  318. rm -fr /etc/ssh/sshd_config 

  319. rm -fr /etc/ssh/tmp1_sshd_config 

  320. mv /etc/ssh/tmp_sshd_config /etc/ssh/sshd_config 

  321. kill -HUP `cat /var/run/sshd.pid` 

  322. echo "Please use public key try login agains!" 

  323. sleep 5  

  324. else 

  325. echo -n "You forego publickey auth!" 

  326. sleep 2 

  327. fi 

  328.  

  329. ManagerFirewallMenu(){ 

  330. clear 

  331. echo 

  332. echo "-------------------------------------------------------------------------" 

  333. tput cup 2 

  334. time=`date +"%Y-%m-%d"

  335. echo -ne "USER:$USER\tHOST:$HOSTNAME\tdate:$time" 

  336. echo 

  337. tput cup 3 

  338. echo  "-------------------------------------------------------------------------" 

  339. tput cup 4 20 

  340. echo -e "1:Enable Customize Firewall;" 

  341. tput cup 5 20 

  342. echo -e "2:Enable Publickey Authenticate;" 

  343. tput cup 6 20 

  344. echo -e "3:Quit;" 

  345. tput cup 7  

  346. echo  "--------------------------------------------------------------------------" 

  347. echo -n "You choice [1,2,3]:" 

  348. read DD  

  349. case $DD in 

  350. 1) 

  351. CustomizeFirewall 

  352. ;; 

  353. 2) 

  354. PublickeyAuthenticate 

  355. ;; 

  356. *) 

  357. echo "Quit" 

  358. break 

  359. ;; 

  360. esac 

  361.  

  362. ManageUser () 

  363. while true 

  364. do 

  365. ManageUserMenu 

  366. done 

  367.  

  368.  

  369. ManageServices(){ 

  370. while true 

  371. do 

  372. ManageServicesMenu 

  373. done 

  374. ManageFirewall() 

  375. while true 

  376. do 

  377. ManagerFirewallMenu 

  378. done 

  379.  

  380. Quit() 

  381. echo "Quit" 

  382. break 

  383.  

  384. while true  

  385. do 

  386. MainMenu 

  387. done 

 

如果想了解更多,请关注我们的公众号
公众号ID:opdevos
扫码关注

gongzhouhao.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值