module Jamf::Client::JamfBinary::ClassMethods

class Methods

Public Instance Methods

build_jamf_command(command, args) click to toggle source
    # File lib/jamf/client/jamf_binary.rb
101 def build_jamf_command(command, args)
102   case args
103   when nil
104     "#{JAMF_BINARY} #{command}"
105   when String
106     "#{JAMF_BINARY} #{command} #{args}"
107   when Array
108     ([JAMF_BINARY.to_s, command] + args).join(' ')
109   else
110     raise Jamf::InvalidDataError, 'args must be a String or Array of Strings'
111   end # case
112 end
execute_jamf(cmd, verbose) click to toggle source
    # File lib/jamf/client/jamf_binary.rb
115 def execute_jamf(cmd, verbose)
116   puts "Running: #{cmd}" if verbose
117   output = ''
118   IO.popen("#{cmd} 2>&1") do |proc|
119     loop do
120       line = proc.gets
121       break unless line
122 
123       output << line
124       puts line if verbose
125     end
126   end
127   output.force_encoding('UTF-8')
128   output
129 end
run_jamf(command, args = nil, verbose = false) click to toggle source

Run an arbitrary jamf binary command.

@note Most jamf commands require superuser/root privileges.

@param command the jamf binary command to run

The command is the single jamf command that comes after the/usr/bin/jamf.

@param args the arguments passed to the jamf command.

This is to be passed to Kernel.` (backtick), after being combined with the
jamf binary and the jamf command

@param verbose Should the stdout & stderr of the jamf binary be sent to

the current stdout in realtime, as well as returned as a string?

@return [String] the stdout & stderr of the jamf binary.

@example

These two are equivalent:

  Jamf::Client.run_jamf "recon", "-assetTag 12345 -department 'IT Support'"

  Jamf::Client.run_jamf :recon, ['-assetTag', '12345', '-department', 'IT Support'"]

The details of the Process::Status for the jamf binary process can be captured from $CHILD_STATUS immediately after calling. (See Process::Status)

   # File lib/jamf/client/jamf_binary.rb
89 def run_jamf(command, args = nil, verbose = false)
90   raise Jamf::UnmanagedError, 'The jamf binary is not installed on this computer.' unless installed?
91   unless ROOTLESS_JAMF_COMMANDS.include?(command.to_sym) || JSS.superuser?
92     raise Jamf::UnsupportedError, 'You must have root privileges to run that jamf binary command'
93   end
94 
95   cmd = build_jamf_command command, args
96   cmd += " #{JAMF_VERBOSE_OPT}" if verbose && !cmd.include?(JAMF_VERBOSE_OPT)
97   execute_jamf cmd, verbose
98 end