Archive

Archive for the ‘Linux’ Category

What is my IP address from the command line, revisited.

March 27th, 2012 6 comments

Last year I wrote about a little script I use to query checkip.dyndns.org for my current IP address from the Linux CLI. dyn’s checker has served me well over the years and has been rock solid stable, which is why I chose it.

A problem has come about however whereby it is returning a private IP address (192.168.xxx.xxx) which it is taking from the X-Forwarded-For value, rather than ignoring it purely because of the fact that it is a private (internal) IP.

I was trying to find a way to scrape Google’s what is my ip page because it doesn’t suffer from the same bug, and in the process I came about ifconfig.me

So here is my new script, in its entirety:

echo -e "\n$(curl -s ifconfig.me/ip)\n"

If you visit ifconfig.me, on the bottom half of the page you will find a handful of other information you could extract. Now let’s hope that ifconfig.me sticks around for a while.

Categories: All, Linux

Automatically update iptables rules for Pingdom monitors.

March 14th, 2012 4 comments

Pingdom is an awesome service that tracks the uptime, downtime, and performance of websites (you can see an example of the public stats of this server here). If you have a firewall running on your system, you need to whitelist Pingdom’s servers or else their monitors will fail. As their servers may change at any given time, it is better to automate this whitelisting by realistically and responsibly polling the RSS feed of their monitoring servers.

It’s been done before, but this is how I have chosen to do it.

First, a little php helper script to extract the Active IP addresses of the monitors. For this example, let’s save it as pingdom.com.php alongside our bash script which will be executed by cron.

preg_match_all(
    '/((\d+\.){3}\d+).*?Active/',
    file_get_contents('https://www.pingdom.com/rss/probe_servers.xml'),
    $ips
);

echo implode("\n", $ips[1]);

Then, our bash script which is called from cron:

for ip in $(/usr/bin/php pingdom.com.php); do
    $IPTABLES -A INPUT -s $ip -p icmp -j ACCEPT
done

Of course, this is just an example and you will need to modify the firewall rule(s) according to your needs.

Raspberry Pi

March 5th, 2012 3 comments

Raspberry Pi

If you have been living under a rock for the past week, you might have missed the buzz around the Raspberry Pi, the “ARM GNU/Linux box for $25”. The above illustration, taken from issue 156, April 2012 front cover of Linux Format gives a clear message about the potential that this device will have. If you are interested in this stuff, you should buy the magazine for the 7 page write up about it.

I am not sure what I will do first when I eventually get my hands on one (probably take a photo of it with my Canon EOS 550D Rebel T2i, install Fedora, connect it to my DS410, install XBMC, install asterisk and then have a beer).

If, like me, you can’t wait, then go here for emulator information and downloads. If you think you want one, you should probably register your interest here or here after reading the FAQs.

Categories: All, Linux

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 2 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 2 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.

Be careful when using find with the delete flag to clean old files.

January 9th, 2012 1 comment

The linux find command is very powerful. I use it all the time, either manually or in cron scripts to delete useless Thumbs.db files for example. A word of caution however, if you intend on using the -delete flag with find (instead of -exec rm… or xargs etc.).

This is very bad:

find / -delete -type f -name 'Thumbs.db' -mtime +7

This is good:

find / -type f -name 'Thumbs.db' -mtime +7 -delete

The order of the -delete flag is very important. In the first case find will delete everything it finds (totally ignoring the subsequent criteria), whereas the second case will delete what is matched.

In other words, the order of the find flags/parameters/actions are very important. If you are in doubt, or even if you are not, you should always have a backup in case something goes wrong.
Read more…

How to install KDE on Fedora Core 13 and onwards.

November 18th, 2011 9 comments

I haven’t used KDE for a very long time, obviously since before Fedora Core 13. Back then, the yum group for KDE was named kde-desktop, so a simple sudo yum groupinstall kde-dekstop would set you on your way.

Since FC13, the group name has changed to the not-so-obvious KDE Software Compilation.

sudo yum groupinstall “KDE Software Compilation” (with the quotes) and you’re off!

I’m installing KDE in a fresh install of FC16 as I’m writing this, because like Leonid, one of the things which annoy me about GNOME 3 is the inability to see a Skype icon with notifications of incoming chats without having to move your mouse to bottom right corner all the time. We are using Skype as an example here, there are of course other icons which one might miss, such as the Dropbox icon for example to know if any synchronisation is in progress.