Archive

Archive for the ‘Linux’ Category

What nobody does is nobody’s business.

January 24th, 2012 No comments

Came across this article about the ‘nobody’ user on *nix systems and loved the closing sentence…

If you stare at the list of running processes on your server for long enough, you are bound to come across the user called “nobody”. Before you call a security expert and prepare to fight off a hacker, relax. While the username “nobody” may seem suspicious, it is actually supposed to be there.
.
.
Therefore, do not fear “nobody” and do not take any steps to hinder the user’s ability to run some of your critical applications, like the web server. What nobody does is nobody’s business.

There, you’ve been told!

Categories: All, Linux

Using sed to search and replace contents of next line in a file.

January 24th, 2012 No comments

This example will show you how easy it is using sed to find a particular line in a file and replace all or part of the next line. In this example, using htaccess we want to deny access to all clients except a particular dynamic IP (for your home connection for instance).

Your .htaccess file might look like this:

order deny,allow
deny from all

# allow from example.dyndns.org
allow from 8.8.4.4

And your sed code to update the allowed IP address might look like this:

sed -i -r "/# allow from example.dyndns.org/I{\
n; s/([0-9]{1,3}.){3}[0-9]{1,3}/8.8.8.8/\
}" .htaccess

Let’s look at it piece by piece.

  • /# allow from example.dyndns.org/I will search the file for this string. The I flag at the end makes the search case insensitive.
  • n; will tell sed to continue processing on the next line.
  • s/([0-9]{1,3}\.){3}[0-9]{1,3}/8.8.8.8/ will regex search for an IP and replace the match with your updated IP (in this case 8.8.8.8).
  • .htaccess is of course the input file to process.

Note that in .htaccess files, comments must be on a line of their own. End of line or inline comments are not permitted. In cases where you do not need to replace a part of the next line but rather all of it, our replacement becomes s/.*/8.8.8.8/

Categories: All, Linux

Epson Stylus Photo PX830FWD

January 24th, 2012 No comments

I’ve recently purchased an Epson Stylus Photo PX830FWD which doesn’t seem to want to stop impressing me. Following the death of my last desktop PC, I needed a way to be able to scan again since there’s no way I’d ever be able to use my SCSI scanner. Wireless was the way to go as our lives are surrounded by laptops, tablets and smartphones not to mention the whole internet. Yes, this is a Google Cloud Print enabled printer which means I can print to it from my Android devices, or from Chrome, etc… wherever I am in the world. Epson have their own cloud print service if you prefer, but I haven’t tested it yet.

I was very happy to learn that I can use this device wirelessly from my Fedora machines as well since booting Windows just to scan wirelessly was not something that I was looking forward to have to do. If you do use Windows, the Scan-to-PC feature is pretty cool. It means that you can scan a bunch of documents at the scanner without touching a PC. I stuck Windows inside a VirtualBox guest, mounted a folder on my Synology NAS and scan to that directly. This way my scans are immediately accessible on my local network, remotely via scp/sshfs and even via the NAS’ web interface.

I haven’t tested the fax capabilities yet, but over and above manual mode there is a Print-to-Fax driver under Windows with it’s own address book. And if you ever run out of ruled or graph paper, stick a blank A4 sheet in it, press a button and hey presto. Stick in an SD card with pictures and it will even use a photo as a background. The 32GB Transcend Class 10 SDHC which I bought for my Canon EOS 550D Rebel T2i worked no problem with the printer.

I no longer have a need to print on CDs or DVDs myself, but it has it’s own built-in tray which sounds like a Transformer transforming as it moves around the internal parts of the printer during eject/insert. All in all, an excellent piece of kit which I would recommend any day. Now I need a good shredder (minimum DIN/Level 3). Any recommendations?

Ternary operator for bash scripting.

January 13th, 2012 No comments

I was curious whether or not ternary operations are possible in bash or not. It turns out they are but only for numeric comparisons. So what if you wanted to compare whether a string is empty if you cannot use a string comparison (-Z or -n for example)? Use the string’s length.

As always with Linux, there’s a plethora of ways to do what I am doing below, so this is just an example for your reference:

function check_mounted () {
    local result=`df | grep $1`
    echo $((${#result} > 0 ? 1 : 0))
}

Categories: All, Linux

Relative path to absolute path in a bash shell.

January 13th, 2012 No comments

There are many ways to convert a relative path to an absolute path which can be accessed regardless of the current working directory, particularly useful for scripting. Later, I may make a post to demonstrate the various ways, but the easiest way to do this is to use the readlink utility which comes bundled with just about every Linux distribution with the exception of Debian based systems which use realpath instead.

$ pwd
/media/usbdisk/bin

$ readlink -f ./../mnt/.Skype
/media/usbdisk/mnt/.Skype

When you use readlink in a script and assign a resulting path to a variable, you should add the n flag (-fn) to suppress the trailing newline.

As you might have noticed, I needed to use this in a script to launch Skype but specify that I want to use the data files on my usb key as opposed to the default ~/.Skype location. This however was not working with relative paths and Skype would crash with something like:

$ FatalAssert(*out_ptr != ‘.’ && !strstr(out_ptr, “../”) && !strstr(out_ptr, “..\\”))
FatalAssert(*out_ptr != ‘.’ && !strstr(out_ptr, “../”) && !strstr(out_ptr, “..\\”))

The moment I pass a real path to Skype’s –dbpath parameter, everything works as expected.