Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Sunday, November 1, 2015

Connecting to WIFI from commandline in Linux

For the longest time I was unable to properly connect to any WPA2 enabled WiFi from Linux from pure command line in Ubuntu/Fedora, and had to rely exclusively on NetworkManager. While NetworkManager does a good job of connecting to new networks etc, I wanted more control over the connection process, since I was trying to configure my old thinkpad as a secondary router. In this aspect NetworkManager added unnecessary layers of complexity and opaqueness to the whole process, and frankly sucked.

So after a very long time I was able to properly figure out the simplest set of commands (Run as root user/sudo). Hopefully this helps folks who are trying to manually configure the network.


#!/bin/bash

set -ex

# This can be any file used in the wpa_supplicant command.
WPA_SUPPLICANT_PID_FILE=/var/run/wpa_supplicant.pid
WPA_CONFIG_FILE=/etc/wpa_supplicant.conf

# You can get it from lsmod.
WLAN_MODULE=iwl3945

# You network specific configuration.
IFACE=wlan0
SSID=
LOCAL_IP=
NETMASK=
ROUTER_IP=

# Cleans up any previous state (Optional)
modprobe -r ${WLAN_MODULE}
modprobe ${WLAN_MODULE}

# wpa_supplicant requires the interface to be up.
ifconfig ${IFACE} up

# Do not restart wpa_supplicant if this exists.
if [ ! -f ${WPA_SUPPLICANT_PID_FILE} ]; then
        wpa_supplicant -c${WPA_CONFIG_FILE} -B -i${IFACE} -Dnl80211 -P${WPA_SUPPLICANT_PID_FILE}
fi

# Configure static ip.
ifconfig ${IFACE} ${LOCAL_IP} netmask ${NETMASK} up

# Exit at this point if you want to prevent this from accessing the internet.
# exit 0

# Configure default route.
route add default gw ${ROUTER_IP}

# Add Google's DNS servers for name resolutions, coz comcast's DNS is slow and sucks!
echo nameserver 8.8.8.8 > /etc/resolv.conf
echo nameserver 8.8.4.4 >> /etc/resolv.conf

Tuesday, April 20, 2010

Fixing high pitched audio in youtube video

An hour ago, I was trying to watch a couple of google IO videos on youtube.com, but they are all about an hour long, which means it takes about 1.5 hrs to download on my "broadband" connection. So I decided that the best way would be to download them and save to disk. (Google seems to have enabled a download option on youtube, but it is available only for US... wtf?)

Anyway, I used the Download Helper plugin of firefox to download the video. But when I tried to play it with mplayer, the guy giving the lecture sounded like a chipmunk. I was unable to understand what he was saying since the voice was very high pitched and was like a tape being forwarded, but when I played it on youtube, it sounded fine.

So after checking out a number of other players at my disposal (which was none :), I decided to google to figure out what the problem was. A guy on the mplayer forums had the perfect solution - use the opensource project scaletempo's audiofilter like below

mplayer -af scaletempo

This worked like a charm, and the videos are playing great now.

Saturday, April 10, 2010

Google DNS server

Now-a-days my airtel DSL connection's DNS servers fail to resolve some DNS addresses. I dont have an idea about what the problem is, but frequently I see many domains unable to be resolved and sometimes pretty slow too. So I decided to move to using the Google Open DNS Server. The IP is also a beautifully easy to remember - 8.8.8.8 . 8.8.4.4 is another DNS server of google which can be used as an alternate.

To keep this IP from being overwritten on my Fedora 12 Box, I figured out that the best way to do it would be to write a new file in /etc/NetworkManager/dispatcher.d/ which I names as 25-setdns with the following contents

#!/bin/sh
if [ "$2" = "up" ]; then
cp -f /etc/resolv.conf /etc/resolv.conf.BAK
echo nameserver 8.8.8.8 > /etc/resolv.conf
echo nameserver 8.8.4.4 >> /etc/resolv.conf
fi

This will ensure that the Google DNS will always be used to resolve my domain names.

