Archive for the 'hacks' Category

Count number of lines of code

Here’s quicky. Let’s say you want to count the number of lines of code in all JAVA files, you would simply execute:
wc -l `find -name "*.java"`

SSH Socks Tunnel

SSH can be used to tunnel a connection from one computer to another. That is when you access the internet from computer A, it will seem like you are accessing it from computer B. This is useful for bypassing the restriction on content imposed from computers in schools, coffee shops, or companies. You can also benefit from the services offered in one specific network.

From a unix environment, simply execute the following replacing username and server by the appropriate values. This will create a SOCKS proxy on port 3333 (you can of course use another port):
ssh -D 3333 -f -C -q -N username@server

Now, you just need to modify your browser preferences to use that proxy and you’re done. In Firefox, head to preferences and find the connection settings. Use manual proxy configuration, and enter in the SOCKS proxy field localhost on port 3333.

Running Windows XP on VMware Player

The main reason limiting individuals from moving permanently from Windows to Ubuntu is the lack of support for numerous software applications. Fortunately, this reality is changing because of the growing Ubuntu community contributing to the process through developing Open Source alternatives, and the expansion of the Linux market attracting the attention of proprietary application developers.

Although some applications run quite smoothly through the excellent Wine package, others cause much trouble and you just need to run Windows in order to get the job done. Thanks to virtualization and VMware Player, a completely free application, you can now run Windows XP directly within your Ubuntu system. Note that this process might take some time but is worth it (take a look below). Let’s get started!

Running Ubuntu and Windows XP on two screens

  1. Install, at once, all the prerequisite packages. Run in the shell:
    sudo apt-get install build-essential linux-headers-`uname -r` gcc-3.4 g++-3.4 wine
  2. Download VMware Player for Linux (make sure you get the .tar version) and install it as follows:
    tar xvzf VMware-player-2.0.2-59824.i386.tar.gz
    cd vmware-player-distrib
    export CC=/usr/bin/gcc-3.4
    sudo ./vmware-install.pl

    The default choice for all prompts should be ok.
  3. We will now create a file partition for Windows XP (Steps 3 to 5). To do so we’ll need to run Qemu, a process emulator, through Wine. Proceed as follows:
    Download Qemu for Windows and install it with Wine
  4. Create a directory to store all our data in:
    mkdir ~/winxp
    cd ~/winxp
  5. Now, create the file partition; you can set the amount of diskpace allocated to Windows XP by changing 10G (that’s 10 Gigabytes) to whatever you need:
    wine ~/.wine/drive_c/Program\ Files/Qemu/qemu-img.exe create -f vmdk WindowsXP.vmdk 10G Formating 'WindowsXP.vmdk', fmt=vmdk
  6. Let’s now create an ISO image of Windows XP. Insert the installation CD and execute:
    umount /media/cdrom0
    dd if=/dev/cdrom of=WindowsXP.iso
    This will create a file called WindowsXP.iso; be patient, it might take some time to complete (took me about 4 minutes).
  7. Create the VMware player configuration file:
    gedit WindowsXp.vmx
    Fill in the following content and save the file. You can set the amount of RAM to be used by changing the memsize value.
    #!/usr/bin/vmware
    config.version = "8"
    virtualHW.version = "3"
    ide0:0.present = "TRUE"
    ide0:0.filename = "WindowsXP.vmdk"
    memsize = "256"
    MemAllowAutoScaleDown = "FALSE"
    ide1:0.present = "TRUE"
    ide1:0.fileName = "WindowsXP.iso"
    ide1:0.deviceType = "cdrom-image"
    ide1:0.autodetect = "TRUE"
    floppy0.fileType = "file"
    floppy0.fileName = ""
    floppy0.startConnected = "True"
    ethernet0.present = "TRUE"
    usb.present = "TRUE"
    sound.present = "TRUE"
    sound.virtualDev = "es1371"
    displayName = "Windows XP"
    guestOS = "winxp"
    nvram = "WindowsXP.nvram"
    MemTrimRate = "-1"
    ide0:0.redo = ""
    ethernet0.addressType = "generated"
    uuid.location = "56 4d cd 3f 59 5b 61 43-fd 73 ef 46 56 4c 23 7b"
    uuid.bios = "56 4d cd 3f 59 5b 61 43-fd 73 ef 46 56 4c 23 7b"
    ethernet0.generatedAddress = "00:0c:29:4c:23:7b"
    ethernet0.generatedAddressOffset = "0"
    tools.syncTime = "TRUE"
    ide1:0.startConnected = "TRUE"
    uuid.action = "create"
    checkpoint.vmState = "WindowsXP.vmss"
    tools.remindInstall = "TRUE"
  8. Run WindowsXp.vmx through VMwarePlayer (double clicking on the file will do), and go through the Windows XP installation, this might take anything from 15 min to 1 h depending on your system specifications.
    Installing Windows XP

Some time later… after installation is completed, you’re done! You can now launch Windows Xp from Applications -> System Tools -> VMWare Player and the speed is almost similar to that of running it as a main operating system.

This tutorial is partly based on dryandplain’s HOWTO on Ubuntu Forums.

Running Internet Explorer

Whether you want to open ie-specific web sites or test and debug your application, running internet explorer on ubuntu might come in handy at some point. Microsoft doesn’t offer a linux version of Internet Explorer, so the trick is to run IE through Wine, the windows api implementation for linux.

First make sure you have wine. If you don’t, run:
sudo apt-get install wine

Then download and execute IEs4Linux, a collection of internet explorer versions since v5, as follows:
wget http://www.tatanka.com.br/ies4linux/downloads/ies4linux-latest.tar.gz
tar zxvf ies4linux-latest.tar.gz
cd ies4linux-*
./ies4linux

This will launch an installation panel. You can select which versions of internet explorer you would like to have available, whether you want to enable flash, etc.
Internet Explorer for Linux
Confirm installation by clicking OK.

Once complete, you can simply execute internet explorer from /home/user/bin

Change your MAC address

I don’t really care why you want to change your MAC address on Ubuntu, you’re probably trying to break into your neighbor’s internet connection again… I’m sure though this will come in handy at some point.

Big Mac Address

  1. First of all you need to figure out which connection you want to change your MAC address for (usually LAN is eth0 or and Wifi is eth1). If you’re not sure, type the following to find out:
    ifconfig
    Look for a similar line (which tells you what your mac address is for that particular connection):
    eth1 Link encap:Ethernet HWaddr 00:10:6A:01:FD:92
  2. Shut down the connection:
    sudo ifconfig down
  3. Set your new MAC address (replace eth1 and 00:11:22:33:44:55 by your connection and new MAC address respectively):
    sudo ifconfig eth1 hw ether 00:11:22:33:44:55
  4. Start the connection again:
    sudo ifconfig up
  5. You’re done! The drawback of this method is that it will only change temporarily your MAC address (changes will be undone once you reboot).

To make the MAC address change permanent, proceed as follows:

  1. Open the bootmisc.sh file with your favorite editor:
    sudo gedit /etc/init.d/bootmisc.sh
  2. Add the following at the end of the file:
    killall dhclient
    killall dhclient3
    ifconfig eth1 down
    ifconfig eth1 hw ether 00:11:22:33:44:55
    ifconfig eth1 up
    /sbin/dhclient
    /sbin/dhclient3
  3. Save. This will execute the process described earlier on start up.