Skip to content

Instantly share code, notes, and snippets.

@iwiwi
Last active December 17, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwiwi/5640749 to your computer and use it in GitHub Desktop.
Save iwiwi/5640749 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# sockets.rb --- by Takuya Akiba (@iwiwi)
#
# Usage:
# sockets.rb PID
#
`ls -l /proc/#{ARGV[0]}/fd | grep [s]ocket`.scan(/\[([0-9]+)\]/).each_with_index do |m, i|
id = m[0]
res = `perl -lane 'print $F[2] if($F[9] == #{id})' /proc/net/tcp`.strip
ip = port = name = ""
if res != ""
ip = res.split(":")[0].unpack("a2" * 4).map{|s| s.hex}.reverse.join(".")
port = res.split(":")[1].hex
name = `dig -x #{ip} | grep -A 1 'ANSWER SECTION'`.split[-1]
end
puts [i, ip, port, name].join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment