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

Sunday, April 22, 2012

An Ideal Dieting Strategy - Lazy Dieting...



DISCLAIMER: I am no doctor/dietitian, and everything in this post could probably kill you, and you cant hold me responsible for it! :)


Today I came across a news report about extreme dieting using a fricking feeding tube!!! I cant believe these fad diets/weight loss programs people come up with... and it makes me stick to the stomach. So I thought I should write this blog about what worked for me, repeatedly.

As many of you know, I had been an on-off fitness nut for a while... I had done many kinds of diets, my most successful one being the high-protein diet, 500 calorie deficit diet... But I realized something - most diets work (other than some absolutely stupid diets - like alcohol diets etc) - if followed well... the key to a successful diet is to follow it and not cheat. Why is cheating so bad? Because other than the physiological implications of eating more calories, everytime you cheat you feel bad about yourself, and you motivation levels go down... so you concentrate less, and the results plateau... and you get even more demotivated and cheat more until you give up completely. It is an unstoppable vicious cycle.

So why do we cheat on a diet, when we know we want the results so bad? It is simple... dieting is TOO FREAKING HARD.. Right??!! Well the surprising thing is - it does NOT have to be so hard! I realized that a successful diet for you should be something - where you have little incentive to cheat...

So I came up with something called the lazy diet... it is a diet where you change it based on your comfort levels, let the diet adjust to your day, not the other way round. that is the only way to make a diet work long term, without feeling like you have given up control of your life... Remember, If you are spending 1 hour to cook something for your "diet" which you hate.. you are setting yourself up for failure...

For example, earlier my diet used to consist of sprouts, but I realized that sprouts are very hard to get. The ones in stores are typically bad, and making them at home requires a lot of planning. So I removed them from my diet and replaced with egg whites. Now eating egg whites is very hard! I am talking eating 6 eggwhites at a serving, so I tended to cheat... so I went for protein shake tins off the shelf... but they had too many carbs, so eventually settled for fat-free lactose-free milk. Just take it out of fridge and drink it up.

The only catch is you need to have a white list of foods, and keep changing them based on your comfort levels... once in a while you can eat other foods too... just you need to keep track of them. My diet is primarily subways & milk - subway takes 10 mins to drive and pick up, and I can drink milk out of the fridge. Once is a while I skip the subway an eat a biryani... and I dont mean once in many weeks, sometimes once or twice a week I skip some of the parts of the diet. The point is not to go overboard and track the calories & control portions... I never eat a full biriyani at a single sitting, but split it over lunch & dinner.

And diet ONLY 5 days a week... the weekend MUST be free to eat whatever you like, just control the portions a little bit.. always remember, the second piece of pastry is much less tastier than the first...

Dieting does NOT have to be so hard... The problem is most people fail so much and so many times at dieting that they think it MUST be super hard, and so try to work themselves to death trying to stick to it... you can only have such motivation levels for a week or two. If you fight your body, you WILL lose... So diet simple, and make it easy on your life, and the results will follow...

Be Lazy*, Be happy

* Not to be taken literally and to the extreme ;)

Tuesday, July 13, 2010

Sakleshpur Green Line Trek - Childhood dream come true

Ever since I visited Araku Valley by train when I was small child, I loved the railway ghat sections. The huge number of tunnels and bridges (since trains cannot climb anything more than a 1 in 100 grade, compared to automobiles which can easily do a 1 in 10) make for a very entralling experience.

So when my trek mates from the Kottebetta trek informed me about the planned Sakleshpur Green Line trek, I literally jumped with excitement. So I reached bangalore on friday morning and crashed at Sameer's place. In the evening we went to Neeraj's place. There were 16 ppl in the group, so we rented a 22 seater bus and started to Sakleshpur at late night, reaching by saturday morning 6 o'clock. After refreshments and introductions at the KSRTC bus station, we headed to the railway line. On the way we also made a brief stopover at a fort and took some nice pics.

From there we started trekking on the tracks. Since it was a ghat section, there was little to no place to trek beside the tracks, so we had to walk on the concrete sleepers. This was kind of a jarring experience since it took utmost concentration to step correctly, lest we trip over or sprain an ankle. The heat was also unbearable with little cloud cover. Added to this I was carrying a 9 kilo backpack. Anyway we met some workers on the tracks, who told us that since there was some accident a few days ago, all train services on that line were suspended. This gave us a huge relief and we started disregarding basic rules - like crossing bridges in small groups, not resting on the tracks themselves, etc.

After a couple of "photoshoots" in tunnels and on bridges, we stopped at a small waterfall to refill, and I heard the faint but distinct sound of a train horn. I almost brushed it aside since we were not expecting any trains, when I heard a louder second horn. Realizing what was happening, I started shouting "Train! Train!" and everyone scrambled to the side of the tracks and pulled the bags which were lying on the tracks. After a couple of seconds, a very long freight train rumbled past us. It gave us the shock of our lives, since, had it happened on a bridge, it would have been a total disaster!

Anyway we continued more carefully, while periodically listening for train horns or vibrations on the tracks to make sure we wont get caught on the wrong foot again. We passed through a lot of tunnels, the longest of which was more than half a km. There were no lights, making the inside of long tunnels pitch black. They were also occasionally damp, making the sleepers very slippery. My torchlight was woefully inadequate since it was very focused, forcing me to use my more wider cellphone light. Also since the tunnels were designed for meter gauge, and later converted to broad gauge, the space on the sides where we were supposed to take cover, in case of an approaching train, was much smaller than expected.

We had our lunch with bhel and bread close to a railway station. After walking for some more time, we reached a small stream. There most of us took a bath, and continued on our way.

After a couple of trysts with some more trains and maintenance vehicles, we finally reached the Yedakumari railway station. After some dinner, we settled down to sleep in a room in the station. The next day morning we woke up early and after enjoying the morning climate for some time, we started back to the highway along the jungle with the help of a local guide. Before starting we took a couple of precautions against leeches, which would be abundant in the jungle we were about to cross.

We moved downhill rapidly, and then came across a small but fast moving stream, with leeches on the banks. My shoes got completely wet, but I would not dare remove them given the abundance of leeches. After trekking for about 3 hours we reached a much bigger river, with the highway on the otherside which we crossed slowly. While we were waiting for the bus to arrive to pick us up, some of us went for a swim in the river. It was a very beautiful experience for me since it was waist deep water, with soft sand underneath. The water was also not too fast or stagnant.

We got on the bus and reached Bangalore amongst a *huge* lot of dancing and shenanigans. I crashed at Shalesh's place for the next day, and then started back to Hyderabad on monday evening.

Overall it was a great feeling about my childhood dream coming true. I still remember the time I spent wanting to walk the railway tracks of Araku valley (on the Visakhapatnam-Kirandul line) near my hometown. It feels so great to finally do something which you have given up on a long time ago, especially if it was a childhood dream!

Tuesday, June 22, 2010

Leech Attack! - How to avoid those little bloodsuckers

Leeches are pretty common to the Nilgiris. Since I suffer from an advanced case of Bdellophobia (a smug bastard's way of saying Fear of leeches), I have compiled a list of things which will help fellow sufferers in keeping the bloodsuckers away.

Note that Leech bites are pretty harmless. Since the leeches use a powerful anesthetic in their saliva, you will feel no pain when you are bitten. Also it contains a strong anti-coagulant, which means you will continue to bleed for a while after the leech drops off (on its own, or if you pull it away). Leech bites can also leave a mark on the skin. You might get some allergy to leech bites too.

Before we start off, I heard that some species of leeches in kerala can actually jump from trees. I dont know how to avoid them, and I dont care since I trek in karnataka mostly. Anyway for normal leeches, the below techniques can help a lot.

Leeches hate the smell of tobacco, so I use tobacco flakes (ghutka) to keep them away from my feet. I also wear 2 layers of socks with tobacco flakes in between each layer, and also spread it over the shoe. You can also rub some on the lower extremities of your pants. I also stuff my pant in between the two layers of socks to prevent any from creeping up the leg.

Finally if you see those blood suckers on your shoe/clothes/bag, how do you remove them? Since I am scared to death of even touching them, I use a pain spray like Moov, and apply it on the leech. This makes the leech drop off after 2-3 seconds. I heard that other sprays like deodorants also work, but never tested them myself. Also I dont think it is a very good idea to use a deodorant on a break in the skin.

After any trek in a leech infested area, I suggest taking a bath and examining the body for any signs of leeches. Also avoid keeping the bags or shoes near the beds, since leeches might climb onto the bed resulting in untold sleepless nights interspersed with nightmares!

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.