Friday, January 28, 2011

Record speaker audio on Ubuntu

Ever wondered about recording audio playing on your computer speaker? Well, I did. There was this ringtone which i could play from a website but could not download it. So here you got and record it to your local file. I am using Ubuntu 9.04 (Karmic Koala). This has also been tested on various flavors of Ubuntu 10.04 and 10.10.

On the command line, install the following packages:
sudo apt-get install pulseaudio-utils
sudo apt-get install sox 
Open up your favorite editor (e.g. vi /tmp/record.sh) and paste the following code in it.
#!/bin/sh
WAV="$1" && [ -z "$WAV" ] && echo "Usage: $0 OUTPUT.WAV" && exit 1
rm -f "$WAV"
# Get sink monitor e.g. alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
MONITOR=$(pactl list | grep 'Monitor Source:' | awk '{print $3}')
[ -z "$MONITOR" ] && echo "Failed to fetch sink monitor" && exit 1
# Record it raw, and convert to a wav
echo "Recording to $WAV file ...Press CTRL-C to stop recording"
parec -d "$MONITOR" | sox -t raw -r 44k -sLb 16 -c 2 - "$WAV"
Save the file. I saved it in /tmp/record.sh. Change the permissions of /tmp/record.sh to 755.
chmod 755 /tmp/record.sh
/tmp/record.sh /tmp/filetostore.wav
Now play any song from your favorite website and  run /tmp/record.sh /tmp/filetostore.wav, it should record  the audio in wav format in /tmp/filetostore.wav.

The output file is in wav format. You may use audacity to convert the output file into the formats of your choice e.g. mp3. Audacity is available in ubuntu repositories and you can install it using: apt-get install audacity.

Enjoy!


Tuesday, January 25, 2011

Cyberoam IPSec VPN client on linux

My struggle of connecting my Ubuntu-9.04 (Karmic Koala) to Cyberoam's ipsec VPN is worth a mention. Cyberoam provides a road-warrior client for establishing VPN connections from Windows, but nothing for Linux based systems.

So here you go. On Cyberoam:
  • Logon to Cyberoam GUI
  • Click on the following menu on left panel: OBJECTS --> Hosts
  • Click the ADD button
  • Add the local network (behind Cyberoam) that you wish to access over VPN and give it a name. See the snapshot below. The local network behind my Cyberoam is 192.168.0.0/20, I named it as 192series. Press OK.

  • Click on the following menu on left panel: VPN --> IPSec
  • Press the ADD button. Follow the steps as per below snapshots.
  • If everything went well, you should see the following screen.
  • The Red button should turn green after pressing it.
  • Now you need to allow the VPN connection in firewall. On the left panel, click on Firewall --> Rule. Click ADD to add a rule to allow traffic from VPN to LAN. Do the same for allowing traffic from LAN to VPN. Here's a snapshot:

Here, you are done with the Cyberoam part. Now turn to your laptop or desktop:
--------------------------------------------------------------
 
On your laptop or PC:

  1. Install the openswan package from the default repositories. For debian based systems:
    apt-get install openswan
    For redhat based systems:
    yum install openswan
     
  2. Add the following configuration to your /etc/ipsec.conf. Change the network parameters as per your network scenario.
    conn roadwarrior
            rightsubnet=192.168.0.0/255.255.240.0
            auto=add
            type=tunnel
            right=<your_cyberoam_public_ipaddress>
            left=your-laptop-ipaddress
            leftnexthop=%defaultroute
            authby=secret
            keyingtries=3
            compress=yes
            failureshunt=drop
            dpddelay=30
            dpdtimeout=120
            dpdaction=clear
            pfs=yes
            ike="aes128-md5-modp1024,aes128-sha1-modp1024,3des-md5-modp1024"
            esp="aes128-md5,aes128-sha1,3des-md5"
     
  3. Add the following line in /etc/ipsec.secrets
    %any <your_cyberoam_public_ipaddress>: PSK "<pre-shared-key which you defined on cyberoam>"

  4. Load the connection in ipsec by using the following command on command line.
    sudo ipsec auto --add roadwarrior
  5. Start the connection and test if you are able to reach the local network behind Cyberoam.
    sudo ipsec auto --up roadwarrior
    ping 192.168.13.102

    That should get your VPN up and running. In case of problems, you may contact Cyberoam Support at:
    http://www.cyberoam.com/contactsupport.html

Wednesday, January 19, 2011

openwrt-x86-ext2.image in qemu on linux

I just downloaded the openwrt firmware and wanted to mount it in my ubuntu laptop. Here's the trick:


Get the starting offset of first partition in the image:
startsector=`fdisk -lu openwrt-x86-ext2.image 2>/dev/null | grep openwrt-x86-ext2.image2 | awk '{print $2}'`
Set-up a loop device to access the file offset as a block device, assuming 512 bytes sector size: 
losetup -o `expr $startsector \* 512` /dev/loop0 openwrt-x86-ext2.image
Now, mount the device to a directory:
mount /dev/loop0 /mnt/
To unmount the device:
umount /dev/loop0
To delete the loop device:
losetup -d /dev/loop0

To run the image under qemu with 128 MB ram and single network card:
sudo qemu -m 128 -hda openwrt-x86-ext2.image -net nic,model=ne2k_pci -net tap,ifname=tap1,script=~/qemu/qemu-ifup.sh