Luocs玩Python之:批量杀掉Redis客户端连接

php中文网
发布: 2016-06-07 16:41:30
原创
2246人浏览过

用途: 批量删除当前连接redis的所有客户端,除了从库的ip和本机ip。 准备一个hosts.txt配置,里面按如下格式填写Redis集群信息# cat hosts.txt[test]h1 = 1.1.1.1h2 = 1.1.1.2h3 = 1.1.1.3[group1]h1 = 2.1.1.1h2 = 2.1.1.2h3 = 2.1.1.3# cat killclient.py

用途: 批量删除当前连接redis的所有客户端,除了从库的ip和本机ip。

AI Undetect
AI Undetect

让AI无法察觉,让文字更人性化,为文字体验创造无限可能。

AI Undetect 162
查看详情 AI Undetect
准备一个hosts.txt配置,里面按如下格式填写Redis集群信息
# cat hosts.txt
[test]
h1 = 1.1.1.1
h2 = 1.1.1.2
h3 = 1.1.1.3
[group1]
h1 = 2.1.1.1
h2 = 2.1.1.2
h3 = 2.1.1.3
# cat killclient.py 
#!/usr/bin/env python 
# -*- coding: UTF-8 -*-
###################################################### 
# @Author:       Luocs(XU)  
# @Create Date:  2014-12-18
######################################################
import os, sys, time, ConfigParser
def killclient(clicmd, host, port, s1, s2, myip):
    os.popen("cat /dev/null > /tmp/client.tmp")
    os.popen("%s -h %s -p %s client list >> /tmp/client.tmp" % (clicmd, host, port))
    f = open("/tmp/client.tmp")
    while 1:
        line = f.readline().strip()
        if not line:
            break
        myclient = os.popen("echo %s | grep -v -E '%s|%s|%s' | awk '{print $2}' | awk -F'=' '{print $2}'" % (line, s1, s2, myip)).read().strip()
        if myclient:
            print("Client %s killed!" % myclient)
            os.popen("%s -h %s -p %s client kill %s" % (clicmd, host, port, myclient))
def getMyip():
    myip = os.popen("/sbin/ifconfig | grep 'inet addr' | awk '{print $2}'").read()
    myip = myip[myip.find(':')+1:myip.find('\n')]
    return myip
def gethost(name):
    fp = ConfigParser.ConfigParser()
    fp.readfp(open('hosts.txt'))
    h1 = fp.get(name,"h1") # 主库的IP
    h2 = fp.get(name,"h2") # 从库IP
    h3 = fp.get(name,"h3") # 从库IP,有多少个可以继续写下去
    return h1, h2, h3
def main():
    name = sys.argv[1]
    port = sys.argv[2]
    #port = 7001 # 也可以写死
    clicmd = "/home/luocs/redis-cli" # redis-cli
    h1, h2, h3 = gethost(name)
    host = h1 # 主库ip传给host
    s1 = h2 
    s2 = h3
    myip = getMyip()
    killclient(clicmd, host, port, s1, s2, myip)
if __name__ == '__main__':
    main()
执行结果:
# python killclient.py test 7001
Client 127.0.0.1:58263 killed!
Client 127.0.0.1:58264 killed!
Client 127.0.0.1:58265 killed!
Client 127.0.0.1:58266 killed!
Client 127.0.0.1:58267 killed!
Client 127.0.0.1:58268 killed!
Client 127.0.0.1:58269 killed!
登录后复制
相关标签:
python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号