python管理rsync配置文件

24 篇文章 0 订阅

1、新增用户和密码

def add_rsync_user_and_password(username):
    password_file_path = "/etc/rsync/pass.pass"
    try:
        f = open(password_file_path,"r")
        infos = f.read().split("\n")
    except:
        import traceback
        traceback.print_exc()
    else:
        f.close()
        d = dict([ elem.split(":") for elem in infos if elem ])
        if username not in d: #不存在
            raw_pwd = get_random_password(8)
            d[username] = raw_pwd
            s = "\n".join(["%s:%s"%(k,v) for k,v in d.items()])
            f =  open(password_file_path,"w")
            f.write(s)
            f.close()
            
            auth_file_path = "/etc/rsyncd.conf"

            #匹配rsync文件中配置用户的行,检查并增加用户到目录行中
            try:
                f = open(auth_file_path,"r")
                s = f.read()
            except:
                pass
            else:
                f.close()
                def add_user(x):
                    d = x.groupdict()
                    v = d["value"].split(",")
                    if username not in v:
                        v.append(username)
                    return d["key"]+ ",".join(v)

                s2 = re.sub(r"(?P<key>auth users.*=)(?P<value>.*)", add_user, s)
                f = open(auth_file_path,"w")
                f.write(s2)
                f.close()
        else:
            print ("YES_EXISTS")

1、删除用户密码

    def delete_password(username):
        password_file_path = "/etc/rsync/pass.pass"
        try:
            f = open(password_file_path,"r")
            infos = f.read().split("\n")
        except:
            import traceback
            traceback.print_exc()
        else:
            f.close()
            d = dict([ elem.split(":") for elem in infos if elem ])
            if self.username in d: #不存在
                del d[self.username]
                self.server_client_pwd = ""
                self.save()
                s = "\n".join(["%s:%s"%(k,v) for k,v in d.items()])
                f =  open(password_file_path,"w")
                f.write(s)
                f.close()
                print("rsync username='%s' exist delete ok"%username)
                return True
            else:
                return False

2、删除配置文件用户

def delete_user(username):
        auth_file_path = "/etc/rsyncd.conf"
        try:
            f = open(auth_file_path,"r")
            s = f.read()
        except:
            pass
        else:
            f.close()

            def delete_user(x):
                d = x.groupdict()
                v = d["value"].split(",")
                if username in v:
                    v.remove(self.username)

                return d["key"]+ ",".join(v)

            s2 = re.sub(r"(?P<key>auth users.*=)(?P<value>.*)", delete_user, s)
            f = open(auth_file_path,"w")
            f.write(s2)
            f.close()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值