Tag Archives: linux

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
w.get().open('http://mutaku.com')

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!

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.

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\$ "
}

PROMPT_COMMAND="parse_git_branch"
Here it is working:
matt@desktop:~$ source /etc/bash.bashrc 
matt@desktop:~$ cd programming/bits
(master) matt@desktop:~/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.


 

Python – Dynamically Printing to Single Line of Stdout

It’s that time again; code snippet time. This is a short and sweet bit of a code that has some really practical and powerful implications.

When handling large amounts of data processing, it’s often desirable to keep the user informed of where things are in terms of progress. However, filling up the scrollback buffer with thousands of “Completed processing [item X]” or “X % completed” is not always desirable. So, how do we handle verbosity without filling scrollback?

Here, we will print data using stdout but we will continually use the same line of the terminal. It’s extremely simple; before each data print, we will move the cursor back to the beginning of the current line, cut out the entire line of text, and then print our current bit of data.

*Note: This is using ANSI escape sequences and therefore will work on any VT100 type terminal.

 

The code:

import sys

class Printer():
	"""
	Print things to stdout on one line dynamically
	"""

	def __init__(self,data):

		sys.stdout.write("\r\x1b[K"+data.__str__())
		sys.stdout.flush()

 

To output data we might do something like:

totalFiles = len(fileList)
currentFileNum = 1.0

for f in fileList:
	ProcessFile(f)
	currentPercent = currentFileNum/totalFiles*100
	output = "%f of %d completed." % (currentPercent,totalFiles)
	Printer(output)
	currentFileNum += 1

 

And here is an example of what our output would look like in a practical scenario.

Processing some data
Progressing along with dynamic status updates

Simple. You can test it out using an ‘for x in range(1,100)’ sort of statement as well. Now we can keep a persistent display of data processing or program progress without slamming the scrollback buffer of the user’s terminal.

Installing Google Chrome in Slackware 13 (13.37)

Trying to install Google-Chrome via RPM (with rpm2tgz) will give you an error with libnss3. Don’t fret, there is already a way to build Google-Chrome without much effort. Below is a quick way using the mirror from Penn State (carroll), but if you have your install media, you can find the same files there in /extra.

Go to: ftp://carroll.cac.psu.edu/pub/linux/distributions/slackware/slackware-13.37/extra/google-chrome/

The README file is pretty self explanatory, but essentially you want to do the following:

  1. Become rootSetup a build directory (mkdir chrome && cd chrome)
  2. Wget these files: [ ftp://carroll.cac.psu.edu/pub/linux/distributions/slackware/slackware-13.37/extra/google-chrome/google-chrome.SlackBuild ] [ ftp://carroll.cac.psu.edu/pub/linux/distributions/slackware/slackware-13.37/extra/google-chrome/google-chrome-pam-solibs-1.1.3-i486-1.txz ] [ ftp://carroll.cac.psu.edu/pub/linux/distributions/slackware/slackware-13.37/extra/google-chrome/ORBit2-2.14.19-i486-1.txz ] [ ftp://carroll.cac.psu.edu/pub/linux/distributions/slackware/slackware-13.37/extra/google-chrome/GConf-2.32.1-i486-1.txz ]
  3. Change execution bit on slackbuild file and run it (chmod +x google-chrome.SlackBuild && ./google-chrome.SlackBuild)
  4. Go to tmp and install the file (cd /tmp && upgradepkg –install-new google-chrome.txz)
  5. Go back and install the other files (cd /root/chrome/ && upgradepkg –install-new *.txz)

Now you should have a fully functional google-chrome install.

Better Python Redirect

Not too long ago, I showed you how to do a simple redirect with Javascript. This works, of course, but it’s far from ideal.

So let’s do it a more correct way.
You ask, “How do we do that and still keep it a hack <scratching head>?”
We will set the Location and Status headers. And to keep things rebellious, we will be using python.

#!/usr/bin/env python
import cgi
url = "http://example.com/"
print "Status: 302 Moved"
print "Location: %s" % url
print

** Note that, as always when doing CGI with Python, we need to have an alone print statement after our header writes. Haven’t seen this before? Here is the same concept, but this time instead of doing a redirect we can see Python CGI in action simply printing out “Hello, world.” :

#!/usr/bin/env python
import cgi
print "Content-type: text/html"
print
print "Hello, world."

That’s it. It’s much more proper, and it’s noticeably faster. Try it both ways and you’ll see.

Don’t have your webserver setup for Python? What are you waiting for? Do yourself a favor and install Lighttpd, configure out fastcgi, and get cranking in Python.

AmazonMP3 and Linux – Clamz

Amazon’s MP3 download store is a very well stocked and typically well priced source for your MP3 music needs. The files free of DRM and are offered up a nice quality without having to pay extra like with iTunes Plus.

In order to download all songs in a single go and receive the full album discount, you need to use the AmazonMP3 download client. The great bit is that a Linux client is offered, and there are several versions packaged up for various distributions, however, these are often hit or miss. There are usually workarounds and methods to get the dependencies you need, but there is a better solution.

Clamz is the hands down way to go. Free and open source, Clamz is a great command line client to download your AMZ files (the full album versions of the downloads).

Head to Clamz project page linked to above, compile and install as usual, and then head over to this link to activate Clamz with your browser.

Now you can One-Click buy your albums. You’ll receive the AMZ file for download which you can then grab your music with something simple like:


mkdir new_album && cd new_album && clamz [thefile].amz

Besides the simple grab as highlighted above, you can do a few other things with Clamz:


$ clamz --help
Usage: clamz [options] amz-file ...
General options:
 -o, --output=NAME:       write output to file NAME (may contain
                          variables, see below)
 -d, --output-dir=DIR:    write output to directory DIR (may also
                          contain variables)
 -r, --resume:            resume a partial download
 -i, --info:              show info about AMZ-files; do not download
                          any tracks
 -x, --xml:               output XML data from AMZ-files; do not download
                          any tracks
 -v, --verbose:           display detailed information
 -q, --quiet:             don't display non-critical messages
 --help:                  display this help
 --version:               display program version

Output options:
 --allow-chars=CHARS:     allow filenames containing CHARS
 --forbid-chars=CHARS:    forbid filenames containing CHARS
 --allow-uppercase:       allow uppercase letters in filenames
 --forbid-uppercase:      forbid uppercase letters in filenames
 --utf8-filenames:        allow UTF-8 filenames
 --ascii-filenames:       force ASCII-only filenames

Filenames (-o, -d) may contain the following variables:
 ${title} ${creator} ${album} ${tracknum} ${album_artist} ${genre}
 ${discnum} ${suffix} ${asin} ${album_asin}

As you can see, Clamz gives you a lot of power over your downloads that you don’t have with the Amazon client [note: it’s been a year or two since I’ve used the Amazon client, things may be different now]. This inevitably leaves you with the ability to create batch processing of your AMZ files if you are downloading a lot of music in a single go. Just download all your AMZ files and then create a simple shell script around Clamz to download all your files to corresponding directories. Splitting out to proper directories could easily be done by reading in parts of the AMZ XML using Clamz with the –xml switch and grabbing any information you might want to use.

Clamz is a must have for those that purchase music from Amazon.  Fast, simple, and essentially foolproof.