Do note that I took a very crude, but simple approach here since I dont use any VPNs. If you do use VPN software, then you should make sure that the google DNS is the next entry. since your VPN's local DNS will resolve your VPN internal domains, which are unknown to outside world!

For the paranoid, this is yet more information you are pushing to google! :D

Tuesday, March 16, 2010

32 bit build environment on 64 bit Linux machines

Generally we come across many application which were designed only for 32 bit operation. Although any 32 bit program can be compiled in a 64 bit environment, sometimes the makefile design makes this a huge pain to do. So sooner than later, we end up with a program which just would not compile in a 64 bit environment.

For those extreme cases, you can create a 32 bit chroot environment on your 64 bit box. To do this, first decide on which environment you would like based on the .cfg files in /etc/mock/. Then create a softlink to the particular config to default.cfg . Then just run mock init.

ln -s /etc/mock/fedora-12-i386.cfg /etc/mock/default.cfg
mock init

mock init will use yum to download the required environment. After it completes, then you can run

mock shell

to enter the 32 bit environement. To install any additional packages use the command

mock --install package-name

Note, this can be used simulate build environments other than x86 too, and is used for testing packages on various environments without the cost of physical machines or the pain of virtual machines.

Thursday, December 31, 2009

An iPod Saga - or how I nearly went mad trying to use it


My sister recently gifted me an iPod Nano. I tried to use iTunes to load some mp3s onto it... seemed like a simple enough task given the "user-friendliness"/"coolness" (depending on if you are a fan/fanatic) of Apple. First of all, it says it needs to delete everything existing on the iPod. Hmmm, ok... WAIT! WHAT THE FUCK! why do you need to delete the existing stuff, Cant you just "add" to it? Apple's Steve Jobs makes an appearance on the iPod nano's screen and lists Apple's Official Policy - "Itz my product damn it, I can make it shit on you - if I wish to".

So, I deleted the existing songs, and wrote new mp3s onto it - using iTunes on my office laptop which had windoze I-REALLY-promise-not-to-crash-XP with Service Pack I-NOW-REALLY-REALLY-promise-not-to-crash. (Never mind it BSODs once a day on "heavy usage", But I digress) Now, I wanted to shutdown the damn iPod and there is no "off" button... hmmm, so I had to google the controls on how to shutoff the damn thing. Score one for "user-friendliness"!

All is well for one single goddamed day, when I decided to add a couple more songs. I start up iTunes and connect iPod, and iTunes crashes. ok maybe I connect ipod before starting iTunes? still crashes. maybe I reboot iPod? crashes. maybe I reboot Windoze? crashes. maybe I reboot both? crashes. maybe I update software? crashes. crashes. crashes.

OK, screw it. I would rather listen to my 12 songs already loaded for the rest of my entire life, than put up with another second of this i-Piece-of-CRAP-Tunes. So disconnect & Play. What is it? oh, when i-fucking-stupid-Tunes crashed - it corrupted the song DB and said I have to reset the device with iTunes. and guess what happens when I try to connect the iPod with iTunes?

At this point, I would have taken a comically giant mallet and bashed the evil incarnate out of existence, if it were not a gift. So, I thought therez got to be a way out from the dark side, and sure enough I found out about GTKPOD for Linux (and win-fucking-doze), which kicks iTunes's ass anyday and we lived happily ever after. The battle against the evil machinations of iTunes was over.

But i-will-be-back-Tunes wont give up so soon. I might see it striking back with the i-am-so-DAMN-cool-that-you-bow-to-me-right-fucking-now-Phone. I am counting on Google Phone to kick its ass then :)

Thursday, May 7, 2009

Codefest :)

Two weeks ago, I resurrected an old game that I have written six years ago with my friends. I was not sure how long it would hold my interest. But I guess I was unnecessarily worried. I have completed over two weeks of work on it and spread over 60 changesets, the total Lines of Code changed is easily over 8,000 lines. So that is good work for 2 weeks, working part-time. The entire game has been revamped, with more focus on extensibility.

Although it will look mostly like the old game, extensive code reorganization work was done on it. Loadable TTF Fonts, Loadable Maps, efficient rendering etc are some of the things that I have been working on. Booty has been working on the windows port. Once the code re-org is completed, we plan to add more experience and gameplay changing features like terrains etc.

We also plan on providing a FPS kind of perspective, in future - but that is a long way to go. By using a source code repository, tracking these major release branches is turning out to be very easy!

Sunday, March 29, 2009

Debugging Memory Allocations

Debugging memory allocations etc is always a pain, especially if you have memory leaks which dont cause errors directly. There are a wide variety of tools which facilitate detection, the most useful being Valgrind. The tool is awesome, and it definitely helps in this task. Also GNU libc has a wide ranging support for just this task.

But unfortunately you are not always lucky, and might find yourself working on systems other than Linux. Like FreeBSD. (Well you might have guessed that I am no fan of FreeBSD). Although Valgrind has a port to FreeBSD, it coredumps on startup. So in such dire circumstances you are left with no choice but to start from scratch.

Well everyone knows the standard mymalloc method, which is just a wrapper for the standard malloc call. However here is a more efficient method of doing it. Use the linker option "--wrap,malloc" which will make any references to malloc point to __wrap_malloc() (which you will be writing) and to call the actual malloc from inside your wrapper just call __actual_malloc().

The other option is to use the LD_PRELOAD, which loads your custom library, so any malloc calls are resolved to your wrapper calls. To call the actual malloc(), use the statement - "dlsym(RTLD_NEXT, "malloc");", which will give a function pointer to the actual malloc().

I know the blog is not too comprehensive on how to use these 2 facilities, but now that you know what can be done, just google them :)

Thursday, February 12, 2009

Kernel Crash? Get the dying moans...

So you had a kernel crash, when you were not around, and could not see on the console what the issue was. Or it happened on a server, which has a hardware watchdog which reboots the machine if it has crashed. So here is a way to get the messages which the kernel spit out before dying...

First of all you need to have enabled kernel crashdumps... there is no reason not to. So you are all set and the next time a crash happens the core is saved in a file called vmcore. Now fire up GDB and run the following commands

(gdb) set logging file ~/gdb.out
(gdb) set logging on
Copying output to ~/gdb.out.
(gdb) set print elements 41216
(gdb) set height 0
(gdb) p (char*)__log_buf

The file gdb.out will now contain the messages, open it in and replace all instances of '\n' with a real newline (using something like ":%s/\\n/\r/gc" in vim), and you will see something like this

<4>ide-floppy driver 0.99.newide
<6>usbcore: registered new driver hiddev
<6>usbcore: registered new driver usbhid
<6>drivers/usb/input/hid-core.c: v2.6:USB HID core driver
<6>PNP: PS/2 Controller [PNP0303:KBC0,PNP0f13:MSE0] at 0x60,0x64 irq 1,12

Happy Debugging! (I KNOW it wont be HAPPY :P)

Wednesday, October 22, 2008

Irritating defaults in Linux Applications

If there is one thing that Linux/Unix applications are famous for, other than being open source - it is the irritating defaults they set. And just when you figured out how to disable the most irritating ones, you upgrade to a newer version, and they flood you with even more irritating defaults.

Vim takes the prize in this behavior. Every time I upgrade my Fedora, I get a new version of vim, and while I stopped finding any actual improvements between the vim I used 7 years ago and now, the new set of irritating defaults gets me. After upgrading to Fedora 9, with Vim 7.1, this struck me again.

While writing a piece of C code, the program keeps highlighting matching braces, whenever my cursor passes over them. This is mightly irritating, and I could not think of one good reason for making this the default behavior. I am a hardcore programmer and I know where the F*** match for that brace is!!! And the worst part was figuring out what the offending setting was. I tried googling for it, but it was difficult, since the normal terms like "brace", "bracket", "highlight", "show brace", "show match" did not turn up any results about the offending behavior.

Then it struck me that I was searching with the wrong terms, and put in "vim irritating brace" into google and presto! there were tons of forums where people like me were bitching about that behavior. The answer to turn off the feature is to add this to your .vimrc file

let loaded_matchparen=1

