Bash scripting

This is definitely something worth looking into. For now, here's a Mac example to set a different /etc/hosts depending on your IP (good for local network/adsl workarounds).

#!/bin/bash

EN1IP=`ifconfig en1 | grep 'inet ' | cut -d' ' -f 2`
H_EN1IP=10.1.1.
W_EN1IP=192.168.0.

if echo "$EN1IP" | grep -q "$H_EN1IP"
then
    cp /etc/hosts.home /etc/hosts
    echo `date` "Location: home - $EN1IP" >> /var/log/hosts.log
else
    cp /etc/hosts.work /etc/hosts
    echo `date` "Location: work - $EN1IP" >> /var/log/hosts.log
fi

exit 0

Then you could set up a LaunchAgent in /Library/LaunchAgents

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>net.hostswapper</string>
        <key>ProgramArguments</key>
        <array>
                <string>/opt/loc</string>
        </array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>

Automatic host swapping!

Update: actually, this wouldn't work since the LaunchDaemon is called before a wireless IP is acquired. Back to the drawing board. Pinging a known IP (ie. the router) with a set interval might work better.

Posted 27 Aug 2007, tagged with bash mac

Favicons

The following will work under any modern browser (IE included):

<link rel="shortcut icon" href="/favicon.ico"
  type="image/vnd.microsoft.icon" />
<link rel="icon" href="/favicon.ico" type="image/vnd.microsoft.icon" />
  1. link must be inside head
  2. /favicon.ico is a legacy name and path that older browsers will sniff regardless of the above code
  3. type may be image/gif or image/png (but it doesn't work in IE)

Posted 16 Apr 2008, tagged with html browsers ie ie6 photoshop

Mount a network share with CIFS

For a one-off:

mount -t cifs //host_or_ip/share /mountpoint

And you'll probably need at least some of:

-o username=smbusername, password=smbpassword, iocharset=utf8,
 file_mode=0777, dir_mode=0777

If you want it automatic, add a line to /etc/fstab:

//netbiosname/share /mountpoint cifs
 username=smbusername,password=smbpassword,
 iocharset=utf8,file_mode=0777,dir_mode=0777 0 0

(you could store the user/pass in a file and point credentials at it for better security)

Now try it out with sudo mount -a.

Posted 03 Aug 2007, tagged with shell

Change MySQL password

Change a user password at console with

SET PASSWORD FOR 'user'@'%' = PASSWORD('newpass');

Which is equivalent to

UPDATE mysql.user SET Password=PASSWORD('newpass')
  WHERE User='user' AND Host='%';
FLUSH PRIVILEGES;

Posted 10 Oct 2007, tagged with mysql