2016-01-05 81 views
0

我正在製作一個將通過打印機服務器運行的程序,並取消掛起的作業。截至目前它輸出的一切,我想要做的是排除root從prt_jobs中排除根目錄

輸出:

credjet-898837   cdd   5312512 Wed 14 Oct 2015 03:42:32 PM CDT 
credjet-898839   cdd   1998848 Wed 14 Oct 2015 03:45:32 PM CDT 
credjet-940485   cdd   1206272 Mon 04 Jan 2016 01:10:30 PM CST 
credjet-940499   cdd    342016 Mon 04 Jan 2016 01:21:42 PM CST 
credjet-940505   cdd    342016 Mon 04 Jan 2016 01:29:26 PM CST 
credjet-940509   cdd    342016 Mon 04 Jan 2016 01:38:24 PM CST 
credjet-940514   cdd    342016 Mon 04 Jan 2016 02:00:02 PM CST 
credjet-940515   cdd   2387968 Mon 04 Jan 2016 02:00:17 PM CST 
credjet-940525   cdd   2387968 Mon 04 Jan 2016 02:10:46 PM CST 
credjet-940526   cdd   2387968 Mon 04 Jan 2016 02:11:01 PM CST 
credjet-940528   cdd   2387968 Mon 04 Jan 2016 02:12:44 PM CST 
credjet-940602   cdd   2382848 Mon 04 Jan 2016 02:26:09 PM CST 
devljet-931153   siv   1798144 Fri 18 Dec 2015 02:38:30 PM CST 
devljet-931157   siv   3278848 Fri 18 Dec 2015 02:47:18 PM CST 
devljet-931158   siv   1538048 Fri 18 Dec 2015 02:47:18 PM CST 
laser11-917719   root    78848 Wed 18 Nov 2015 09:56:47 PM CST 
laser11-918257   root    78848 Thu 19 Nov 2015 09:45:23 PM CST 
laser11-918262   root    79872 Thu 19 Nov 2015 09:49:30 PM CST 
laser11-918263   root    78848 Thu 19 Nov 2015 09:53:45 PM CST 

預期輸出:

credjet-898837   cdd   5312512 Wed 14 Oct 2015 03:42:32 PM CDT 
credjet-898839   cdd   1998848 Wed 14 Oct 2015 03:45:32 PM CDT 
credjet-940485   cdd   1206272 Mon 04 Jan 2016 01:10:30 PM CST 
credjet-940499   cdd    342016 Mon 04 Jan 2016 01:21:42 PM CST 
credjet-940505   cdd    342016 Mon 04 Jan 2016 01:29:26 PM CST 
credjet-940509   cdd    342016 Mon 04 Jan 2016 01:38:24 PM CST 
credjet-940514   cdd    342016 Mon 04 Jan 2016 02:00:02 PM CST 
credjet-940515   cdd   2387968 Mon 04 Jan 2016 02:00:17 PM CST 
credjet-940525   cdd   2387968 Mon 04 Jan 2016 02:10:46 PM CST 
credjet-940526   cdd   2387968 Mon 04 Jan 2016 02:11:01 PM CST 
credjet-940528   cdd   2387968 Mon 04 Jan 2016 02:12:44 PM CST 
credjet-940602   cdd   2382848 Mon 04 Jan 2016 02:26:09 PM CST 
devljet-931153   siv   1798144 Fri 18 Dec 2015 02:38:30 PM CST 
devljet-931157   siv   3278848 Fri 18 Dec 2015 02:47:18 PM CST 
devljet-931158   siv   1538048 Fri 18 Dec 2015 02:47:18 PM CST 
#<= No more root 

有沒有一種方法,我可以輸出相同的信息,但排除根作業?


來源:

#!/local/usr/bin/ruby 

require 'rubygems' 
require 'net/ssh' 
require 'etc'   

class PrintJobs 

    HOST = '<server here>' #<= Left blank for security 
    USERNAME = Etc.getlogin 
    PASSWORD = nil 

    def scan_for_jobs 
    check_jobs = Net::SSH.start(HOST, USERNAME, :password => PASSWORD) do |ssh| 
     cmd = "prt_jobs" 
     res = ssh.exec!(cmd) 
     puts res 
    end 
    end 
end 

test = PrintJobs.new 
test.scan_for_jobs 
+0

如果答案對您有幫助,請接受。 –

+0

@WandMaker沒有先生。 – Bam

回答

2

您可以通過下面的代碼擺脫多餘線條:

res = ssh.exec!(cmd) 
    res = res.split("\n").reject {|line| line.match(/\s+root\s+/)}.join("\n") 
    puts res 

exec!結果發出了SSH一個包含命令的輸出字符串。因此,我們需要通過換行來拆分它,並遍歷數組以拒絕包含root的行,並使用新行重新加入數組。