Long way to go... before ppl stop messing around with defaults.. Linux is no longer just a programmer's system, but an end-user system. Not every one would like to spend hours trying to figure out why something has changed since their last upgrade... the sooner the linux developers realize this, the better.

Sunday, September 28, 2008

Windows and the hardcore Linux user

Being a hardcore Linux user is not easy, if you are forced to use Windows, especially by official decree. When I used to work for Motorola, we used to get only Windows machines, and the closest thing to a Linux machine was a Solaris 8 server. I could not get a Linux machine without any official project requirement. In the current startup where I work, Linux is the defacto standard, so my life is much easier.

Recently as part of a new assignment, I had to use a firmware compiler, which unfortunately runs only on windows. So I started installing windows on another machine. After installing XP (after a couple of BSoDs and reinstalls tending from heavy infections), I tried to download the compiler software, and I found out that the network card driver was not installed. I was totally pissed off, and then I remembered that I had a Fedora 9 Boot CD, which made the situation akin to someone asking Salman Khan to remove his shirt, or Deve gowda to take a nap... I immediately installed Linux on the machine, and once it is done, booted into linux and figured out the type of hardware from lspci. I started downloading windows drivers for the network card, and it took me quite a while to get proper network drivers.

It made me realize that I have become addicted to Linux... which although does not have a support group like Linux Addicts Anonymous, is a serious impairment for someone living in Bill Gates's Paradise. Ever since I started using only Linux, both in office and on my personal desktop and laptop, my patience for waiting for stuff to happen was gradually thinning all the time. I became a crazed maniac asking for lower response times to actions - until now. Now that I have to use Windows atleast once in a day, I am back to being more patient waiting for my computer to respond, or taking care of it like a loving father while it throws tantrums like a BSoD or plain stops responding to my pleas or gets sick. Guess with due effort, I can become a WindowsXP certified user soon enough. However to become a Windows Vista certified user, I think I would need a prescription medication like Prozac.

P.S: I am not a Linux Fan, just a user. If there is anything I am a fan of, it would be the Windows OS from a shareholder standpoint.

Tuesday, September 16, 2008

Tunneling to the inside - using SSH

Many networks are setup in such a way that you can access a machine (lets say GGG) in the network through SSH and then you can access any other machine (lets say XXX) in that network using protocols like SSH, telnet, samba etc. But wouldn't it be great to access those services right from the machine (lets say LLL) you are sitting at itself?

Enter the SSH Tunnel. SSH tunnel could be used to create an end to end SSH connection between the local machine (LLL) and the gateway machine (GGG). Then you can access any machine (XXX) from (LLL) itself. The best part is you do not have to be root for this!

First establish the SSH tunnel connection between GGG and LLL using the following command on LLL
ssh user@GGG -L 2000:XXX:22 -N

where, 2000 is the local port on LLL which will act as
a gateway (this can be any port you want)
22 is the remote port on the machine XXX to which you
want to connect to (For example this will be 23
for telnet, 139 for SMB, 22 for SSH etc)

Now your tunnel is setup. Just connect to the machine by connecting to LLL on port 2000 - you will be automatically forwarded to connect to 22 on XXX machine!

Saturday, September 6, 2008

MPlayer - Overcoming in-built low volumes of media files

Many times, the AVI files of movies end up with a very low audio volume causing a lot of pain for use with laptop speakers (esp like ThinkPad), which do not have a very wide range of volumes like headphones or traditional speakers. To overcome this issue, many media players provide an option to increase the volume more than 100% (upto 400% in case of VLC Media player).

After googling and RTFMing for a while I figured out that the way to do this in MPlayer is to use a software volume mixer instead of the soundcard's mixer. You can run mplayer with the following additional options
mplayer -softvol -softvol-max 400 

The 400 indicates that you want to increase the output by upto 400% of the base volume indicated in the media file.

This link has more information on using an audio filter instead of using a software volume mixer to do the same. Happy movie watching!!

Wednesday, September 3, 2008

Ripping DVDs into AVI files

