Linux: Fixing an unreliable network connection with ASUS P8Z 68-V onboard LAN

Recently I got a new ASUS P8Z 68-V motherboard and CPU, and had been having some strange network issues with it when running Gentoo Linux. The problems included connection failures after random periods of time and generally slow download speeds. The only way to get the connection running again after it failed (which was every few minutes at times) was to run:

ifconfig eth0 down
ifconfig eth0 up

On top of that, running ifconfig (with no args) was reporting 100% RX packets dropped for the interface.

This is the info on the network adapter:

$ lspci
...
07:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express
Gigabit Ethernet controller (rev 06)

At first I suspected there may have been something wrong with my (relatively new) Gentoo setup, so I did some testing running off an Ubuntu 11.10 live CD.  Interestingly this gave the same unreliable behaviour that wasn’t present when running Windows 7.

After further digging it turned out the problem was that the Linux kernel was loading the wrong module for the network adapter. This was confirmed by the presence of “r8169” in the output of lsmod.

The solution was to remove and blacklist the r8169 module and install Realtek’s official r8168 Linux kernel module from their website. On Gentoo I had compiled the kernel with the r8169 module built-in, so this meant first deselecting it and recompiling the kernel. After that, all that was left was to extract Realtek’s driver package and run ./autorun.sh as root.

Solution source: http://askubuntu.com/questions/46942/how-do-i-stop-my-ethernet-network-connection-from-dropping

Update:
In Linux Mint 12, you first need to run sudo apt-get install build-essential linux-headers-3.0.0-12-generic before running autorun.sh.

Nosetests: Capturing log messages written to stderr

This is a tip for using the Python logging module in conjunction with unit-tests.
When using the root logger to write debug messages, e.g.

 import logging
..
logging.debug('x = %s' % x)

to capture the messages and write them to the console when running nosetests, pass ‘root’ to the –log-debug nosetests option. E.g.

nosetests test_module1.py -s --log-debug=root

Ubuntu/Debian: see files installed by package

Checking the files installed by a package is easy enough with the Synaptic package manager (Right-click package > properties > installed files tab).

Here’s how to get that information without leaving the comfort (cough) of your bash terminal:

$ dpkg-query -L package_name

For example:

$dpkg-query -L flashplugin-installer
/.
/usr
/usr/lib
/usr/lib/xulrunner
/usr/lib/xulrunner/plugins
/usr/lib/mozilla
/usr/lib/mozilla/plugins
/usr/lib/iceape
/usr/lib/iceape/plugins
...

Vim: pasting text without it cascading

If you’re like me and use highlight + middle click pasting in Linux a lot, you may have tried to paste text in a Vim window at some point… with surprising results.

For example, highlight the following text:

import re
for test_string in ['555-1212', 'ILL-EGAL']:
    if re.match(r'^\d{3}-\d{4}$', test_string):
        print test_string, 'is a valid US local phone number'
    else:
        print test_string, 'rejected'

Pasting it into a Vim terminal window when in insert mode will give you something like this:

mport re
for test_string in ['555-1212', 'ILL-EGAL']:
    if re.match(r'^\d{3}-\d{4}$', test_string):
                print test_string, 'is a valid US local phone number'
                                else:
                                                        print test_string, 'rejected'

Eh?

To make it paste as expected, first use the following command in Vim:

:set paste

Paste your text and it will come out formatted correctly. When you’re done, unset it with:

:set nopaste

(Python code example from http://wiki.python.org/moin/SimplePrograms)

MySQL + ODBC + Python

How to connect Python programs to a MySQL database using ODBC on Ubuntu 10.04 LTS (Lucid)

This guide assumes you already have a MySQL server set up somewhere

  1. Install needed packages:

    sudo apt-get install unixodbc unixodbc-dev python-dev libmyodbc

    (libmyodbc is the MySQL driver for ODBC)

  2. Get current version of pyodbc:
    If you have python-setuptools installed:

    sudo easy_install pyodbc

    Or with pip (from python-pip):

    sudo pip install pyodbc

    Or if all else fails, download the latest source archive from https://code.google.com/p/pyodbc/downloads/list
    (I used v2.1.8) extract it somewhere on disk, cd into the directory, and run

    sudo python setup.py install

  3. Add a reference to MySQL driver to ODBC config file /etc/odbcinst.ini:

    [MySQL]
    Description = ODBC for MySQL
    Driver = /usr/lib/odbc/libmyodbc.so
    FileUsage = 1

  4. Test it:

    python
    import pyodbc
    cn = pyodbc.connect('DRIVER={MySQL};SERVER=localhost;DATABASE=test;UID=root;PWD=abc;')

For more examples of pyodbc usage the official documentation is very good: https://code.google.com/p/pyodbc/wiki/GettingStarted

This was pieced together from a number of sources which I’ll credit when I find the links again…