# File lib/jamf/client.rb 237 def self.primary_console_user 238 `#{CONSOLE_USERS_SCUTIL_CMD}` =~ /^\s*Name : (\S+)$/ 239 user = Regexp.last_match(1) 240 user == LOGINWINDOW_USER ? nil : user 241 end
class Jamf::Client
This module contains methods for working locally on a managed Jamf
client computer, on which this code is running.
jamf client computer
Constants
- CONSOLE_USERS_SCUTIL_CMD
This command gives raw info about console users
- DOWNLOADS_FOLDER
The JAMF downloads folder
- JAMF_PLIST
The
Pathname
to the preferences plist used by the jamf binary- JAMF_SUPPORT_FOLDER
The
Pathname
to the JAMF support folder- LOGINWINDOW_USER
ignore primary console user loginwindow
- PS_USER_COMM
the ps command used to figure out who’s running Self Service
- RECEIPTS_FOLDER
The JAMF receipts folder, where package installs are tracked.
- ROOT_USER
ignore console user = root (loginwindow)
- SELF_SERVICE_EXECUTABLE_END
The end of the path to the Self Service Executable. Used to figure out who’s running Self Service.app
- SUPPORT_BIN_FOLDER
The bin folder inside the
Jamf
support folder- USER_PREFS_BYHOST_FOLDER
the path to a users byhost folder from home
Public Class Methods
alias for primary_console_user
# File lib/jamf/client.rb 244 def self.console_user 245 primary_console_user 246 end
Who’s currently got an active GUI session? - might be more than one if Fast User
Switching is in use.
@return [Array<String>] The current users with GUI sessions
# File lib/jamf/client.rb 224 def self.console_users 225 output = `#{CONSOLE_USERS_SCUTIL_CMD}` 226 userlines = output.lines.select { |l| l =~ /SessionUserNameKey\s*:/ } 227 userlines.map! { |ul| ul.split(':').last.strip } 228 userlines.reject { |un| un == ROOT_USER } 229 end
@param user[String, nil] The user to query, the current user if nil.
@return [Boolean, nil] Is ‘Do Not Disturb’ enabled for the user?
nil if unknown/not-applicable
# File lib/jamf/client.rb 263 def self.do_not_disturb?(user = nil) 264 home = user ? homedir(user) : Dir.home 265 myudid = udid 266 nc_prefs_file = Pathname.new "#{home}/#{USER_PREFS_BYHOST_FOLDER}/com.apple.notificationcenterui.#{myudid}.plist" 267 return nil unless nc_prefs_file.readable? 268 JSS.parse_plist(nc_prefs_file)['doNotDisturb'] 269 end
The parsed HardwareDataType output from system_profiler
@return [Hash] the HardwareDataType data from the system_profiler command
# File lib/jamf/client.rb 214 def self.hardware_data 215 raw = `/usr/sbin/system_profiler SPHardwareDataType -xml 2>/dev/null` 216 JSS.parse_plist(raw)[0]['_items'][0] 217 end
The home dir of the specified user, nil if no homedir in local dscl.
@param user the user whose homedir to look up
@return [Pathname, nil] The user’s homedir or nil if no such user
# File lib/jamf/client.rb 278 def self.homedir(user) 279 dir = `/usr/bin/dscl . -read /Users/#{user} NFSHomeDirectory 2>/dev/null`.chomp.split(': ').last 280 dir ? Pathname.new(dir) : nil 281 end
Is the jamf binary installed?
@return [Boolean] is the jamf binary installed?
# File lib/jamf/client.rb 103 def self.installed? 104 JAMF_BINARY.executable? 105 end
The contents of the JAMF plist
@return [Hash] the parsed contents of the JAMF_PLIST
if it exists, an empty hash if not
# File lib/jamf/client.rb 161 def self.jamf_plist 162 return {} unless JAMF_PLIST.file? 163 JSS.parse_plist JAMF_PLIST 164 end
What version of the jamf binary is installed?
@return [String,nil] the version of the jamf binary installed on this client, nil if not installed
# File lib/jamf/client.rb 111 def self.jamf_version 112 installed? ? run_jamf(:version).chomp.split('=')[1] : nil 113 end
The Jamf::Computer
object for this computer
@return [Jamf::Computer,nil] The JSS
record for this computer, nil if not in the JSS
# File lib/jamf/client.rb 188 def self.jss_record 189 Jamf::Computer.fetch udid: udid 190 rescue Jamf::NoSuchItemError 191 nil 192 end
the URL to the jss for this client
@return [String] the url to the JSS
for this client
# File lib/jamf/client.rb 119 def self.jss_url 120 @url = jamf_plist['jss_url'] 121 return nil if @url.nil? 122 @url =~ %r{(https?)://(.+):(\d+)/} 123 @protocol = Regexp.last_match(1) 124 @server = Regexp.last_match(2) 125 @port = Regexp.last_match(3) 126 @url 127 end
Get the current IP address as a String
.
This handy code doesn’t acutally make a UDP connection, it just starts to set up the connection, then uses that to get the local IP.
Lifted gratefully from coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
@return [String] the current IP address.
# File lib/jamf/client.rb 85 def self.my_ip_address 86 # turn off reverse DNS resolution temporarily 87 # @note the 'socket' library has already been required by 'rest-client' 88 orig = Socket.do_not_reverse_lookup 89 Socket.do_not_reverse_lookup = true 90 91 UDPSocket.open do |s| 92 s.connect '192.168.0.0', 1 93 s.addr.last 94 end 95 ensure 96 Socket.do_not_reverse_lookup = orig 97 end
Which console user is using the primary GUI console? Returns nil if the primary GUI console is at the login window.
@return [String,nil] The login name of the user is using the primary
GUI console, or nil if at the login window.
All the JAMF receipts on this client
@return [Array<Pathname>] an array of Pathnames for all regular files in the jamf receipts folder
# File lib/jamf/client.rb 170 def self.receipts 171 raise Jamf::NoSuchItemError, "The JAMF Receipts folder doesn't exist on this computer." unless RECEIPTS_FOLDER.exist? 172 RECEIPTS_FOLDER.children.select(&:file?) 173 end
Who’s currently running Self Service.app? - might be more than one if Fast User
Switching is in use.
@return [Array<String>] The current users running Self Service.app
# File lib/jamf/client.rb 253 def self.self_service_users 254 ss_userlines = `#{PS_USER_COMM}`.lines.select { |l| l.include? SELF_SERVICE_EXECUTABLE_END } 255 ss_userlines.map { |ssl| ssl.split(' ').first } 256 end
The serial number for this computer via system_profiler
@return [String] the serial number for this computer
# File lib/jamf/client.rb 206 def self.serial_number 207 hardware_data['serial_number'] 208 end
The UUID for this computer via system_profiler
@return [String] the UUID/UDID for this computer
# File lib/jamf/client.rb 198 def self.udid 199 hardware_data['platform_UUID'] 200 end