Created
January 19, 2012 06:14
-
-
Save ryan5500/1638307 to your computer and use it in GitHub Desktop.
use -ww option getting pid from ps with pty lib (because pty's terminal output width is narrow)
This file contains 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 | |
# conding: utf-8 | |
# | |
# this script shows pid which contains 'some_process' | |
# | |
require 'pty' | |
require 'expect' | |
cmd = 'ssh username@somehost' | |
PTY.spawn(cmd) do |reader, writer| | |
reader.expect(/Password:/) | |
writer.puts('password') | |
reader.expect(/\]%/) | |
writer.puts('ps auxww') | |
ps_lines = reader.expect(/\]%/).first.split("\r\n") | |
ps_lines = ps_lines[2..ps_lines.size-2] | |
#puts ps_lines.inspect | |
val = ps_lines.select {|l| l =~ /some_process/ } | |
puts val.first.split(' ')[1] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment