Last active
December 17, 2015 16:39
-
-
Save iwiwi/5640749 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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