#!/usr/bin/python #coding:utf8 import sys import commands,time,re ##自定义redis链接类 from redismodel import my_redis import argparse def get_processlist(host,port,passwd): try: #redisconn=redis.StrictRedis(host=host, port=int(port),password=passwd) clientlist=redisconn.conn.client_list() #processlist=list(set([ i["addr"].split(":")[0] for i in clientlist])) return clientlist except Exception as error: print error print "print redis connect error" return False exit(0) if __name__ == '__main__': parser = argparse.ArgumentParser(description='kill redis connection') parser.add_argument('-s','--host', type=str,required=True,help="ip") parser.add_argument('-p','--port', type=int,required=True,help="数据库端口") parser.add_argument('-a','--passwd', type=str,required=False,help="密码") args = parser.parse_args() host = args.host dbport = args.port passwd = '' passwd = args.passwd redisconn=my_redis(host,dbport,passwd) processlist=get_processlist(host,dbport,passwd) #print processlist for row in processlist : if row["cmd"]!="replconf" and row["cmd"]!="client": print "kill %s " % (row["addr"]) redisconn.conn.client_kill(row["addr"]) elif row["cmd"]=="replconf": print "从库 %s" % (row["addr"].split(":")[0]) clientlist=get_processlist(host,dbport,passwd) print "当前链接的ip:" print list(set([ i["addr"].split(":")[0] for i in clientlist]))