Posts

Showing posts from 2009

An excellent PHP script to handle time difference

I am working on a project that involves displaying the time difference between a start and end time on a website. The following script is lifted directly from: http://aidanlister.com/2004/04/making-time-periods-readable/ /** * A function for making time periods readable * * @author Aidan Lister * @version 2.0.0 * @link http://aidanlister.com/2004/04/making-time-periods-readable/ * @param int number of seconds elapsed * @param string which time periods to display * @param bool whether to show zero time periods */ function time_duration($seconds, $use = null, $zeros = false) { // Define time periods $periods = array ( 'years' => 31556926, 'Months' => 2629743, 'weeks' => 604800, 'days' => 86400, 'hours' => 3600, 'minutes' => 60, 'seconds' => 1 ); // Break into peri

Things to do with an iPod and Fedora

My iPod Nano 8gb. ( not the fancy new one with the video ) works perfectly well inside fedora so long as you bear in mind the following basic points: The thing doesn't play ogg files so if you have any of those on your system, they can not be synced. Depending on the application you use, you end up with podcasts being saved under music. Sometimes you get images sometimes you don't. Albums with Various Artists do not appear under compilations. Instead your Artists menu shows a long list of artists most of which only have one track. Applications that ship with fedora and support the iPod: gtk-pod ( manage your ipod ) gpodder ( download podcasts etc - these actually go into the podcasts menu on the device. ) Amarok Rythmbox Other music applications I have played with that support the iPod: Songbird 2 I should not here that once again, I have found that Amarok 1.4 is still the best player out there. I had to install it using the F11 binaries ( just search for them on google ) and

Bash Plus Spaces In Filenames Equals Headache

OR NOT... Check this out: http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html

Understanding Redhat Kickstart

Image
Kickstart provides linux system administrators a method for installing linux on pc's in unattended mode. A properly formatted Kickstart configuration will perform all tasks such as setting the root password, the networking interfaces, the disk partitioning scheme as well as the installation of packages. I needed to build a clustered reverse proxy the other day to test if it could be used for SSL termination. I was looking for a way to aggregate multiple webservers behind a proxy server so that they could have their SSL terminate at the proxy, then the HTTP traffic passed through a Level 7 ( application layer ) scanner to test for sql injection and the like. So I fired up my trusty Virtualbox and built a standard Centos 5.4 server. I copied the CD into the ftp directory and started NFS with an export to that FTP directory. This server was to serve as an install NFS server for the 2 proxy servers as well as a yum repository for them. That's why I placed the CD files in the F

Scheduled Tasks on HA Pairs in Windows

Sometimes you might find yourself deciding that a Scheduled task is required for some report or file transfer on a production system. The production system, however, is part of a high availability (HA) pair. The particular method of dealing with HA in this particular ( hypothetical of course ) case works by moving an IP Address called a Virtual IP address between the servers of the HA Pair. Whichever server holds the VIP is the current node. Your application needs to know if it is executing on the current node before starting. Wrap your application in a very simple snippet of Comand Line script that: Looks for the VIP in ipconfig. If no errors found then jump to the section of your script that launches your application or another script to do the task. If an error is found then jump to the section of your script that notifies the log. @echo off REM #Testing FIND in IPCONFIG SET VIPTHATWORKS="192.168.122.1" SET VIPTHATFAILS="192.168.122.127" ipconfig /all | find %

Things to do with an iPod

I have had my iPod 8gb Nano ( Video ) for about 4 months now and I have been having heaps of fun with it. I know it's not cool like an iPod Touch or iPhone but it is still plenty powerful. I have tried using it with Linux and although it is supported in Amarok ( I am still on Amarok 1.4 due to lack of Fedora packages for the new version which actually has replay-gain for Xine included ) I have found that the support for pod casts is a little limited. For example: I like to download all the pod casts in a feed at once so that I can catch up to the current feed. So I stick with iTunes. This means sticking with a windows pc or if you are lucky to own one, a mac. Initially I spent heaps of time building the perfect play list on my iTunes and moving that over to the iPod. This was fun but only for a short while. The main problem being that no matter how many weeks of music you have on your iPod, you still end up with the feeling that you have heard it all before. I was struck by t

Rip from WAV to MP3 and add ID3 tags -- All with Perl

