Got error: 2013: Lost connection to MySQL server at ‘reading initial communication packet’ / channel 2: open failed: connect failed: Connection refused

Trying to ssh tunnel to a remote service that is running on a localhost rather than a local IP is resulting in this error:

Got error: 2013: Lost connection to MySQL server at 'reading initial communication packet' / channel 2: open failed: connect failed: Connection refused

The reason this fails is because an SSH tunnel will normally try to talk to the remote host using its local IP (e.g. 192.168.0.1) instead of its localhost interface (127.0.0.1).

To resolve this, a double SSH tunnel can be setup (in this example, tunneling MySQL over port 3306 listening on 127.0.0.1 on the remote host):


PORT=`jot -r 1 2000 65000`
PORT2=`jot -r 1 2000 65000`
echo "Opening tunnel on random local port - $PORT"
ssh -f -L $PORT:192.168.2.10:22 admin@rd_fw -N
PID=$!
ssh -f -L $PORT2:127.0.0.1:3306 draman@127.0.0.1 -p $PORT -N
PID2=$!


mysql -P $PORT2 -h 127.0.0.1 -u apkscan_backup -p apkscan_website

kill $PID
kill $PID2

Comments Off on Got error: 2013: Lost connection to MySQL server at ‘reading initial communication packet’ / channel 2: open failed: connect failed: Connection refused

Unknown error 3000 when updating to iOS 7 GM

I ran into the same issue that apparently thousands of others have faced when trying to one-up iOS, and managed to fix it by ensuring that: I had iTunes 11 Beta 2 installed (log into the iOS Dev Center and get it here), had all my OSX updates installed, disconnected my external sound card and other USB devices and restarted OSX.

Comments Off on Unknown error 3000 when updating to iOS 7 GM

Use regular expressions in TextMate to match text between a prefix and a suffix

Apparently TextMate uses something called “Oniguruma” for its regular expressions syntax. An overview of the syntax can be found here: http://manual.macromates.com/en/regular_expressions.

I needed to match literals of unknown length today, more specifically the following expression:

<li><a href = "http://url.com">

where url.com could be any valid URL</span>. In Onigurama syntax, you can match all those expressions using the following regular expression:

<li><a href = ".*?">

The quantifiers and anchors will cover a broad range of regexp needs, an extract of the syntax:

4. Quantifier
 
  greedy
 
    ?       1 or 0 times
    *       0 or more times
    +       1 or more times
    {n,m}   at least n but not more than m times
    {n,}    at least n times
    {,n}    at least 0 but not more than n times ({0,n})
    {n}     n times
 
  reluctant
 
    ??      1 or 0 times
    *?      0 or more times
    +?      1 or more times
    {n,m}?  at least n but not more than m times  
    {n,}?   at least n times
    {,n}?   at least 0 but not more than n times (== {0,n}?)
 
  possessive (greedy and does not backtrack after repeated)
 
    ?+      1 or 0 times
    *+      0 or more times
    ++      1 or more times
 
    ({n,m}+, {n,}+, {n}+ are possessive op. in ONIG_SYNTAX_JAVA only)
 
    ex. /a*+/ === /(?>a*)/
 
 
5. Anchors
 
  ^       beginning of the line
  $       end of the line
  \b      word boundary
  \B      not word boundary
  \A      beginning of string
  \Z      end of string, or before newline at the end
  \z      end of string
  \G      matching start position
Comments Off on Use regular expressions in TextMate to match text between a prefix and a suffix

Unzip a list of zip files into separate directories using the command line

Today I needed to unzip a huge list of compressed files into separate folders. I needed to do this because each zip file contained the same subdirectory names, so simply doing “unzip \*.zip” did not do the job as it would overwrite the deflated files for each separate zip file.

To solve this, I wrote five lines of bash:

for file in *.zip
do
  echo "$file"
  unzip "$file" -d "$file.dir"
done

The script will simply read in all the zip files from the current directory, print out the name of the file (e.g. file_name.zip) and deflate the file to a new directory called “file_name.zip.dir“.In other words, for each zip file, a new directory will be created in which the content of the zip file is extracted.

Automatically reload Chrome when editing files on OSX

When developing web applications, we often have to switch between a text editor (TextMate in my case) and a web browser (Chrome in my case) for testing purposes. Manually saving your files, switching to the browser and reloading the page can become a pain, as we perform these actions constantly during development.

To automate this process, we can use fswatch to monitor file changes and AppleScript to reload the browser whenever such a file change has been detected. Breaking it down into easy steps, this is what you should do:

1. Download and install fswatch. fswatch is awesome and it will monitor any changes that happen to any file in a specific directory (or any of its sub-directories).

2. Create the following file (I called it reloadActiveChromeTab.applescript) that reloads the active tab of Chrome whenever it is executed as AppleScript:

