Setting Default Browser in Linux When Things are Stubborn

Running XFCE4 and setting the preferred applications generally works. However, I noticed that some applications still would not listen to my demands of using a specific browser when launching links. For example, using python’s webbrowser module, and therefore almost any other module opening external links, would use Firefox or Opera (depending on which was available).

# Checking python invocation of a browser
import webbrowser as w

Here’s the culprit, check in your /etc/alternatives/ directory and you’ll see what I mean.

ls -l /etc/alternatives/

Notice the entry x-www-browser. This will be a symlink to a browser and this value was different than what I was attempting to set through the XFCE4 GUI configuration. This problem seems somewhat common as I’ve seen it occur in KDE and GNOME as well.

To fix this issue, simply run the following command and you should be presented with a list of browsers from which you can set.

# Update your browser choice and choose from the presented text menu
sudo update-alternatives --verbose --config x-www-browser

# Now make sure teh proper symlink is setup
ls -l /etc/alternatives/x-www-browser

Now you should have a functional default choice. Those GUIs can suck it!

Transposing Matrices in Python

We have a list of nested lists (list of matrices) that we wish to transpose.

Utilize chain to iterate over our master list (x), flattening it, and then we iterate over the elements and return these as a transposed list element.
We can also check that an iterated element exists and meets some value requirement (here, less than 200).

from itertools import chain
[[y[i] for y in list(chain.from_iterable(x)) if y[i] and y[i] < 200] for i in range(0,len(x[0][0]))]

Here, we are assuming that the subunits are invariable in length.

Checking your SSH key Fingerprint

As many of you probably are already well aware, Github recently was made aware of a mass-assignment vulnerability (1). If you aren’t caught up, you can find the info here on the Github blog (2, 3).

This afternoon, Github sent out an email to users that they had disabled all SSH keys for a users’ accounts and they would remain suspended until validated. You can validate your keys by visiting the SSH keys section under your account settings where you will then be able to reject or validate each of your stored keys. These are listed by machine ID and key fingerprint.

But how do you know the fingerprint of your SSH key to ensure it is the correct one for each machine ID? Simply SSH into each machine and run the following, replacing the location of the key to match where you have yours stored:

ssh-keygen -lf ~/.ssh/

Analyzing Brute Force Attempts in BASH

If you have a public facing SSH server running on the standard port, your message log is probably filled with failed authentication attempts from brute force attacks. Let’s mash up some quick BASH commands to analyze this data. For our purposes, we’ll look at the top attacker IPs and the top usernames tried.

First, pull down all of your message files and decompress them in a working directory using bunzip2.

Once you have all your message logs ready, we will search through them and pull out all of the authentication failure entries and grab the IP and username for each attempt.

grep -r "authentication error" messages* | awk '{split($0,a," "); print a[NF],a[NF-2]}' > attempts

So we first use grep to look for failed authentications by searching recursively for the string “authentication error” in all of our message logs by using the wildcard. We then pipe this to awk and split each input line found into an array delimited by a whitespace. The last part of each line, and therefore our new array, goes something like: ‘authentication error for USERNAME from IP’. So to get the username and IP from our array we can use the array length variable NF and use that to index the variables we need. Here we grab the last using a[NF] and two in from that with a[NF-2]. Finally, we output this to a file called attempts.

Now, let’s use more BASH magic to do the analyses for us. Our attempts file now is in the format as follows: IP USERNAME. We want to see the top IPs and usernames and we can do that with some sorting commands.

cut -d ' ' -f2 attempts | sort | uniq -c | sort -nr > attempts_username
cut -d ' ' -f1 attempts | sort | uniq -c | sort -nr > attempts_ip

Here, we simply grab either the username in the second column or the IP in the first with cut, sort this data, prepend lines with the number of occurrences of each, and then sort by this occurrence number and output to a new file. You can now view attempts_username and attempts_ip to see the top usernames and IPs, respectively, of brute force attacks.

