游戏服要经常进行调试时间来测试游戏,之前一直手动进行date命令修改后让测试进行游戏测试
现在用web页面让测试直接填写时间后进行表单提交修改时间
整个目录
[root@node02 script]# tree -DC python python ├── [Feb 15 0:01] cgi-bin │ └── [Feb 15 0:01] change_date.py ├── [ The Future ] cgihttpd.py └── [ The Future ] index.html
cgi服务端内容
[root@node02 python]# cat cgihttpd.py #coding:utf-8 __author__ = 'JYC103' from BaseHTTPServer import HTTPServer from CGIHTTPServer import CGIHTTPRequestHandler from SocketServer import ForkingMixIn class ForkingServer(ForkingMixIn,HTTPServer): pass serveraddr = ('',8001) srvr = ForkingServer(serveraddr,CGIHTTPRequestHandler) srvr.serve_forever()
首页显示页面
[root@node02 python]# cat index.html <html> <h1>modify system data web</h1> <form name="input"action="/cgi-bin/change_date.py"method="get"> <!--hour: <input type="text"name="hour"><br><br>--> mon: <input type="text"name="mon"><br><br> day: <input type="text"name="day"><br><br> <input type="submit"value="Submit"> </form> </html>
修改系统时间的脚本,要放在cgi-bin目录下
[root@node02 python]# cat cgi-bin/change_date.py #!/usr/bin/python import cgi,os import getpass form = cgi.FieldStorage() print"Content-Type: text/html" print"" print"<html>" print"<h2>System Date</h2>" print"<p>" print"The user entered data:<br>" print"<b>mon:</b> "+ form["mon"].value +"<br>" print"<b>day:</b> "+ form["day"].value +"<br>" mon = int(form["mon"].value ) day = int(form["day"].value) if ((1<=int(mon)<=12) and (1<=int(day)<=31)): newtime = os.popen(r"sudo date -s '2015-%s-%s 23:59:55'" %(mon,day)).readlines()[0] print newtime else: print "error time input" print"</p>" print"</html>"
这些内容写完还不算完成。
因为运行这个cgi内容的是nobody用户
所以必须给nobody有对date命令可执行权限
直接在/etc/sudoers修改
[root@node02 etc]# chmod u+x sudoers [root@node02 etc]# vim sudoers ## Allow root to run any commands anywhere rootALL=(ALL) ALL #nobodyALL=(ALL) ALL nobody ALL=(ALL) NOPASSWD:/bin/date [root@node02 etc]# chmod u-x sudoers
要在rootALL=(ALL) ALL下添加一行
nobody ALL=(ALL) NOPASSWD:/bin/date就行
注意sudoers文件的权限
在chmod u+x修改文件之后要把权限去掉chmod u-x,否则不能正常使用sudo 命令
PS:查阅资料得知修改sudoers文件真正的用法应该用visudo方式修改
因/etc/sudoers有语法限定,若改错会有不良后果,所以才用visudo修改,在修改结束离开时,系统会自动检测/etc/sudoers的语法正确性,这样就保证系统的安全。
效果图
修改后的时间
转载于:https://blog.51cto.com/alwaysyunwei/1609345