Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1862391
  • 博文数量: 117
  • 博客积分: 2559
  • 博客等级: 少校
  • 技术积分: 4385
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-13 20:08
个人简介

作为初学者,要想取得进步,成为高手,首先应该了解自己的不足之处.

文章分类

全部博文(117)

文章存档

2014年(1)

2013年(25)

2012年(13)

2011年(77)

2010年(1)

分类: 系统运维

2011-01-20 14:43:05

  1. #!/usr/bin/python

  2. '''The rsync_server
  3. config'''

  4. import os,stat,sys,time

  5. now=time.strftime('%Y%m%d%H%M')
  6. cfgrsyncd='''
  7. #rsyncd#
  8. uid = root
  9. gid = root
  10. use chroot = no
  11. max connections = 5
  12. lock file = /var/log/rsync/rsyncd.lock
  13. log file = /var/log/rsync/rsyncd.log
  14. pid file = /var/log/rsync/rsyncd.pid
  15. hosts allow = 192.168.0.9
  16. hosts deny = *
  17. ignore errors
  18. read only = yes
  19. list = no
  20. auth users = username
  21. secrets file = /etc/.passwd\n
  22. '''

  23. cfgxrsync='''
  24. service rsync
  25. {
  26.     disable = no
  27.     socket_type = stream
  28.     wait = no
  29.     user = root
  30.     server = /usr/bin/rsync
  31.     server_args = --daemon --config=/etc/rsyncd.conf
  32.     log_on_failure += USERID
  33. }'''

  34. print 'First,Please Install Xinetd And Rsync Server \n'

  35. #A function definition of xinetd-rsync
  36. def cfgxrs():
  37.     if os.path.exists('/etc/xinetd.d/rsync'):
  38.         cmd_mv='mv /etc/xinetd.d/rsync /etc/xinetd.d/rsync.%s' % (now)
  39.         os.system(cmd_mv)
  40.     content=file('/etc/xinetd.d/rsync','w')
  41.     content.write(cfgxrsync)
  42.     content.close()

  43. #A function definition of password-file
  44. def cfgpwd():
  45.     cfgpasswd='''backup:passwd'''
  46.     content=file('/etc/.passwd','w')
  47.     content.write(cfgpasswd)
  48.     content.close()
  49.     os.chmod('/etc/.rsyncd.passwd', stat.S_IREAD)

  50. #A function definition of rsyncd
  51. def cfgsrv():

  52.     if os.path.exists('/etc/rsyncd.conf'):
  53.         cmd_mv='mv /etc/rsyncd.conf /etc/rsyncd.conf.%s' % (now)
  54.         os.system(cmd_mv)
  55.     if os.path.exists('/var/log/rsync'):
  56.         print '/var/log/rsync is already established.'
  57.     else:
  58.         os.mkdir('/var/log/rsync')
  59.     content=file('/etc/rsyncd.conf','w')
  60.     content.write(cfgrsyncd)
  61.     content.close()

  62. #A function definition of '/etc/rsyncd.conf' is module append
  63. def cfgapd():
  64.     try:
  65.         try:
  66.             content=file('/etc/rsyncd.conf','a')
  67.             mod=raw_input('please enter the module: \n>')+'\n'
  68.             content.write(mod)
  69.             pth='path='+raw_input('please enter the path:> \n>')+'\n'
  70.             content.write(pth)
  71.             content.close()
  72.         except:
  73.             print 'Please enter a correct Module and Path \n'
  74.             sys.exit()
  75.             print 'Scripting termination !! \n'
  76.     finally:
  77.         content.close()

  78. #Display file content
  79. def cfgdpy():
  80.     print '\nPrint ''/etc/rsyncd.conf'' content \n'
  81.     time.sleep(1)
  82.     content=file('/etc/rsyncd.conf','r')
  83.     while True:
  84.         line = content.readline()
  85.         if len(line) == 0:
  86.             break
  87.         print line,
  88.     content.close()

  89. def srvrst():
  90.     print '\nexec /etc/init.d/xinetd restart'
  91.     cmd='/etc/init.d/xinetd restart'
  92.     time.sleep(2)
  93.     os.system(cmd)

  94. #Function call
  95. cfgxrs()
  96. cfgsrv()
  97. cfgpwd()
  98. #cfgapd()
  99. cfgdpy()
  100. #srvrst()

部分代码解析:

#!/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()

阅读(2238) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~