从 prt_jobs 中排除根

Excluding root from prt_jobs

我正在制作一个程序,它将 运行 通过打印机服务器并取消挂起的作业。截至目前,它输出所有内容,而我想要做的是排除 root:

输出:

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

您可以使用以下代码删除不需要的行:

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

exec! 的结果是一个字符串,其中包含通过 ssh 发出的命令的输出。因此,我们需要用换行符拆分它并遍历数组以拒绝包含 root 和 re-join 数组的行使用换行符。