Python redis-cli.py Python3 redis-cli Command-line administration tool
Due to the recent test of redis unauthorized access vulnerability, found that the machine does not have Redis installed, can not run redis-cli, so he wrote a simple redis-cli terminal, the function is not complete, just support simple landing and other operations.
mini redis-cli tools
After my tests, it is still possible to execute a simple command.
The code has been uploaded to GitHub https://github.com/b4zinga/PythonTools/blob/master/redis-cli.py
#!/usr/bin/env Python# Coding:utf-8# date:2018-03-20 13:45:00# Author:b4zinga# Email: [Email protected]# Function:mini REDIS-CLI ToolsImportSocketImportSysImportResocket.setdefaulttimeout (3)defSendCommand (host, port): sock=Socket.socket ()Try: sock.Connect((Host,port))except Exception asErr:Print(Err) Sys.exit (0) while True: cmd= input(' > ')ifCmd== ' exit ': BreakCmd=Makecmd (CMD)Try: Sock.send (Cmd.encode ()) while True: recv=SOCK.RECV (1024x768)Print(Handlerecv (recv))if Len(recv)<1024x768:# Loop receives 1024, if the length is less than 1024 the default is no content, break Break except Exception asErr:Print(err) Sock.close ()defMakecmd (cmd): command= "*"Cmd=Cmd.split () command=Command+ Str(Len(CMD))+ '\ r \ n' forCinchCmd:command=Command+ ' $ ' + Str(Len(c))+ '\ r \ n' +C+ '\ r \ n' returnCommanddefHANDLERECV (recvdate): recvdate=Recvdate.decode ()ifRecvdate.startswith (' * '): Recvdate=recvdate[2:].strip ('\ r \ n') recvdate=Re.sub (' \$\d+\\R\\N ',"', recvdate)returnRecvdateif __name__ == ' __main__ ': Usage="""====================================================Mini REDIS-CLI tools.Usage:redis-cli.py <IP> <port>Default IP 127.0.0.1Default Port 6379Example:redis-cli.py 127.0.0.1 6379redis-cli.py 127.0.0.1\ nredis-cli.py 192.168.1.133 6378==================================================== """ if '-H ' inchsys.argvor '--help ' inchsys.argvor Len(SYS.ARGV)> 3:Print(usage) Sys.exit (0)if Len(SYS.ARGV)== 1: Sys.argv.append (' 127.0.0.1 ')if Len(SYS.ARGV)== 2: Sys.argv.append (6379) SendCommand (sys.argv[1],int(sys.argv[2]))
redis-cli.py python redis-cli redis Management terminal