Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Last active May 25, 2016 10:16
Show Gist options
  • Save ifyouseewendy/be897c49d659eaaa88b6b59c8f14411e to your computer and use it in GitHub Desktop.
Save ifyouseewendy/be897c49d659eaaa88b6b59c8f14411e to your computer and use it in GitHub Desktop.
Get client ips of redis connection.
#!/usr/bin/env ruby
# Usage
#
# echo 'client list' | redis-cli -h $HOST -p $PORT | ./redis_client_ip.rb
#
# Reference
#
# http://www.jstorimer.com/blogs/workingwithcode/7766125-writing-ruby-scripts-that-respect-pipelines
# http://redis.io/commands/client-list
require 'set'
ips = Set.new
while input = ARGF.gets
input.each_line do |line|
# id=203767 addr=[::1]:1234 fd=921 name= age=9791 idle=348 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=exec
# id=203604 addr=10.0.0.1:1234 fd=364 name= age=22830 idle=1399 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=exec
addr = line.split[1].split('=')[1]
next if addr.start_with?('[')
ip = addr.split(':')[0]
ips << ip
end
end
p ips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment