2010/6/1 Nate St.germain <n8 / adullmoment.com>:
> please bear with me on this. i'm blundering my way through learning
> ruby.
>
> i have a script intended to run on os x to restore local admin rights to
> users who've had the rights removed by another script. it's sort of a
> fallback measure. i'm using this as an opportunity to learn ruby in the
> process.
>
> the newest version of the removal script (bash) will add a plain text
> file in /Library/Receipts containing the account short names on which it
> just operated. i want the restore script to be flexible enough to either
> read the list from the file if it exists or get the users from scratch
> using the "/usr/sbin/jamf listUser" command (part of the Casper admin
> suite), which outputs xml, unfortunately. at some point, i'll probably
> switch to another method to avoid having to resort to regex matches on
> the xml tags.
>
> regardless, the issue is the getUsers() method works fine on its own but
> then give me the following output when run from the script (below):
>
> sam
> norton
> mary
> Restoring admin rights for <name>sam</name>
> Restoring admin rights for <name>norton</name>
> Restoring admin rights for <name>mary</name>
>
> so in the actual script, the names are returned once correctly, then
> again with the xml tags and tabs intact. i'm not sure what i'm missing
> here.
>
> it's probably something obvious, and i'm sure this script can be written
> more concisely. i appreciate any pointers you have. note, the actual
> admin rights granting lines are commented below for testing.
>
>
> === script ===
>
> #!/usr/bin/env ruby
> # restore rights to local users who've recently had them removed.
> # 5/25/10, nate@tsp
> # 6/1/10, updated
>
> # set system variable
> # chops out the second digit in the version number, which is the only
> differentiating factor here
> def getos()
>  ¨Âùóôåí½à¯õóò¯âéî¯ó÷ßöåò> -productVersion`.chomp.split(".").slice(1).to_i
>  ¨Âóùóôåí½½ôèåî
>  ¨Âåôõò¢ôéçåò>  ¨Âìó>  ¨Âåôõò¢ìåï>  ¨Âîä
> end
>
> # cheating by using the jamf binary.
> def getUsers()
>  ¨Âóåòìéóô½à¯õóò¯óâéî¯êáíìéóôÕóåòóà®ôïß>  ¨Âóåòó½õóåòìéóô®çòåð ¯Ü¼îáíå¯
>  ¨Âóåòó®åáãüõóåòü ðõôó õóåò®óðìéô¨¯¼Û޾ݪÛ޾ݪ¾¯©Û±
The line above produces the output of the short names - and then it
returns "users" *unmodified*.  This means that the array returned from
getUsers() still has all the XML tags in there.

You can replace that line with:

users.map { |user| user.split(/<[^>]*[^>]*>/)[1] }

Or, with an alternative approach

def get_users
  users = []
  `/usr/sbin/jamf listUsers`.scan(/<name>(.*?)</name>/) { users << $1 }
  users
end

Or even use an XML tool (e.g. REXML) to extract values of tags if the
input is valid XML.

Note: conventionally we use CamelCase only for ClassNames and not for
method_names.

> end
>
> # method to read admins from a text file from a removal run
> # this may not be necessary if included in the restoreAdmin methods
> def readAdmins()
>  ¨Âåãåéðô½Æéìå®ïðå¯Ìéâòáòù¯Òåãåéðôó¯ïòç®ãïíðáîù®òåíïöåäáäíéîó§§ò§©
>  ¨Âåôõòòåãåéðô®òåáäìéîå> end
>
> # use dseditgroup for 10.[5-6] clients
> def restoreAdmin5()
>  ¨ÂÆéìå®åøéóô¿¨§¯Ìéâòáòù¯Òåãåéðôó¯ïòç®ãïíðáîù®òåíïöåäáäíéîó§© ôèåî
>  ¨Âóåòó½òåáäÁäíéî>  ¨Âóåòó®åáãäï üõ>  ¨Âõô¢Òåóôïòéîç áäíéòéçèôó æï£ûõý>  x(/usr/sbin/dseditgroup -o edit -a {u} -t user admin)
>  ¨Âîä
>  ¨Âìó>  ¨Âóåòó½çåôÕóåò>  ¨Âóåòó®åáãäï üõ>  ¨Âõô¢Òåóôïòéîç áäíéòéçèôó æï£ûõý>  x(/usr/sbin/dseditgroup -o edit -a {u} -t user admin)
>  ¨Âîä
>  ¨Âîä
> end
>
> # use nicl for 10.4 clients
> def restoreAdmin4()
>  ¨ÂÆéìå®åøéóô¿¨§¯Ìéâòáòù¯Òåãåéðôó¯ïòç®ãïíðáîù®òåíïöåäáäíéîó§© ôèåî
>  ¨Âóåòó½òåáäÁäíéî>  ¨Âóåòó®åáãäï üõ>  ¨Âõô¢Òåóôïòéîç áäíéòéçèôó æï£ûõý>  x(nicl -raw /var/db/netinfo/local.nidb -append /groups/admin
> users #{u})
>  ¨Âîä
>  ¨Âìó>  ¨Âóåòó½çåôÕóåò>  ¨Âóåòó®åáãäï üõ>  ¨Âõô¢Òåóôïòéîç áäíéòéçèôó æï£ûõý>  x(nicl -raw /var/db/netinfo/local.nidb -append /groups/admin users
> #{u})
>  ¨Âîä
>  ¨Âîä
> end
>
> # test the os with the getos() method and proceed accordingly based on
> platform
> result = case getos()
>  ¨Âèå¢ôéçåòôèåî
>  ¨ÂåóôïòåÁäíéî´
>  ¨Âèå¢ìåïôèåî
>  ¨ÂåóôïòåÁäíéîµ
>  ¨Âìóðõôó ¢îöåòóéïóðåãéæéåä® óôïððéîç®®®¢
>  ¨Âîä

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/