Skip to content

Instantly share code, notes, and snippets.

@rking

rking/cat-p Secret

Created September 15, 2012 03:03
Show Gist options
  • Save rking/ce052ba296e23b1e277f to your computer and use it in GitHub Desktop.
Save rking/ce052ba296e23b1e277f to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
## Swipe stdout from another program by pid (using strace -p)
# Test by running
# echo $$; while sleep .5; do echo '"\"; system("echo egads"); \""'; done
# (observe the pid that comes out at the start)
pid = ARGV[0].to_s or fail "Need a pid to trace."
cmd = ['strace', '-p', pid, '-e', 'write', '-s', '999999', :err=>[:child, :out]]
IO.popen cmd do |io|
io.each_line do |line|
line.sub /^write\(1, "(.+)", \d+\)/ do
puts eval %{"#{$1}"}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment