Thursday 11 July 2013

Work around to fix the wifi issue on OS X Mavericks (DP versions)


Since Apple released the developer preview of the next OS X system Mavericks I cannot wait to try it on my old Mac Book Pro (2010 version). Although lots of people complaining that they constantly keep dropping wifi connection I had no issues at all until I upgraded the system to the DP 3 - wifi connection keeps dropping on my Mac.

There isn't a solution to fix the wifi issue after searching google for the solutions and I definitely don't want to rollback to Mountain Lion because the new memory compression feature works great on my machine with 8G memory. Usually I don't switch off the Mac but only put it to sleep mode and over time the 8G memory will be 100% used plus over 2.5G swap space used. This never happend again after it upgraded to Mavericks.

After the WIFI dropped I have to reconnect to the wifi point so I can access the network. As there isn't a proper solution to fix the issue and reconnect the wifi network will work the trick, why not automate it?

The easiest way to reconnect the wifi is run the command 

ipconfig set en1 DHCP

We can use the cron job to run this command automatically every minute, however, we only want to run it after the connection dropped. So we need some intelligent to do that


# replace 10.0.0.1 to your router ip address
count=`ping -c 2 10.0.0.1 | grep icmp | grep timeout | wc -l`; if [ $count != 0 ]
then
    ipconfig set en1 DHCP
 # it tells you when the network was reset
    osascript -e 'tell app "Finder" to display dialog "Reset Wifi Connection From cron Job."'
fi

You can either put the code into one script and configure the cron to execute it every minute or just compile the code to one single line and past it on the cron job. I chose the second option:


# temperary fix the wifi dropping issue on OSX Mavericks
# replace 10.0.0.1 to your router ip address
* * * * * count=`ping -c 2 10.0.0.1 | grep icmp | grep timeout | wc -l`; if [ $count != 0 ]; then ipconfig set en1 DHCP;osascript -e 'tell app "Finder" to display dialog "Reset Wifi Connection From cron Job."' ; fi

the osascript -e 'tell app "Finder" to display dialog "Reset Wifi Connection From cron Job."' will notify you the network was reset with a popup message on the Finder window so you will remember to delete the job from cron once that's fixed by Apple.

Thursday 23 May 2013

A Simpler PHP LDAP API

LDAP service and ActiveDirectory service are very commonly used services to hold employee information in large companies and organisations. I was a software engineer worked on a large oilfield service company which implemented the one of the best LDAP service to host over 80K employees records. This is one of the best LDAP services I ever seen.

As you can imaging I have to use the LDAP service to query employee records and authenticate user logins on different projects. Most of the time LAMP is my first choice of the development environment so I have to keep using the PHP ldap API (ldap_connect, ldap_bind etc) to do all sorts of LDAP queries and user authentication.

After several times of keep writing the same code again and again I came up with this simpler PHP LDAP API that you can complete ldap actions in 10 lines of code:

<?php
$portal_username='yufeiliu';
$portal_password='password';
$ldap_user = new LDAPUser();
$ldap_user->setLDAPHost("server", 389);
$ldap_user->setBaseDN('DC=domain,DC=company,DC=com');
$ldap_user->connectLDAPServer();
$ldap_user->setServiceAccount('CN=admin,ou=service,ou=accounts,dc=cs,dc=company,dc=com', 'password');
if($ldap_user->authorizeUser($portal_username, $portal_password)){
  echo "YES\n";
}else{
  echo "NO\n";
}
$ldap_user->disconnectLDAPServer();

?>

This API is not designed commonly to be used at anywhere so you may need to make some changes on the code so you can use it on your projects.

The php code can be downloaded from gist here