linux菜单脚本

  Python Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
#author lic(oldboy linux student)
#date 1304
DISK_NO= "/dev/sda1"
NGINX_DIR=
"/usr/local/nginx/sbin/nginx"
MYSQL_DIR=
"/usr/local/mysql/bin/mysqld_safe"
SERVER1=
"NGINX"
SERVER2=
"MYSQL"
SERVER1_START=
"/usr/local/nginx/sbin/nginx"
SERVER1_STOP=
"kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`"
SERVER1_RESTART=
"kill -HUP `cat /usr/local/nginx/logs/nginx.pid`"
SERVER2_START=
"/etc/init.d/mysql start"
SERVER2_STOP=
"/etc/init.d/mysql stop"
SERVER2_RESTART=
"/etc/init.d/mysql restart"
#获取当前时间
DATE=`date + "%y-%m-%d %H:%M:%S" `
#获取当前ip
IPADDR=`ifconfig|grep  "inet " |grep -v  "127.0.0.1" |awk  '{print $2}' `
#获取当前主机名
HOSTNAME=`hostname -s`
#获取当前用户
USER=`whoami`
#disk_check
DISK_SDA=`df -h grep $DISK_NO awk  '{print $5}' `
#cpu_average_check
cpu_uptime=`cat /proc/loadavg cut -c1- 14 `
#process_check

function process_check() {
for   dir   in  $NGINX_DIR $MYSQL_DIR
do
  process_count=$(ps -ef grep 
"$dir"  grep -v grep wc -l)        #wc -l 统计行数                                 过滤进程,计算启动进程的数量
   for  service  in  nginx mysql 
  do
  echo 
"$dir"  |grep -q  "$service"                                            #grep -q 如果匹配内容相同,则返回0
   if  $? -eq  0  ]
    then
           
if  $process_count -eq  0                                       #线程等于0,可以判断出服务没有启动,输出not run,如不等于0则认为已启动,输出running
                then
                       echo 
"$service.......................[NOT RUN]"
           
else
                       echo 
"$service.......................[RUNNING]"
                       
continue
            fi
  fi
  done
done
}
                                                                           
#服务判断部分结束
declare flag= 0                                              #declare 用与定义变量的属性                    这里flag定义成0,方便后面循环“起跳”,例如后面循环只要设成
clear                                                                                                                                                  #"如果flag等于零,则....."  这样可以执行循环体
while   "$flag"  -eq  0  ]
do
echo 
"========================================"
process_check                                             
#调用之前定义的服务判断部分
echo  "========================================"
cat << EOF                          
#cat << EFO          意思是告诉系统,把这中间的内容当成文本显示
|-------------系-统-信-息--------------
 时间            :$DATE                                        
 主机名称        :$HOSTNAME
 当前用户        :$USER                                        
IP地址          :$IPADDR
|
|
 磁盘使用        :$DISK_SDA