Ok - so I know I have said before that I am not much of a perl fan. ( yes it's safe, I am wearing my flame proof suit. ) I will, however, say now; I am coming around. It would seem that I am starting to see the light. I needed to extract some files from a CD I have and convert them to MP3. The CD was not made with extracting to mp3 in mind if you get my drift. It's ok, because the CD is my copy and I want to listen to it on my iPod. Regular extracting on iTunes was not working out for me. So I rolled my chair over to my trusty Linux Box and perl, id3, cdparanoia and lame. First thing I did was use cdparanoia to extract all the tracks. This went swimmingly leaving me with a folder full of files called 01.cdparanoia.wav or something just as useless. The files played ok but I needed to name them all. This particular CD was not listed on Music Brainz so I couldn't do anything fancy with FreeCDDB or whatever they call that now days. * shudder * - I had to manually edit

Real simple to connect to MSSQL from Perl

Again, this took me a little while but here I am. Connecting to a database ( SQL SERVER 2008 ) from a Windows Server 2008 VM I own ( thanks TECH ED 2008 ) and Active State Perl. Install Scite cos it's a nice editor and you are away laughing... #!/usr/bin/perl use DBI; my $DSN = 'driver={SQL Server};Server=localhost; database=AdventureWorks;TrustedConnection=Yes'; my $dbh = DBI->connect("dbi:ODBC:$DSN") or die "$DBI::errstr\n"; my $sth = $dbh->prepare('select top 10 * from Sales.vSalesPerson') or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute(); while( @data = $sth->fetchrow_array()) { foreach(@data) { print "[$_]"; } print "\n\n"; } $sth->finish; $dbh->disconnect; Results: C:\Users\dave\Documents>perl perl1.pl [268][][Stephen][Y][Jiang][][North American Sales Manager][238-555-0197][stephen 0@adventure-works.com][0][2427 Notre Dame Ave.][][Redmond]

Basic Perl file parser - useful for working with logs

Really I just need to remember how to do this: #!/usr/bin/env perl #yum log file use warnings; use strict; open FILE, "/var/log/yum.log" or die $!; while (<FILE>) { /^(.+?)\s(.+?)\s(.+?)\s(.+?)\s(.+?)\s/; my ($month,$day,$time,$action,$package) = ($1,$2,$3,$4,$5); if( $action =~ /Installed/ ) { my $suffix = 'th'; if( $day == 1 ){ $suffix = 'st'; } if( $day == 21 ){ $suffix = 'st'; } if( $day == 31 ){ $suffix = 'st'; } if( $day == 2 ){ $suffix = 'nd'; } if( $day == 22 ){ $suffix = 'nd'; } if( $day == 3 ){ $suffix = 'rd'; } if( $day == 23 ){ $suffix = 'rd'; } print "$day$suffix of $month at $time\n" } } close FILE

Joining multiple PDF documents with Gostscript.

This really works. I downloaded all the chapters of beginning perl as published here: http://www.perl.org/books/beginning-perl/ To do it in a painless way I first used curl to grab the html. curl http://www.perl.org/books/beginning-perl/ -o bp.txt Then I used grep to grab the hyperlinks to pdf files and piped that into another file. cat bp.txt | grep -o "<a.*pdf"> bp2.txt Then I used sed to clean up a little bit. cat bp2.txt | sed "s/^<a href=\"//g"> bp3.txt Then I used wget to download each pdf. for line in `cat bp3.txt`; do wget $line; done So now I had a whole bunch of pdf files that together make up one book. A quick hunt on google produced this guy. http://www.linux.com/news/software/applications/8229-putting-together-pdf-files And I was away laughing. gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=beginning-perl.pdf 3145_Intro.pdf 3145_Chap*.pdf 3145_App*.pdf 3145_Index.pdf

Samba File Sharing to Windows ( Includes Vista )

I found this: http://www.mjmwired.net/resources/mjm-fedora-f11.html#samba Here are the main points: Install Samba yum install samba samba-client Configure Samba ( as root ) # vim /etc/smb.conf Add a workgroup name under the [globals] section. Add your fileshares at the end of the file. [TerraByte] path = /media/TerraByte public = yes writable = yes This will create a writable share. File permissions in linux must match. Ie: (dwrx-dwr-dwr ). If your drive is an NTFS formatted drive, these permissions can not be applied. It doesn't matter though because it still works. Set up samba users. Users must already exist as users on your linux system. They must be valid users. The password does not need to match. There are a bunch of warnings that can be ignored. # smbpasswd -a username New SMB password: Retype new SMB password: account_policy_get: (warnings ignored) Added user username. Start the samba service and enable it in chkconfig levels 3 and 5. Enable samba in the fi

Having fun with LaTeX

LaTeX - Pronounced Lay-tec ( as in tech ) as in don't pronounce the X like X-Ray but rather as a soft c as in tech. Ok so now we have the way to say it under control, I thought I might describe it a little. LaTeX is a document mark-up language based on Tex. ( just look this stuff up on Wikipedia cos I am too lazy tonight to starting collecting references and formatting them correctly and worrying about how the document is structured etc... ) Its all so boring - Enter LaTeX to save the day. On Fedora you can download the LaTeX plugin for Gedit and start editing your next book, thesis, report or article. Basically it provides a mark-up language not unlike that provided by MediaWiki. It lends itself nicely to multiple document type conversions as the content formatting is described rather than embedded. LaTeX takes this one step further by handling all your references inside a separate file compiled by the companion application called Bibtex. The Gedit plug-in will actually prom

Orcon owns iServe owns Kiwiwebhost

It was announced ( http://www.iserve.co.nz/news.php ) on the 1st May that Orcon have acquired iServe. iServe are the parent company for Kiwiwebhost. It seems like a remarkable coincidence that in the days leading up to this acquisition, Kiwiwebhost rushed into upgrading their servers from php4 / MySQL 4 to php5 / MySQL 5. Besides my moaning about all the hard work I had to suddenly do to get my one osCommerce website ready for this upgrade, I am ultimately pleased that the upgrade was made. PHP5 offers so much more in terms of Object Oriented Programming ( OOP ) as well as increased security. Kiwiwebhost have also turned Register Globals off. This closes a fundamental security hole. So well done to Kiwiwebhost for implementing a large upgrade in a short period of just two days with outages amounting to less than 12 hours per server that was being upgraded.

osCommerce & Register Globals

What happens when your web host finally get's around to upgrading to php5 and MySQL 5? Here is what happened to me in order: 1. Rejoice because finally your website is hosted according to industry standards. 2. Panic because you are unprepared. 3. Worked through the night to get the website updated. In order to migrate an osCommerce website from php4 to php5 there is really only one thing you need to worry about and that's a little "feature" in php called, Register Globals. Register Globals makes every variable contained in the URL, POST, SESSION and ENVIRONMENT directly available in every script. What this means is that with register globals turned on, you can write a webform that posts a variable to a script and that script can refer to the variable in the POST by name. For example: <?php /* ** Example script to show what happens when register globals is turned on. ** ** A form will submit POST data to this website as follows: ** String $_POST['name'];

Recycling old computers

I have an old PIII at home with 256Mb Ram and a 20Gb hard disk. Graphics is nothing to write home about and it will not support Vista or KDE 4.2. I know there are many, many blog posts on the internet that discuss this so I will simply add to the general wash. Ubuntu Server: File server with samba Apache webserver with dyndns.com ( free dynamic IP to Domain Name resolution ) - This can be useful if you are a freelance website designer / programmer and you need to publish draft websites for your clients prior to going live. Xbuntu Desktop: Install Childsplay - a very basic suite of games that are designed to develop mouse and keyboard familiarisation in a fun and easy to understand environment. - In fedora this was just as simple as, "yum install childsplay" Install the Mame Emulator and download roms for some of those neat and safe for kids arcade games that you played in the 80's at your local cornershop or arcade. If you have an XBOX controller - install the drivers

Write .NET applications on Mono and use them in Windows

Image
I am no professional .NET developer but I thought I would have a crack at a simple application written on my Fedora 10 in the Monodevelop IDE and then run the same exe it compiles in a Windows XP. It took a while but I got there in the end and in retrospect it was not too hard. Just a few dependancies, a short forum-search for a solution to a small problem. Here is what I did: 1. Install mono, monodevelop and gtk-sharp ( GTK# ): The monodevelop IDE provides a gui designer for gtk#. So I thought I would try this out and see how I could get that working in windows too. In Fedora I just ran the following: # yum install monodevelop gtk-sharp* As usual I let yum decide dependancies for me. 2. Start monodevelop and create a new solution: Select the C# templateand then pick Gtk# 2.0 Project Under Solution name: type "tutorial" and under Name: type "myGuiApp" to create a Gtk# GUI application under the folder {$HOME}/Projects/tutorial. Click Forward and select your Targ

Blogger Templates

I have changed the template only slightly. It seems that this default denim template also has a "wide" option. The colours and widgets remain the same. The only difference is that source code can now be read more easily as the layout will stretch with the browser.

CSS Floating Box Model

A simple floating box model example showing how to "float" div elements within another div element. This example includes boxes that float left and boxes that float right. It is easy to imagine how one could beef up the aesthetic quality of these floating boxes with rounded corners or varied background colours or patterns. The boxes used here are for demonstration purposes only. <html> <head> <title>Floating Right and Wrapping</title> <style> #maincontent { display:block; width: 600px; margin: 0px auto; } .wrapcontainer { float:left; width:600px; padding: 5px; border: 1px solid #777777; margin: 5px; display: block; font-family: arial,helvetica,sans-serif; } .sidebar-right { float: right; border: 1px solid #777777; padding: 5px; margin: 5px; display: block; width: 210px; font-family: arial,helvetica,sans-serif; } .sidebar-left { float: lef

Learning Linux at 84

I am sure I have never encountered a person at the grand age of 84 learning Linux before today. As it turned out I sold a Fedora 10 DVD from my website ( www.thelinuxcdstore.com ) to a gentleman in Auckland who needed some help. Usually I shy away from supporting Linux unless it is something related to the media I sell. In this particular case I needed to get involved because I needed to determine where the problem my customer had came from. We got on the phone together and I ran up a fresh install of Fedora 10 on a VMWare Virtual machine so I could guide the customer through the install. Considering the initial failure as the customer described it was most likely related to Anaconda, I decided that a text install might be more successful. Fedora keeps the text installer hidden. You have to hit the TAB key to edit the boot parameters when the DVD boots up and add the word "text" at the end of the line. After that the DVD boots into a TEXT only installer. We had a scary

Simple Cluster with Heartbeat

Image
Following on from my previous post about setting up a reverse proxy in Fedora 10, I now delve into high availability. The plan here is to create two reverse proxy servers and cluster them together in an Active / Passive configuration with automatic fail-over. So here is our trusty network diagram: You will notice the shared IP and Proxy02 with connected to Proxy01 with the cross-over cable. You need the cross-over cable for the heartbeat keep-alive messages. As I am using VMWare I have used the HOST ONLY network configuration for the second nics on the servers thus simulating a physical cross-over cable. Here is the network configuration on each proxy server: Proxy01: eth0- ( same network and subnet as the client ) IP =192.168.0.11 SUBNET MASK=255.255.255.0 DEFAULT GW =192.168.0.1 HOSTNAME = proxy01.latham.internal eth1 - ( different network and subnet as the client ) IP = 192.168.169.11 SUBNET MASK = 255.255.255.0 DEFAULT GW = none Pr

Clustered Reverse Proxy with Fedora

Image
I was given the enviable task of setting up a reverse proxy in Fedora. A reverse proxy is a piece of software that is installed on a device that has network access to an external and internal network. The proxy acts as a bridge between the two networks. A normal proxy as installed in most company networks allows all users on the internal network to access the external network. A reverse proxy allows clients on the external network to access services hosted on the internal network. It can be installed on the front end of the network and will proxy specified traffic through to the internal network. Here is a diagram of my lab network. In this example we can see: Multiple clients are connecting to the proxy cluster. ( more on clustering in the next article. ) Only one node in the cluster has possession of the shared IP address. The Apache web service on the proxies are configured to listen on the shared IP address. The host web server behind the resource zone is serving traffic to th

Commandline to reliably burn an ISO ( On My Machine )

In order to reliably burn an ISO image to CD or DVD there are a couple of options that need to be set. That's if you are planning on using terminal commands ( command line tools ) to do the job. The disk must be written in DAO ( Disk At Once ) also known as SAO ( Session At Once ) The disk must be written in as slow a speed as possible. On my PC that is about 8x for a CD and 4x for a DVD. Remember speed ratios differ between DVD and CD. My Scripts: ( saved in /usr/local/bin ) [dave@fedora10 bin]$ cat burn-cd #/bin/bash #burn CD # Usage - Enter full path to distro here. /usr/bin/wodim dev=/dev/sr0 driveropts=burnfree fs=14M speed=9 -dao -v -eject $1 [dave@fedora10 bin]$ cat burn-dvd #/bin/bash #burn CD # Usage - Enter full path to distro here. /usr/bin/wodim dev=/dev/sr0 driveropts=burnfree fs=14M speed=4 -dao -v -eject $1 The options for wodim ( cdrecord as it is called nowdays ) Note the Speed, Burnfree and -dao options. -dao tells Wodim that you want to burn the ISO image in

Helping out folks with internet access difficulties.

I subscribe to the NZLUG ( New Zealand Linux Users Group found at http://www.linux.net.nz ) mailing list and received a very interesting item in my inbox this evening. Internet Assist North Shore are looking for ways we can help out families who do not have the wherewithal or sufficient finances to get on-line. The idea is that we donate old hardware with opensouce only software and operating systems along with our time for installation, setup and training if required. Another idea was also put forward to provide the actual internet access. However that is possible. I think they are looking for ideas. One option might be through sponsorship. Corporates looking to provide their services might, for the extra publicity we could maybe provide, cover the $25.00 a month for a year or even 6 months to get folks on-line and up and running. It might be tricky to work out who qualifies... Anyway - those are my ideas... For now anyway. Check them out at: http://internetassistnorthshore.blog

Flock - Social Web Browser

Those of you who are into social networking and like to keep up to date with your feeds, facebook, twitter, youtube and anything else, then you might like Flock. Flock is a mozilla based portal into all of these services.  It saves your account login data and allows you to both browse and social network at the same time from the same application.  Rather than all your services in multiple windows, you have them in one application. I recommend a large screen though.  Wide screens are especially suitable for thos wide sidebars. Check out Flock at http://flock.com/ There is a windows and a linux version.  I didn't bother to check if there was a mac version. Blogged with the Flock Browser

Linux Security

I would like to share this post on Linux Security. I would place it in my shared items but I thought that if I left it there it would go largely unnoticed. The author has written a fantastic entry about the User effect on security within a Linux environment. He makes points about users running arbitrary code as root, installing unsigned packages and a lot more. It is well worth a read! http://www.happyassassin.net/2009/01/20/on-linux-security/ I would like to note that I am merely trying to introduce a great piece of writing by another blogger.

Bash Progress Bar

Ever wanted to show a progress bar on your bash scripts? I am talking specifically about the progress of file transfer or raw read scripts. It might be easier to explain with an example: I have a script that reads raw data from a CD or DVD ROM disk and pipes that data to md5sum or sha1sum to find if the data on the disk matches the published md5sum or sha1sum checksums published by the vendor. I download ISOs and verify them before I send them to my customers. I wanted to have the script show a progress bar so I started searching... Here is how I have implemented clpbar Installation instructions ( Fedora 10 x86_64 ) Download and install clpbar ( bar-1.10.9.tar.gz ) -- See references at the end of this script. tar xvfz bar-1.10.9.tar.gz cd bar-1.10.9 ./configure make su -c "make install" Usage example #!/bin/sh # # Start with verifying CDs # device="/dev/cdrom" # pass the type of checksum into the script. (md5sum|sha1sum) checksumtype=$1 #Find details of the devi

The New Paymex

I had a crack at installing Paymex on my osCommerce website this evening. I will try to summarise the experience: INSTALLATION Paymex provide an osCommerce module for download from their website. It is mind numbingly simple to install. A simple upload of files is all that is required. There is absolutely no php editing required. This is always a plus for me. osCommerce code is painful enough and in a production environment any update that does not require php code editing is very useful. 5 out of 5 for installation. CONFIGURATION There is only one item of identification required by the Paymex module config. A business id. This looks remarkably like a windows uid. I just copy pasted it from the Paymex website while logged in there. There is the option to have the paymex module enabled or disabled, Test mode or Production mode, set the acknowledged order status and the sort order. So not much to configure really and very simple to understand. 4 out of 5 for configuration. Not