Module: Jamf::Client::JamfBinary::ClassMethods

Defined in:
lib/jamf/client/jamf_binary.rb

Overview

class Methods

Instance Method Summary collapse

Instance Method Details

#build_jamf_command(command, args) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jamf/client/jamf_binary.rb', line 101

def build_jamf_command(command, args)
  case args
  when nil
    "#{JAMF_BINARY} #{command}"
  when String
    "#{JAMF_BINARY} #{command} #{args}"
  when Array
    ([JAMF_BINARY.to_s, command] + args).join(' ')
  else
    raise Jamf::InvalidDataError, 'args must be a String or Array of Strings'
  end # case
end

#execute_jamf(cmd, verbose) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/jamf/client/jamf_binary.rb', line 115

def execute_jamf(cmd, verbose)
  puts "Running: #{cmd}" if verbose
  output = ''
  IO.popen("#{cmd} 2>&1") do |proc|
    loop do
      line = proc.gets
      break unless line

      output << line
      puts line if verbose
    end
  end
  output.force_encoding('UTF-8')
  output
end

#run_jamf(command, args = nil, verbose = false) ⇒ String

Note:

Most jamf commands require superuser/root privileges.

Run an arbitrary jamf binary command.

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

Examples:

These two are equivalent:

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

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

Parameters:

  • command (String, Symbol)

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

  • args (String, Array) (defaults to: nil)

    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

  • verbose (Boolean) (defaults to: false)

    Should the stdout & stderr of the jamf binary be sent to the current stdout in realtime, as well as returned as a string?

Returns:

  • (String)

    the stdout & stderr of the jamf binary.

Raises:



89
90
91
92
93
94
95
96
97
98
# File 'lib/jamf/client/jamf_binary.rb', line 89

def run_jamf(command, args = nil, verbose = false)
  raise Jamf::UnmanagedError, 'The jamf binary is not installed on this computer.' unless installed?
  unless ROOTLESS_JAMF_COMMANDS.include?(command.to_sym) || JSS.superuser?
    raise Jamf::UnsupportedError, 'You must have root privileges to run that jamf binary command'
  end

  cmd = build_jamf_command command, args
  cmd += " #{JAMF_VERBOSE_OPT}" if verbose && !cmd.include?(JAMF_VERBOSE_OPT)
  execute_jamf cmd, verbose
end