1
2
3
tell application "Google Chrome"
  reload active tab of window 1
end tell

3. Assuming that the root folder of your web project is located at “/Users/you/Sites/project”, you can now automatically reload Chrome each time you save a file in that project folder by using the following command:

1
fswatch /Users/you/Sites/project "osascript reloadActiveChromeTab.applescript"

That’s it! Feel free to post a comment if you run into any problems.

Comments Off on Automatically reload Chrome when editing files on OSX

Spotify crashes on iOS 7 beta 4

If you don’t modify the volume using the physical + and – buttons on the side of your iPhone, everything seems to be working fine.

Comments Off on Spotify crashes on iOS 7 beta 4

SIM card always locked on iOS 7 beta 3

Today after installing iOS 7 beta 3, I noticed that my iPhone card was always showing “locked SIM” in the upper left corner of the screen. Restarting my phone, going to Settings > Phone > SIM, trying to call someone, … did not trigger the “enter SIM PIN code” dialog. It turned out that taking the SIM card out and putting it back in again solved the problem. After doing this, my SIM card got detected immediately! I suspect this will be solved in beta 4.

Nothing happens after reboot when upgrading to Snow Leopard

Today I was finally forced to upgrade my Lion installation to Snow Leopard. After backing up using Carbon Copy Cloner and temporarily disabling full-disk encryption (which took about 10 hours in total – an encrypted disk had caused serious issues during a previous OSX update), I was ready to install Snow Leopard from the App Store.

Disable full-disk encryption on OSX
Removing full-disk encryption on OSX Lion. Problem?

The Snow Leopard installation application restarts after a few minutes, after which I was expecting to be presented with a process dialog telling me to wait until the OS has been updated. However, my system rebooted and … nothing happened. I booted right back into my Lion desktop. What had happened?

After some troubleshooting (I never got an error message after the reboot, no error messages in the logs and my system was working as before) it turned out that a connected USB device was preventing the installer from proceeding with the install. Disconnecting all USB devices (including my optical Dell mouse and my Numark Mixtrack Pro) resolved the problem: after disconnecting all devices I launched the Snow Leopard installer again from the App Store, and this time I got presented with the Snow Leopard installation screen.

Comments Off on Nothing happens after reboot when upgrading to Snow Leopard

Sending mail through Google mail using SMTP in CodeIgniter

The email helper class in CodeIgniter makes sending e-mail from within your controllers painless, or at least that’s the promise. In the past, I have used the email helper extensively in combination with sendmail, however for a new project I was required to route e-mail through a third-party SMTP server, in this case Gmail. I used the following code:

1
2
3
4
5
6
7
8
9
10
11
12
$config = Array(		
		    'protocol' => 'smtp',
		    'smtp_host' => 'ssl://smtp.googlemail.com',
		    'smtp_port' => 465,
		    'smtp_user' => '<your username>@gmail.com',
		    'smtp_pass' => '<your password>',
		    'smtp_timeout' => '4',
		    'mailtype'  => 'text', 
		    'charset'   => 'iso-8859-1'
		);
 
		$this->load->library('email', $config);

Although the code snippet above seemingly contains all the correct SMTP settings to connect to Google’s mail server, I was presented with the following error message when testing outgoing e-mail using these settings:

An Error Was Encountered
 
220 mx.google.com ESMTP m1sm10383396eex.17 - gsmtp 
<snip>
hello: 250-mx.google.com at your service, [2001:41d0:1:7aad::1]
The following SMTP error was encountered: 451 4.4.2 Timeout - closing connection. m1sm10383396eex.17 - gsmtp Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.</snip>

The error is confusing, because I was absolutely sure that the server settings were correct. However, it turned out that CodeIgniter did not correctly handle newlines when constructing the e-mail, which apparently triggered the SMTP server to keep waiting for more data, since it did not correctly interpret the newlines embedded in the e-mail stream as an EOF character. The SMTP server was waiting for more data, and CodeIgniter was waiting for a response from the server, resulting in a deadlock and the “Timeout – closing connection” message in the error log. Manually setting the newline sequence for the email helper fixed the problem:

$this->email->set_newline("\r\n");

Combining the first code snippet and this fix, the resulting (working) code to send e-mail through Google’s SMTP server looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
$config = Array(		
		    'protocol' => 'smtp',
		    'smtp_host' => 'ssl://smtp.googlemail.com',
		    'smtp_port' => 465,
		    'smtp_user' => '<your username>@gmail.com',
		    'smtp_pass' => '<your password>',
		    'smtp_timeout' => '4',
		    'mailtype'  => 'text', 
		    'charset'   => 'iso-8859-1'
		);
 
		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");

After making the highlighted change, e-mails started arriving correctly in the recipient’s mailbox. Problem solved.