Friday, September 16, 2011

LDAP search queries

Some quick examples of common LDAP search queries. See this blog for more explanation and examples (thanks for the comment)

Search for a particular user:
ldapsearch -LLLx "uid=myuser"
If your LDAP database is giant, you might want to limit that search to just the people tree:
ldapsearch -LLLx -b ou=people,dc=myorg,dc=com "uid=myuser"
Find a netgroup:
ldapsearch -LLLx -b ou=netgroup,dc=myorg,dc=com "cn=mymachine.myorg.com"
Wildcards also work:
ldapsearch -LLLx -b ou=netgroup,dc=myorg,dc=com "cn=*fred*.myorg.com"
Regular group:
ldapsearch -LLLx -b ou=group,dc=myorg,dc=com "cn=sysadmins"

Reading MCX local_computer policies on the command line

Mactech has an article on using local mcx policies. To read them:
dscl . -mcxread /Computers/local_computer
and you can also see the full contents of the local_computer record like this:
defaults read /var/db/dslocal/nodes/Default/computers/local_computer

Wednesday, September 14, 2011

Reading and modifying OS X plist files on the command line

OS X uses plist files to store configuration information - you can think of them as OS X's version of the windows registry.

To read a plist you can use:
defaults read /Library/Preferences/com.apple.CrashReporter
and similarly to write a value to a plist
sudo defaults write /Library/Preferences/com.apple.CrashReporter SomeKey -bool TRUE
sudo defaults write /Library/Preferences/com.apple.CrashReporter SomeKey -string "somevalue"
and delete
sudo defaults delete /Library/Preferences/com.apple.CrashReporter SomeKey
You can also use plutil to dump an XML version to stdout:
plutil -convert xml1 -o - filename.plist
as well as convert between binary and XML formats. Xcode also ships with a plist editor (standalone prior to XCode 4, built-in since then).

And if that wasn't enough there's also PlistBuddy, here's an example command to print a particular key 'BluetoothVersionNumber':
/usr/local/bin/PlistBuddy -c Print:BluetoothVersionNumber /Library/Preferences/com.apple.Bluetooth.plist

Managing OS X users, groups, and access control from the commandline

User info
dscl . read /Users/testing
Group info
dscl . read /Groups/admin
Create a user
sudo dscl . -create /Users/testing
Add a user to the admin group
sudo dseditgroup -o edit -a testing -t user admin
Remove a user from the admin group
sudo dseditgroup -o edit -d testing -t user admin
Delete a user
sudo dscl . -delete /Users/testing

Friday, September 9, 2011

Redirection of stdout and stderr

Some notes on redirection, this article has a good summary. I hold these patterns in my head, but had long since forgotten what the syntax actually meant. Send stdout and stderr to the same file:
./generatesomeoutput &> some_file
Send stderr to stdout:
./generatesomeoutput 2>&1
which is filedescriptor '2' (stderr) to file descriptor '1' (stdout), and the '&' indicates that '1' is a filedescriptor not a filename.

Wednesday, September 7, 2011

Mounting DMG images, the hard way

Opening a DMG in the finder is a fast way to get it mounted. The manual steps are broken down as follows:

Attach the disk image (if you omit the 'nomount' flag it will do the mounting automatically, I'm stopping that for demonstration purposes)
root# hdiutil attach -nomount testing.dmg
/dev/disk1           Apple_partition_scheme          
/dev/disk1s1         Apple_partition_map             
/dev/disk1s2         Apple_Driver_ATAPI              
/dev/disk1s3         Apple_HFS                
The disk now exists:
dhcp-172-26-92-208:images root# diskutil list
[...snip...]
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     Apple_partition_scheme                        *4.2 GB     disk1
   1:        Apple_partition_map                         30.7 KB    disk1s1
   2:         Apple_Driver_ATAPI                         2.0 KB     disk1s2
   3:                  Apple_HFS Mac OS X                4.2 GB     disk1s3
And you can mount all partitions like this:
diskutil mountDisk /dev/disk1
or just one with:
diskutil mount /dev/disk1s3
Since we didn't specify targets, they will appear under /Volumes.

HOWTO Convert an Apple disk image (DMG) to ISO

hdiutil convert /path/to/filename.dmg -format UDTO -o /path/to/savefile.iso

Sunday, September 4, 2011

HOWTO: dd a disk and watch progress

On linux, dd a disk and watch progress with this command. $! is the PID of the most recent background command:
dd if=/dev/zero of=/dev/null bs=512k&
while [ $! ]; do kill -USR1 $! && sleep 10 ; done

OS X uses a different signal:
dd if=/dev/zero of=/dev/null bs=512k&
while [ $! ]; do kill -SIGINFO $! && sleep 10 ; done

Thursday, September 1, 2011

python datetime, strptime, strftime: working with datetime strings

I frequently find myself wanting to convert between a datetime object and a string representation. Here is a quick code example of swapping between the two:

t=datetime.datetime.now()
astring = t.strftime("%Y-%m-%d %H:%M:%S")
new_t = datetime.datetime.strptime(astring, "%Y-%m-%d %H:%M:%S")