CPU使用率       :$cpu_uptime                       
----------------------------------------
|****请选择你要操作的项目:[
0 - 5 ]****|
----------------------------------------
(
1 设置  $SERVER1  Service
(
2 设置  $SERVER2  Service
(
3 设置  NETWORKE  Service
(
4 更改密码
(
5 查看日志
(
0 退出
EOF
read  -p  "请选择[0-5]: "   input
case $input 
in
#NGINX部分
1 )
clear
while   "$flag"  -eq  0  ]
do
cat << EOF
----------------------------------------
|****请选择你要操作的项目:[
0 - 3 ]****|
----------------------------------------
(
1 开启 $SERVER1 Service
(
2 关闭 $SERVER1 Service
(
3 重启 $SERVER1 Service
(
0 返回上一层
EOF
 
read  -p  "请选择[0-3]: "  input1
 case $input1 
in  
 
1 )
  echo -e 
"\n>>>>>>>>>>>$DATE Start $SERVER1" >>/log.txt
  $SERVER1_START 
2 >>/log.txt
  
if  $? ==  0  ];then
   echo 
"Start $SERVER1......................................................[OK]"
   echo -e 
"\n>>>>>>>>>>>$DATE Start $SERVER1 success" >>/log.txt
  
else  
   echo 
"Start $SERVER1......................................................[FAILED]"
   echo -e 
"\n>>>>>>>>>>>$DATE Start $SERVER1 faild" >>/log.txt
  fi
  sleep 
5
  
clear
  ;;
 
2  
  echo -e 
"\n>>>>>>>>>>>$DATE Stop $SERVER1" >>/log.txt
  $SERVER1_STOP 
2 >>/log.txt
  
if  $? ==  0  ];then
   echo 
"Stop $SERVER1.......................................................[OK]"
  
else  
   echo 
"Stop $SERVER1.......................................................[FAILED]"
  fi
  sleep 
5
  
clear
  ;;
 
3
  echo -e 
"\n>>>>>>>>>>>$DATE ReStart $SERVER1" >>/log.txt
  /usr/local/tdoa/php/sbin/php-fpm restart 
2 >>/log.txt
  $SERVER1_RESTART 
2 >>/log.txt
  
if  $? ==  0  ];then
   echo 
"Restart $SERVER1....................................................[OK]"
  
else  
   echo 
"Restart $SERVER1....................................................[FAILED]"
  fi
  sleep 
5
  
clear
  ;;
 
0
 
clear  
 
break
 ;;
 *) echo 
"----------------------------------"
    echo 
"|            警告!!!           |"
    echo 
"|    请 输 入 正 确 的 选 项      |"
    echo 
"----------------------------------"
 
for   in  `seq -w  5  - 1   1                                                 #倒计时10秒
   do
     echo -ne 
"\b\b$i" ;
     sleep 
1 ;
   done
 
clear
 ;;
 esac
 done
;;
#mysql service
2 )
clear
while   "$flag"  -eq  0  ]
do
cat << EOF
----------------------------------------
|****请选择你要操作的项目[
0 - 3 ]****|
----------------------------------------
(
1 开启 $SERVER2 Service
(
2 关闭 $SERVER2 Service
(
3 重启 $SERVER2 Service
(
0 返回上一层
EOF
 
read  -p  "请选择[0-3]: "  input2
 case $input2 
in  
 
1 )
  echo -e 
"\n>>>>>>>>>>>$DATE Start $SERVER2" >>/log.txt
  $SERVER2_START 
2 >>/log.txt
  
if  $? ==  0  ];then
   echo 
"Start $SERVER2......................................................[OK]"
  
else  
   echo 
"Start $SERVER2......................................................[FAILED]"
  fi
  sleep 
5
  
clear
  ;;
 
2 )
  echo -e 
"\n>>>>>>>>>>>$DATE Stop $SERVER2" >>/log.txt
  $SERVER2_STOP 
2 >>/log.txt
  
if  $? ==  0  ];then
   echo 
"Stop $SERVER2.......................................................[OK]"
  
else  
   echo 
"Stop $SERVER2.......................................................[FAILED]"
  fi
  sleep 
5
  
clear
  ;;
 
3
  echo -e 
"\n>>>>>>>>>>>$DATE ReStart $SERVER2" >>/log.txt
  $SERVER2_RESTART 
2 >>/log.txt
  
if  $? ==  0  ];then
   echo 
"Restart $SERVER2....................................................[OK]"
  
else  
   echo 
"Restart $SERVER2....................................................[FAILED]"
  fi
  sleep 
5
  
clear
  ;;
 
0
 
clear  
 
break
 ;;
 *) echo 
"----------------------------------"
    echo 
"|            警告!!!           |"
    echo 
"|    请 输 入 正 确 的 选 项      |"
    echo 
"----------------------------------"
 
for   in  `seq -w  5  - 1   1 `
   do
     echo -ne 
"\b\b$i" ;
     sleep 
1 ;
   done
 
clear
 ;;
 esac
 done
;;
#网络服务
3 )
clear
while   "$flag"  -eq  0  ]
do
cat << EOF
----------------------------------------
****请选择你要操作的项目[
0 - 3 ]****|
----------------------------------------
(
1 重启 NETWORK Service
(
2 配置 NETWORK Service
(
0 返回上一层
EOF
 
read  -p  "请选择[0-3]: "  input3
 case $input3 
in
 
1 )
   echo -e 
"\n>>>>>>>>>>>$DATE ReStart Network" >>/log.txt
  /etc/init.d/network restart  
2 >>/log.txt
  
if  $? ==  0  ];then
   echo 
"Restart Network.....................................................[OK]"
  
else  
   echo 
"Restart Network.....................................................[FAILED]"
  fi
  sleep 
5
  
clear
  ;;
 
2  
    
clear
  setup
  
clear
  ;;
 
0
 
clear  
 
break
 ;;
 *) echo 
"----------------------------------"
    echo 
"|            警告!!!           |"
    echo 
"|    请 输 入 正 确 的 选 项      |"
    echo 
"----------------------------------"
 
for   in  `seq -w  5  - 1   1 `
   do
     echo -ne 
"\b\b$i" ;
     sleep 
1 ;
   done
 
clear
 ;;
 esac
 done
;;
4 )
clear
while   "$flag"  -eq  0  ]
do
cat << EOF
----------------------------------------
|****请选择你要操作的项目[ 
0   3  ]****|
----------------------------------------
(
1 更改root用户密码
(
2 更改Samba服务器密码
(
0 返回上一层
EOF
 
read  -p  "请选择[0-3]: "  input5
 case $input5 
in
 
1 )
  echo -e 
"\n>>>>>>>>>>>$DATE Change System Administertor Root Passwd\n" >>/log.txt
  passwd root 
2 >>/log.txt
  
if  $? ==  0  ];then
   echo 
"Change ROOT Passwd.....................................................[OK]"
  
else  
   echo 
"Change ROOT Passwd.....................................................[FAILED]"
  fi
  sleep 
5
  
clear
  ;;
 
2
  echo -e 
"\n>>>>>>>>>>>$DATE Change Samba Passwd" >>/log.txt
  smbpasswd -a tdoa 
2 >>/log.txt
  
if  $? ==  0  ];then
   echo 
"Change Samba Passwd.....................................................[OK]"
  
else  
   echo 
"Change Samba Passwd.....................................................[FAILED]"
  fi
  sleep 
5
  
clear
  ;;
 
0 )
 
clear  
 
break
 ;;
 *) echo 
"----------------------------------"
    echo 
"|            警告!!!           |"
    echo 
"|    请 输 入 正 确 的 选 项      |"
    echo 
"----------------------------------"
    
for   in  `seq -w  5  - 1   1 `
       do
       echo -ne 
"\b\b$i" ;
       sleep 
1
    done 
    
clear
 ;;
esac
done
;;
5 )
clear
echo 
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo 
"|"
less /log.txt
echo 
"|"
echo 
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
;;
0 )
clear
exit 
0
;;
 *) echo 
"----------------------------------"
    echo 
"|            警告!!!                |"
    echo 
"|    请 输 入 正 确 的 选 项             |"
    echo 
"----------------------------------"
 
for   in  `seq -w  5  - 1   1 `
   do
     echo -ne 
"\b\b$i" ;
     sleep 
1 ;
   done
 
clear
;;
esac
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值