作为初学者,要想取得进步,成为高手,首先应该了解自己的不足之处.
全部博文(117)
分类: 系统运维
2011-01-20 14:43:05
部分代码解析:
#!/usr/bin/python
'''The rsync_server
config'''
注意区别三个'''与一个'的区别:一个',表示单行的注释,三个''',表示多行的注释,不用使用换行符
import os,stat,sys,time
导入模块
now=time.strftime('%Y%m%d%H%M')
cfgrsyncd='''
#rsyncd#
uid = root
gid = root
use chroot = no
max connections = 5
lock file = /var/log/rsync/rsyncd.lock
log file = /var/log/rsync/rsyncd.log
pid file = /var/log/rsync/rsyncd.pid
hosts allow = 192.168.1.9
hosts deny = *
ignore errors
read only = yes
list = no
auth users = username
secrets file = /etc/.passwd\n
'''
cfgxrsync='''
service rsync
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon --config=/etc/rsyncd.conf
log_on_failure += USERID
}'''
定义时间等变量
print 'First,Please Install Xinetd And Rsync Server \n'
在执行脚本的时候显示
#A function definition of xinetd-rsync
def cfgxrs():
if os.path.exists('/etc/xinetd.d/rsync'):
cmd_mv='mv /etc/xinetd.d/rsync /etc/xinetd.d/rsync.%s' % (now)
os.system(cmd_mv)
content=file('/etc/xinetd.d/rsync','w')
content.write(cfgxrsync)
content.close()
函数功能:
1·创建xinetd启动的rsync文件
2·首先检查文件是否存在,如果存在则执行mv命令
3·否则,就以写入的模式,添加"cfgxrsync"定义的内容
#A function definition of password-file
def cfgpwd():
cfgpasswd='''backup:passwd'''
content=file('/etc/.rsyncd.passwd','w')
content.write(cfgpasswd)
content.close()
os.chmod('/etc/.rsyncd.passwd', stat.S_IREAD)
函数功能:
1·创建密码文件
2·修改密码文件权限,stat.S_IREAD代表600
#A function definition of rsyncd
def cfgsrv():
if os.path.exists('/etc/rsyncd.conf'):
cmd_mv='mv /etc/rsyncd.conf /etc/rsyncd.conf.%s' % (now)
os.system(cmd_mv)
if os.path.exists('/var/log/rsync'):
print '/var/log/rsync is already established.'
else:
os.mkdir('/var/log/rsync')
content=file('/etc/rsyncd.conf','w')
content.write(cfgrsyncd)
content.close()
函数功能:
1·创建rsyncd.conf配置文件
2·创建日志目录
#A function definition of '/etc/rsyncd.conf' is module append
def cfgapd():
try:
try:
content=file('/etc/rsyncd.conf','a')
mod=raw_input('please enter the module: \n>')+'\n'
content.write(mod)
pth='path='+raw_input('please enter the path:> \n>')+'\n'
content.write(pth)
content.close()
except:
print 'Please enter a correct Module and Path \n'
sys.exit()
print 'Scripting termination !! \n'
finally:
content.close()
函数功能:
1·手动输入模块名
2·增加交互性。不便于使用,所以下面没有调用
3·try...finally,不管try段如何,都运行finally段的代码
#Display file content
def cfgdpy():
print '\nPrint ''/etc/rsyncd.conf'' content \n'
time.sleep(1)
content=file('/etc/rsyncd.conf','r')
while True:
line = content.readline()
if len(line) == 0:
break
print line,
content.close()
函数功能:
1·显示配置文件内容
2·while True循环,把每一行都赋给line,if len(line) == 0:表示到了最后一行
def srvrst():
print '\nexec /etc/init.d/xinetd restart'
cmd='/etc/init.d/xinetd restart'
time.sleep(2)
os.system(cmd)
重启
#Function call
cfgxrs()
cfgsrv()
cfgpwd()
#cfgapd()
cfgdpy()
#srvrst()