Lastly, we can associate the keep usernames and IPs together and sort on one or the other to see the correlation between the two. To end our initial analyses, we will sort on usernames and find out for the top attempted usernames, what are the top originating IPs.

 sort -k 2,2 attempts| uniq -c | sort -nr | head -n 10

Next time we will be using some GEOIP methods to see where our top attack attempts are originating.

Finding Images with Python

Let’s say we wish to take a directory, the current directory in this example, and gather all the image files. This could be helpful if we were then going to use PIL or Image Magick to manipulate those files, or create a select-able list for use in a GUI, and so on…

There are countless ways we could do this, but here is one with a module that is very helpful with image details.

import os,imghdr
for f in os.listdir(os.getcwd()):
		if imghdr.what(f):
			print f

Monty Hall Sneak Peak

Coming Soon: Monty Hall game player in Python.

For now, here is a little teaser:

The 10 billion iteration answer:
Ran 10000000000 iterations in 104833.082781s {'wins': 3333340564L, 'losses': 6666659436L}

The code is hosted, of course, on GitHub and can be found here:

As soon as I get the rest of it written up I’ll be posting everything here along with a little code walk-through including a very basic example of Python decorators as well as general object-oriented approaches.

Bash Snippet for Git Users

Keeping track of what branch you are using while in a git repository can be a bit of a pain. Adding the following code to your ~/.bashrc file can make it a bit easier since it will prepend your prompt with the current branch.

function parse_git_branch {
    GIT_BRANCH=$(git branch --no-color 2> /dev/null | awk '{if ($1 == "*") { printf "(%s) ",$2}}')
    PS1="$GIT_BRANCH\u@\h:\w\$ "

Here it is working:
[email protected]:~$ source /etc/bash.bashrc 
[email protected]:~$ cd programming/bits
(master) [email protected]:~/programming/bits$ 

Migrating to XFCE

The following was from a Google+ post that I figured I would share here as well.

Well I think I’m finally switching my main desktop from KDE to XFCE. Too much flash and pizazz and having to turn off behaviors and mannerisms for my liking. I guess I’m just getting too old for these young peoples’ desktops.

It was a decent 9+ years, KDE. Sorry you had to turn into GNOME.

A later follow-up comment:

[warning: non-proofread, off-the-cuff rant ahead]

Made the switch last night. Very refreshing!

Not sure if others get the same feeling, but I believe desktops have a measurable weight in terms of interaction and feel. As of late (read KDE 4.*), KDE has lost weight and felt ungrounded. Delayed click responses, bouncing icons, crashing UI effects, windows partially disappearing, and the ‘improved’ graphics all tend to lend way to a flighty feel and experience. But not in a good way, but rather a lack of solidity that I want from a desktop. I want a concrete user experience that shows of it’s strength in the back-end by portraying a solid and consistently reliable front-end. I’ll trade ‘sleek’, ‘cutting-edge’, and ‘feature overflowing’ for solidity and reliability any day. When productivity calls what really matters?

Is it just me feeling the departure of usability? I am one who spends 75% of my computing time tied to a terminal emulator. Maybe I am simply disconnected from what is truly meant to be the modern desktop experience. I look to OS X and Windows Vista/7 for comparison and see similar qualities to KDE 4 and GNOME 3. It’s quite possible that these are the ways of today and are in response to what the majority of users demand from their desktops.

One of the major selling points for Linux is obviously the stability and reliability of the kernel. Therefore, I would expect that the layering OS and desktop should reflect such qualities and do nothing but to bring out the best possible performance and usability of the Linux kernel. KDE4 and GNOME3 seem to have diverged greatly from these ideas. Is it time for distributions to begin defaulting their OSes to a more reasonable (see qualities above for a definition of reasonable) desktop? Maybe Xubuntu should really become Ubuntu, leaving Kubuntu and Gubuntu (?) as unique, non-default variations.