Ripping DVDs in Linux using mencoder is tricky business. No doubt mencoder is the most powerful tool at your disposal with a wide variety of options to convert practically from any format to any format. This in turn also becomes its disadvantage - there are too many options, which an ordinary user cannot understand. There were nearly a 1000 options out there, with a variety of combinations between them, most of which keep taking about stuff like deinterlacing, quantization and key frames.

After trying a number of times to rip DVDs unsuccessfully in Linux, I recently struck gold, while googling, in a discussion forum on how to do this correctly. This is the command that worked flawlessly for me
/usr/bin/mencoder -vf harddup -vf-add 
smartblur=.6:-.5:0,unsharp=l5x5:.8:c5x5:.4
-vf scale=480:360 -xvidencopts
bitrate=900:profile=dxnhtntsc -lameopts
cbr:br=128:aq=0:vol=1 -oac mp3lame
-ovc xvid -o outfile.avi *.vob

The scale= could be used to set the new size of the AVI file. Lesser sizes will result in lesser space. The bitrate= also controls the quality of the movie generated, and is inversely related to the size of the generated AVI file.

Download the mencoder (located in the mplayer package) from some default distribution (I used yum to get it), so that most of the required options are compiled in.

Saturday, June 28, 2008

T61 - The Thinkpad Chronicles Part II

There are some things, you never expect would happen, but they do anyway, leaving you spellbound...

The fingerprint reader on my Thinkpad is one such thing. I have never expected that it would have a proper driver in Linux at all, leave alone be of any proper use. It all changed after I found out about the thinkfinger package. After installing the package and editing some files thanks to www.thinkwiki.org, I ran "su -" and got this prompt - "Password or swipe finger:". It blew me away... Never had I thought that this would happen.

The other surprise was the BlueTooth. I tried to enable bluetooth manually and failed miserably. Then I found out about KBlueTooh. And things were never the same again! I no longer use my cellphone's data cable. I just select files in my phone and do a send via Bluetooth and it immediately starts downloading to my PC. Great!

The next biggest surprize was KPowersave. One I started it, I saw that it had all these fancy options like Hibernate and Sleep. I thought that this is one feature which would never work because you need tons of devices, to work together and go to sleep or wake up across PCI/USB etc buses. Needless to say, They worked like a charm.

For the windows user, the above stuff means nothing. It just works and it always did. But for someone like me who has not kept up with Linux since FC3, this is truly amazing stuff!

Saturday, June 7, 2008

Compiz Rules

Recently installed Compiz-Fusion on my Thinkpad T61, and it rules... ok ok.. what is compiz-fusion? I would say it is the next best thing since the Linux Kernel itself! It is a 3D window manager which started as Compiz, then it was forked into Beryl, only to be merged back again.

Anyway the effects that are possible with compiz are not just cool, but extremely effective too... Imagine you had 10 windows open, and want to go to the one you need. How many ALT-TABs can you do? now with Compiz, you dont have to do any of that painful stuff...

Detailing the features is difficult, so I will let you watch it yourself in this video I made..

Monday, June 2, 2008

T61 - The Thinkpad Chronicles Part 1

I recently bought a Thinkpad T61 with, god forbid, Vista pre-installed. I thought that I would use my Desktop as the Linux machine, and Vista as the laptop one.

One day into using Vista, I ran into this wierd issue where the packets would go only to machines in the local network. Although I set the gateway etc, and no firewall was installed, the packets refuse to move beyond the gateway. It must have been some wierd "user-friendly" setting I guess. But 3 hours down the lane, no luck.

I thought of installing Linux on it. But the Lenovo guys install one big fat C: and thats it. I have to download partition magic - but again no gateway for that.

So I went ahead and formatted the entire C: broke it into 5 pieces and installed Fedora 8 on it. I was using FC3 earlier, and going by the common "knowledge" about hardware support, I assumed I would have half of my functionality not working in Linux. But that was no reason to take crap from Vista.

After installing Fedora 8, I was totally surprised by how well it was designed and how all the laptop functionality was working. Linux has come a long way in the past few years. I would go to the extent that it is just the perception that remains that using Linux is difficult. But it got so much easier.