使用输出数字取消挂起的打印机作业

时间:2016-01-05 17:47:48

标签: ruby printing ssh output

我有一个脚本会检查我们的服务器是否有使用net/ssh挂起的打印作业。

现在我要做的是使用脚本输出的job ID(包含所有数字的第3列)并取消作业(我知道我将如何取消)在Ruby中,有没有办法使用输出数字作为输入?

来源:

#!/usr/local/bin/ruby

require 'rubygems'
require 'net/ssh'
require 'etc'
require 'adfitech/mail'

class PrintJobs

  HOST = ARGV[0]
  USERNAME = Etc.getlogin
  PASSWORD = nil

  def scan_for_jobs
    check_jobs = Net::SSH.start(HOST, USERNAME, :password => PASSWORD) do |ssh|
    cmd = "prt_jobs"
    info = ssh.exec!(cmd)
    res = info.split("\n").reject {|line| line.match(/\s+2016\s+/)}.join("\n")
     puts res
     print "Kill jobs: "
     input = STDIN.gets.chomp.upcase
     if input == "YES"
       kill_jobs(check_jobs, res)
     else
       exit 1
     end
   end
 end

 def kill_jobs(check_jobs, res)
   puts "Loading jobs in kill que.."
   # <= Here output digits  
  end
end

 test = PrintJobs.new
 test.scan_for_jobs

输出:

