免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5958 | 回复: 5
打印 上一主题 下一主题

python脚本,关于自动配置rsync-server [复制链接]

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:55:28IT运维版块每日发帖之星
日期:2016-07-29 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-14 21:00 |只看该作者 |倒序浏览
简单介绍:自动生成rsync配置文件和同步所需要的密码文件
操作说明:交互过程有提示,只需要输入“模块名”和“备份路径”
脚本内容如下:
希望大家给些改进意见

#!/usr/bin/python
'''The rsync_server config'''
import os
import stat
import sys
import time
now=time.strftime('%Y%m%d%H%M')
print 'First,Please Install Xinetd And Rsync Server \n'
#A function definition of xinetd-rsync
def cfgxrs():
    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
}'''
    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()
#A function definition of password-file
def cfgpwd():
    cfgpasswd='''ning:123456'''
    content=file('/etc/.rsyncd.passwd','w')
    content.write(cfgpasswd)
    content.close()   
    os.chmod('/etc/.rsyncd.passwd', stat.S_IREAD)
#A function definition of rsyncd
def cfgsrv():
    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 = 127.0.0.1 192.168.60.134
hosts deny = *
ignore errors
read only = yes
list = no
auth users = ning
secrets file = /etc/.rsyncd.passwd
transfer logging = ture \n'''
    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()

#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()
#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()
def srvrst():
    print '\nexec /etc/init.d/xinetd reload'
    cmd='/etc/init.d/xinetd reload'
    time.sleep(2)
    os.system(cmd)
#Function call
cfgxrs()
cfgsrv()
cfgpwd()
cfgapd()
cfgdpy()
srvrst()

论坛徽章:
0
2 [报告]
发表于 2010-10-15 22:37 |只看该作者
不要交互。

论坛徽章:
0
3 [报告]
发表于 2010-10-15 22:38 |只看该作者
哦,再用代码覆盖工具测试一下你的代码。

论坛徽章:
0
4 [报告]
发表于 2010-10-17 19:28 |只看该作者
我是新手,请教一下,三层引号内的内容,如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
}'''
引号的内容是doncument吗,它是不是不属于程序代码的一部分,在运行时不会执行是吗?麻烦给解释一下。

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:55:28IT运维版块每日发帖之星
日期:2016-07-29 06:20:00
5 [报告]
发表于 2010-10-28 10:06 |只看该作者
回复 4# vividvivion


    就相当于文档标识符,不会当成代码来执行

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:55:28IT运维版块每日发帖之星
日期:2016-07-29 06:20:00
6 [报告]
发表于 2010-10-28 18:05 |只看该作者
回复 2# icykiss


    嗯,交互确实感到麻烦了,所以取消了srvrst()和srvapn()两个函数
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP