全文如下:最初的版本,实现的功能有限,也少必要的参数检测,持续更行中!
用到了构建命令行工具的模块optparse.文字处理string.对文件描述符的操作。
#!/usr/bin/env python
#
#akpw_grub.py:append kernel-parameters with grub.conf
#
# (in alphabetical order...)
#
#lijiansheng(lijiangsheng1@gmail.com)
#
# Maybe And many others
#
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import optparse
import ConfigParser
import re,os,sys
import string
f = open('/etc/grub.conf','r')
clines = f.readlines()
f.close()
dictory =[
("noacpi"),
("acpi=off")
]
def list_current():
print """************************below is your current running Linux OS kernel-parameterse************************
"""
if os.access("/proc/cmdline", os.R_OK):
path = "/proc/cmdline"
else:
path = None;
if path is not None:
f = open(path, "r")
lines = f.readlines()
parameter=lines[0]
print parameter
#next regular match so output all kernel-parameters
print """********************below is your current grub.conf about kernel-parameterse*****************************
"""
if os.access("/etc/grub.conf", os.R_OK):
grub = "/etc/grub.conf"
else:
grub = None;
if grub is not None:
f = open(grub, "r")
lines = f.readlines()
for i in xrange(len(lines)):
if lines[i][0] != "#":
line = string.strip(lines[i])
try:
line = string.split(line,'#')[0]
except: pass
tokens = string.split(line)
# if tokens[0] == "kernel":
if lines[i][1] == "k" or lines[i][0] == "k":
print lines[i]
f.close()
# print tokens[i]
if os.access("/etc/grub.conf" , os.W_OK):
print """****************************************Now, go on
"""
else:
print """unknown error
"""
append_new()
def append_new():
selection = raw_input("Please Input Your Kernel-Parameter:\n")
print ("your input kernel parameter is " + selection )
yes_no = raw_input("are you sure add this parameter?(yes/no) " )
if yes_no == "yes":
fd = open('/etc/grub.conf', 'r')
clines = fd.readlines()
for i in xrange(len(clines)):
if clines[i][0] != "#":
line = string.strip(clines[i])
try:
line = string.split(line,'#')[0]
except: pass
tokens = string.split(line)
# for strline in clines:
if clines[i][1] == "k" or clines[i][0] == "k":
# if strline[:6] == "kernel":
stringline = line.replace(line,line + ' %s\n' % selection)
print stringline
fd.close()
fd1 = open('/etc/grub.conf', 'w')
for i in xrange(len(clines)):
if clines[i][0] != "#":
line = string.strip(clines[i])
try:
line = string.split(line,'#')[0]
except: pass
tokens = string.split(line)
if clines[i][1] == "k" or clines[i][0] == "k":
stringline = line.replace(line,line + ' %s\n' % selection)
fd1.writelines(stringline)
else:
if clines[i][0] != "#":
line = string.strip(clines[i])
try:
line = string.split(line,'#')[0]
except: pass
fd1.writelines(['%s' % line + '\n'])
# print 'next is your changed kernel %s' % stringline
fd1.close()
def main():
"""Welcome to use redflag server,enjoy it! """
p = optparse.OptionParser(description='change your boot kernel-parameterse and append your configure file,like grub',
prog='akpw_grub',
version='akpw_grub 0.1;write by lijiangsheng1@gmail.com',
usage= '%prog [options] arg ')
p.add_option('--add','-a',help='append kernel-parameters and work immediately.')
p.add_option('--list','-l',default=True,help='list current kernel-parameters')
options, arguments = p.parse_args()
if options.add:
append_new()
elif options.list:
list_current()
if __name__ == '__main__':
if os.getuid() != 0:
print """you must be run this program as root!!"""
sys.exit(10)
main()
阅读(1639) | 评论(1) | 转发(0) |