#3rd column with the digits is the job ID
laser26-828837          kaj            1042432   Fri 21 Aug 2015 03:59:35 PM CDT
laser26-828982          leb             446464   Fri 21 Aug 2015 04:52:20 PM CDT
laser26-828983          leb            1042432   Fri 21 Aug 2015 04:52:20 PM CDT
laser26-828986          leb             446464   Fri 21 Aug 2015 05:04:39 PM CDT
laser26-828987          leb            1042432   Fri 21 Aug 2015 05:04:39 PM CDT
laser26-828991          leb             446464   Fri 21 Aug 2015 05:15:08 PM CDT
laser26-828992          leb            1042432   Fri 21 Aug 2015 05:15:08 PM CDT
laser26-898419          kaj             430080   Wed 14 Oct 2015 02:01:34 PM CDT
laser26-898420          kaj            1042432   Wed 14 Oct 2015 02:01:34 PM CDT
laser26-898444          kaj             430080   Wed 14 Oct 2015 02:09:08 PM CDT
laser26-898445          kaj            1042432   Wed 14 Oct 2015 02:09:08 PM CDT
laser26-898526          kaj             446464   Wed 14 Oct 2015 02:50:45 PM CDT
laser26-898527          kaj            1042432   Wed 14 Oct 2015 02:50:45 PM CDT
laser26-898577          kaj             446464   Wed 14 Oct 2015 03:09:03 PM CDT
laser26-898578          kaj            1042432   Wed 14 Oct 2015 03:09:04 PM CDT
laser26-898583          kaj             430080   Wed 14 Oct 2015 03:13:27 PM CDT
laser26-898584          kaj            1042432   Wed 14 Oct 2015 03:13:27 PM CDT
laser26-898587          kaj             446464   Wed 14 Oct 2015 03:17:17 PM CDT
laser26-898588          kaj            1042432   Wed 14 Oct 2015 03:17:17 PM CDT
laser26-898596          kaj             446464   Wed 14 Oct 2015 03:25:40 PM CDT
laser26-898597          kaj            1042432   Wed 14 Oct 2015 03:25:40 PM CDT
laser26-898602          kaj             446464   Wed 14 Oct 2015 03:26:13 PM CDT
laser26-898603          kaj            1042432   Wed 14 Oct 2015 03:26:13 PM CDT
laser26-898617          kaj             430080   Wed 14 Oct 2015 03:31:27 PM CDT
laser26-898618          kaj            1042432   Wed 14 Oct 2015 03:31:27 PM CDT
laser26-903874          bja             446464   Tue 20 Oct 2015 09:18:34 AM CDT
laser26-903875          bja            1042432   Tue 20 Oct 2015 09:18:34 AM CDT
laser26-904044          kaj             430080   Tue 20 Oct 2015 10:11:32 AM CDT
laser26-904045          kaj            1042432   Tue 20 Oct 2015 10:11:32 AM CDT
laser26-904171          kaj             446464   Tue 20 Oct 2015 10:34:45 AM CDT
laser26-904172          kaj            1042432   Tue 20 Oct 2015 10:34:45 AM CDT
laser26-904368          bja             430080   Tue 20 Oct 2015 11:33:48 AM CDT
laser26-904369          bja            1042432   Tue 20 Oct 2015 11:33:48 AM CDT
laser26-904479          bja             446464   Tue 20 Oct 2015 12:06:10 PM CDT
laser26-904480          bja            1042432   Tue 20 Oct 2015 12:06:10 PM CDT
laser26-904495          bja             430080   Tue 20 Oct 2015 12:10:26 PM CDT
laser26-904496          bja            1042432   Tue 20 Oct 2015 12:10:26 PM CDT
laser26-966772          ksn             430080   Thu 03 Dec 2015 03:35:28 PM CST
laser26-966773          ksn            1042432   Thu 03 Dec 2015 03:35:28 PM CST
laser26-966861          ksn             446464   Thu 03 Dec 2015 03:50:00 PM CST
laser26-966862          ksn            1042432   Thu 03 Dec 2015 03:50:00 PM CST
laser26-966979          ksn             446464   Thu 03 Dec 2015 04:18:25 PM CST
laser26-966980          ksn            1042432   Thu 03 Dec 2015 04:18:25 PM CST
laser26-966989          ksn             430080   Thu 03 Dec 2015 04:23:05 PM CST
laser26-966990          ksn            1042432   Thu 03 Dec 2015 04:23:05 PM CST
laser32-829104          glt             187392   Sun 23 Aug 2015 07:22:16 PM CDT
laser35-797457          jss             572416   Wed 29 Jul 2015 02:59:20 PM CDT
laser35-806865          kjw             982016   Wed 05 Aug 2015 02:15:55 PM CDT
laser40-898540          rrw             715776   Wed 14 Oct 2015 02:51:30 PM CDT
laser40-898547          rrw             715776   Wed 14 Oct 2015 02:53:05 PM CDT
laser40-904373          rrw            2565120   Tue 20 Oct 2015 11:37:39 AM CDT
mailrm2-829117          lmb             356352   Mon 24 Aug 2015 06:20:59 AM CDT
pref_jet-797185         djl             871424   Wed 29 Jul 2015 01:46:38 PM CDT
pref_jet-797187         aqs            1104896   Wed 29 Jul 2015 01:46:57 PM CDT
pref_jet-829111         djl             117760   Mon 24 Aug 2015 05:38:54 AM CDT
pref_jet-829112         djl             117760   Mon 24 Aug 2015 05:41:05 AM CDT
team_d-797167           kld             144384   Wed 29 Jul 2015 01:39:29 PM CDT
team_d-797168           kld             145408   Wed 29 Jul 2015 01:39:39 PM CDT
team_d-797169           kld             144384   Wed 29 Jul 2015 01:39:48 PM CDT
team_d-797170           kld             145408   Wed 29 Jul 2015 01:39:59 PM CDT
team_d-797171           kld             144384   Wed 29 Jul 2015 01:40:10 PM CDT
team_d-797172           kld             144384   Wed 29 Jul 2015 01:40:23 PM CDT
team_d-797173           kld             144384   Wed 29 Jul 2015 01:40:31 PM CDT
team_d-797174           kld             144384   Wed 29 Jul 2015 01:40:41 PM CDT
team_d-797175           kld             144384   Wed 29 Jul 2015 01:40:50 PM CDT
team_d-797176           kld             144384   Wed 29 Jul 2015 01:41:00 PM CDT
team_d-797177           kld             144384   Wed 29 Jul 2015 01:41:08 PM CDT
team_d-797178           kld             144384   Wed 29 Jul 2015 01:41:16 PM CDT
team_d-797179           kld             144384   Wed 29 Jul 2015 01:41:25 PM CDT
team_d-797180           kld             145408   Wed 29 Jul 2015 01:41:37 PM CDT
team_d-797181           kld             145408   Wed 29 Jul 2015 01:41:45 PM CDT
team_d-797186           kld             144384   Wed 29 Jul 2015 01:46:40 PM CDT
team_d-829108           rdm             429056   Mon 24 Aug 2015 04:55:43 AM CDT
team_d-850252           kld             145408   Wed 09 Sep 2015 12:34:18 PM CDT
team_d-869247           kld              77824   Wed 23 Sep 2015 07:57:35 AM CDT
team_d-869248           kld              81920   Wed 23 Sep 2015 07:57:43 AM CDT
team_d-898621           kld              39936   Wed 14 Oct 2015 03:32:01 PM CDT
team_d-898622           kld              39936   Wed 14 Oct 2015 03:32:12 PM CDT
team_d-898633           kld              36864   Wed 14 Oct 2015 03:33:59 PM CDT
team_d-904046           kmt           16634880   Tue 20 Oct 2015 10:12:04 AM CDT
team_d-904497           kmt            5965824   Tue 20 Oct 2015 12:11:56 PM CDT
team_f-898550           rlr            1070080   Wed 14 Oct 2015 02:55:30 PM CDT
team_t-795252           tud             891904   Tue 28 Jul 2015 01:40:06 PM CDT
team_t-795255           tud             970752   Tue 28 Jul 2015 01:40:41 PM CDT
team_t-795256           tud            1033216   Tue 28 Jul 2015 01:40:55 PM CDT
team_t-795263           dlc             228352   Tue 28 Jul 2015 01:46:07 PM CDT
team_t-795264           alm            1280000   Tue 28 Jul 2015 01:46:30 PM CDT
team_t-797184           slp             154624   Wed 29 Jul 2015 01:46:25 PM CDT
team_t-797188           slp             155648   Wed 29 Jul 2015 01:47:20 PM CDT
team_t-797456           alm             244736   Wed 29 Jul 2015 02:59:17 PM CDT
team_t-828984           slp              16384   Fri 21 Aug 2015 04:53:01 PM CDT
team_t-828985           slp              16384   Fri 21 Aug 2015 04:56:14 PM CDT
team_t-828993           slp             192512   Fri 21 Aug 2015 05:16:37 PM CDT
team_t-829082           dlc             243712   Sun 23 Aug 2015 07:44:47 AM CDT
team_t-829103           glt             187392   Sun 23 Aug 2015 07:20:19 PM CDT
team_t-829114           ddh              17408   Mon 24 Aug 2015 06:03:36 AM CDT
team_t-829115           ddh              16384   Mon 24 Aug 2015 06:04:16 AM CDT
team_t-898551           alh             219136   Wed 14 Oct 2015 02:56:02 PM CDT
team_t-898552           alh             238592   Wed 14 Oct 2015 02:56:21 PM CDT
team_t-898619           ljr             141312   Wed 14 Oct 2015 03:31:28 PM CDT
team_t-898717           ljr             144384   Wed 14 Oct 2015 03:54:16 PM CDT
team_t-898826           dlc             320512   Thu 15 Oct 2015 06:39:42 AM CDT
team_t-898834           alm             367616   Thu 15 Oct 2015 07:05:41 AM CDT
team_t-966858           ljr            1857536   Thu 03 Dec 2015 03:48:23 PM CST
team_t-966863           ljr             555008   Thu 03 Dec 2015 03:50:10 PM CST
thermal12-980170        clw               1024   Fri 11 Dec 2015 02:32:39 PM CST
thermal13-851959        wdp               1024   Thu 10 Sep 2015 11:35:08 AM CDT
thermal13-898422        wdp               1024   Wed 14 Oct 2015 02:01:37 PM CDT
thermal13-967002        wdp               1024   Thu 03 Dec 2015 04:24:59 PM CST
thermal13-967011        wdp               1024   Thu 03 Dec 2015 04:25:49 PM CST
thermal20-870628        czp               1024   Wed 23 Sep 2015 02:08:13 PM CDT
Kill jobs: yes
Loading jobs in kill que..
[tep@coltrane ruby]$ ruby prt_jobs_check davey
closing_2-205137        snb             231424   Wed 29 Jul 2015 01:40:00 PM CDT
closing_2-205138        snb             232448   Wed 29 Jul 2015 01:40:33 PM CDT
closing_2-205139        jiw             242688   Wed 29 Jul 2015 01:40:48 PM CDT
closing_2-205140        jiw             228352   Wed 29 Jul 2015 01:41:06 PM CDT
closing_2-205164        jiw             222208   Wed 29 Jul 2015 02:38:37 PM CDT
laser20-205141          sdj             814080   Wed 29 Jul 2015 01:45:28 PM CDT
laser20-205142          sdj             649216   Wed 29 Jul 2015 01:45:44 PM CDT
laser20-205143          sdj             649216   Wed 29 Jul 2015 01:47:05 PM CDT
laser27-205163          acm             684032   Wed 29 Jul 2015 02:38:20 PM CDT
laser27-209301          acm              14336   Fri 21 Aug 2015 07:43:38 AM CDT
laser27-220263          acm             265216   Thu 15 Oct 2015 06:54:16 AM CDT
laser27-220264          acm              16384   Thu 15 Oct 2015 06:54:17 AM CDT
laser31-220266          jal               1024   Thu 15 Oct 2015 06:56:57 AM CDT
laser32-220265          tep              39936   Thu 15 Oct 2015 06:54:54 AM CDT
npl_3-220245            jrr             176128   Wed 14 Oct 2015 03:32:03 PM CDT
ship_setup2-205177      slf            1193984   Wed 29 Jul 2015 03:02:27 PM CDT
ship_setup2-205178      slf            1571840   Wed 29 Jul 2015 03:02:47 PM CDT
ship_setup2-205180      slf            1571840   Wed 29 Jul 2015 03:03:34 PM CDT
team_d-209530           akn            1393664   Sun 23 Aug 2015 07:09:12 AM CDT
team_d-209533           akn              50176   Sun 23 Aug 2015 12:47:52 PM CDT
team_d-209534           akn              50176   Sun 23 Aug 2015 01:05:16 PM CDT
team_d-209535           akn              50176   Sun 23 Aug 2015 01:07:02 PM CDT
team_f-206217           jlh            1327104   Wed 05 Aug 2015 01:07:12 PM CDT
verif_jet-220261        twk            1369088   Thu 15 Oct 2015 06:51:34 AM CDT
verif_jet-220262        twk            1363968   Thu 15 Oct 2015 06:52:09 AM CDT
verif_jet-220267        twk            1372160   Thu 15 Oct 2015 07:01:45 AM CDT
verif_jet-220268        twk            1374208   Thu 15 Oct 2015 07:02:00 AM CDT
verif_jet-221015        sum            1258496   Tue 20 Oct 2015 11:44:50 AM CDT
verif_jet-221016        sum            1198080   Tue 20 Oct 2015 11:45:32 AM CDT
verif_jet-221017        sum            1227776   Tue 20 Oct 2015 11:46:34 AM CDT
verif_jet-221018        sum            1190912   Tue 20 Oct 2015 11:47:51 AM CDT
verif_jet-221019        sum            1179648   Tue 20 Oct 2015 11:48:08 AM CDT
verif_jet-221030        tad            1681408   Tue 20 Oct 2015 12:11:39 PM CDT
verif_jet-221031        tad             903168   Tue 20 Oct 2015 12:11:44 PM CDT
verif_jet-221032        tad             872448   Tue 20 Oct 2015 12:11:50 PM CDT
verif_jet-221033        tad            1716224   Tue 20 Oct 2015 12:12:56 PM CDT
Kill jobs: yes
Loading jobs in kill que..

1 个答案:

答案 0 :(得分:1)

你的问题很模糊,但我想我明白了,所以我会捅它。我想你想解析那个输出并得到工作ID,对吧?我假设工作ID在该输出的第3列中,让我们摘录一下:

output = <<-HEREDOC
laser26-828837          kaj            1042432   Fri 21 Aug 2015 03:59:35 PM CDT
laser26-828982          leb             446464   Fri 21 Aug 2015 04:52:20 PM CDT
laser26-828983          leb            1042432   Fri 21 Aug 2015 04:52:20 PM CDT
HEREDOC

要从该字符串中获取所有Job IDS,我们可以映射每一行,将这些行按空格分开并获得第3项:

column = 2 # 3rd column
job_ids = output.lines.map { |line| line.split(/\s+/)[2] }
# => ["1042432", "446464", "1042432"]

希望有所帮助。