Showing posts with label os x. Show all posts
Showing posts with label os x. Show all posts

Wednesday, April 8, 2009

Use MAC Address to Look Up Computer In Open Directory

Occassionally you know a Mac computer is in your Open Directory, but don't know what it is named in there. If you know the primary MAC address, you can look it up.

After you open up a trusty Terminal, you can make a list of all of the Computer Names and their Mac addresses by doing this (on one line):

dscl /LDAPv3/192.168.100.100 -readall /Computers RecordName macAddress > out.txt

Then, open up out.txt in a text editor, and do a search for your MAC address, or, from the command line, do this:

grep -A 1 "00:1b:63:36:95:35" out.txt

The result will look like:

dsAttrTypeNative:macAddress: 00:1b:63:36:95:35
RecordName:
ComputerNameHere

If you have access to a computer over ssh or ARD, you can get the MAC address with this command:

ifconfig en0 | grep eth

'en0' is the first interface, which is almost always ethernet, and should be the value stored in the directory. Using 'en1' would give you the second interface's MAC address; it is almost always the Airport.

Thursday, March 5, 2009

Python Subprocess Wrapper for MySQL

Compiling MySQL support for Python under OS X Leopard is a pain, and, so far as I can see, there is no handy way to do it with a redistributable universal binary.

So, I decided to call MySQL from within Python using a subprocess. This is the code that I came up with, which should work fine with a default installation of MAMP. It likely has more overhead than using MySQLdb, and would likely choke if you got a really large result-set back. However, I think it will serve my purposes admirably.

#!/usr/bin/env python

from subprocess import Popen, PIPE

# Set the command you need to connect to your database
# WARNING: password will be visible in a process listing!
mysql_cmd_line = "/Applications/MAMP/Library/bin/mysql -u root -p"
mysql_password = "root"

def RunSqlCommand(sql_statement, database=None):
    
    """Pass in the SQL statement that you would like executed.
    Optionally, specify a database to operate on.  Returns the result."""
    
    command_list = mysql_cmd_line.split()
    if database:
        command_list.append(database)
        
    # Run mysql in a subprocess
    process = Popen(command_list, stdin=PIPE, stdout=PIPE,
                    stderr=PIPE, close_fds=True)
    
    # pass it our commands, and get the results
    (stdout, stderr) = process.communicate( mysql_password )

    return stdout

def test():
    """Performs a simple test connection."""

    print "This specified MySQL server has the following databases:"
    print
    print RunSqlCommand("SHOW DATABASES;")

    print RunSqlCommand("SELECT * FROM USER_PRIVILEGES LIMIT 2;", "information_schema")

if __name__ == "__main__":
    test()

The output of that command, against a pristine MAMP installation, is:

This specified MySQL server has the following databases:

Database
information_schema
mysql
test

GRANTEE TABLE_CATALOG PRIVILEGE_TYPE IS_GRANTABLE
'root'@'localhost' NULL SELECT YES
'root'@'localhost' NULL INSERT YES