Marius van Witzenburg We fight for our survival, we fight!

14Jun/110

How to use the exec() and system() function and capture the returned output in Perl

Posted by mariusvw

When you want to execute a program you sometimes want to use the result of the program you executed.

exec(PROGRAM);
$result = system(PROGRAM);

Both Perl's exec() function and system() function execute a system shell command. The big difference is that system() creates a fork process and waits to see if the command succeeds or fails - returning a value. exec() does not return anything, it simply executes the command. Neither of these commands should be used to capture the output of a system call. If your goal is to capture output, you should use the backtick operator:

$result = `PROGRAM`;

Found this information in a post by Kirk Brown.

14Jun/110

How to trim whitespace with a custom subroutine in Perl

Posted by mariusvw

Since perl does not have a built-in trim function (yet). Use the subroutine below to trim whitespace (spaces and tabs) from the beginning and end of a string in Perl. This function is directly based on the Perl FAQ entry, How do I strip blank space from the beginning/end of a string?. The ltrim and rtrim functions can trim leading or trailing whitespace.

#!/usr/bin/perl
 
# Declare the subroutines
sub trim($);
sub ltrim($);
sub rtrim($);
 
# Create a test string
my $string = "  \t  Hello world!   ";
 
# Here is how to output the trimmed text "Hello world!"
print trim($string)."\n";
print ltrim($string)."\n";
print rtrim($string)."\n";
 
# Perl trim function to remove whitespace from the start and end of the string
sub trim($) {
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($) {
	my $string = shift;
	$string =~ s/^\s+//;
	return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($) {
	my $string = shift;
	$string =~ s/\s+$//;
	return $string;
}
14Jun/111

How to send a message to your iPhone via Pushme.to with PHP or Bash and Curl

Posted by mariusvw

After having Pushme.to installed on my iPhone for over 3 months now I finally had the time to work on some scrips to make it work in different languages for use on servers that I manage.

The PHP version is the less version I use but may be handy for some people.

PHP Version:

function pushMeTo($username, $message, $signature) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'http://pushme.to/' . $username . '/'); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'message=' . urlencode($message) . '&signature=' . urlencode($signature));
    curl_exec($curl);
    curl_close($curl);
}
pushMeTo('username', 'Hello world!', 'God');

A more used version is the commandline (Bash) version:

Bash version:

#!/bin/bash
username="username"
message="Hello world"
signature="God"
/usr/local/bin/curl -F "message=$message" -F "signature=$signature" "http://pushme.to/$username/"

I currently use these for server monitoring on FreeBSD like the GEOM Raid bay and S.M.A.R.T. status of the disks and website monitoring which works quite nice. And in case the internet connection drops I have a backup via SMS with a serial connected cellphone. So this should be quite failsafe :-)

If you have any other language that you want to use such as Perl just let me know and I could write it for you.

I hope you find these scripts useful. Happy texting! :-)

12Jun/110

Test your console if it can display 256 colors with Perl

Posted by mariusvw

With this script you can test which colors your console can display.

You will simply see all the supported colors on your console.

#!/usr/bin/perl
# Author: Todd Larason <jtl@molehill.org>
# $XFree86: xc/programs/xterm/vttests/256colors2.pl,v 1.1 1999/07/11 08:49:54 dawes Exp $
 
# use the resources for colors 0-15 - usually more-or-less a
# reproduction of the standard ANSI colors, but possibly more
# pleasing shades
 
# colors 16-231 are a 6x6x6 color cube
for ($red = 0; $red < 6; $red++) {
	for ($green = 0; $green < 6; $green++) {
		for ($blue = 0; $blue < 6; $blue++) {
	    	printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\",
		   	16 + ($red * 36) + ($green * 6) + $blue,
		   	int ($red * 42.5),
		   	int ($green * 42.5),
		   	int ($blue * 42.5));
		}
	}
}
 
# colors 232-255 are a grayscale ramp, intentionally leaving out
# black and white
for ($gray = 0; $gray < 24; $gray++) {
	$level = ($gray * 10) + 8;
	printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\", 232 + $gray, $level, $level, $level);
}
 
 
# display the colors
 
# first the system ones:
print "System colors:\n";
for ($color = 0; $color < 8; $color++) {
	print "\x1b[48;5;${color}m  ";
}
print "\x1b[0m\n";
for ($color = 8; $color < 16; $color++) {
	print "\x1b[48;5;${color}m  ";
}
print "\x1b[0m\n\n";
 
# now the color cube
print "Color cube, 6x6x6:\n";
for ($green = 0; $green < 6; $green++) {
	for ($red = 0; $red < 6; $red++) {
		for ($blue = 0; $blue < 6; $blue++) {
	    	$color = 16 + ($red * 36) + ($green * 6) + $blue;
	    	print "\x1b[48;5;${color}m  ";
		}
		print "\x1b[0m ";
	}
	print "\n";
}
 
 
# now the grayscale ramp
print "Grayscale ramp:\n";
for ($color = 232; $color < 256; $color++) {
	print "\x1b[48;5;${color}m  ";
}
print "\x1b[0m\n";
25Aug/100

How to fix inconsistent line ending (EOL) style with find and Perl

Posted by mariusvw

If you work with subversion you might get this error when you got files that have been edited on different operating systems like Windows, Linux, FreeBSD or Mac OS X.

Well, the fix is quite simple. You simply replace the wrong line endings with right ones depending of which you want. In my situation I want unix style line endings.

Replace in PHP/JavaScript files:

find ./ -name '*.php' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ;
find ./ -name '*.php' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;
find ./ -name '*.js' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ;
find ./ -name '*.js' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;

In case you want to replace them in multiple file types you can adjust the command. In this example we want to replace in the following file types:

  1. asp
  2. cfm
  3. css
  4. html
  5. js
  6. php
  7. pl
  8. txt

Use the following commands:

find ./ -name '*.asp' -or -name '*.cfm' -or -name '*.css' -or -name '*.html' -or -name '*.js' -or -name '*.php' -or -name '*.pl' -or -name '*.txt' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ;
find ./ -name '*.asp' -or -name '*.cfm' -or -name '*.css' -or -name '*.html' -or -name '*.js' -or -name '*.php' -or -name '*.pl' -or -name '*.txt' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;

Keep in mind, you may use these commands on your own risk. I'm not responsible if you lose your work ;-)

Now you should be able to commit your files again :-)

8Jun/100

How to upgrade Perl 5.8.x to 5.10.x on FreeBSD.

Posted by mariusvw

In the ports collection you can find /usr/ports/UPDATING. Here you can read how to upgrade Perl 5.8.x to the new release 5.10.x.

The description below is copied from the updating file.

Note: you might need to manually upgrade some ports after this. For example, I had to recompile APR, Apache and Subversion.

Portupgrade users:
0) Fix pkgdb.db (for safety):

pkgdb -Ff

1) Reinstall perl with new 5.10:

env DISABLE_CONFLICTS=1 portupgrade -o lang/perl5.10 -f perl-5.8.*

2) Reinstall everything that depends on Perl:

portupgrade -fr perl

Portmaster users:

env DISABLE_CONFLICTS=1 portmaster -o lang/perl5.10 lang/perl5.8
portmaster -r perl-

Note: If the "perl-" glob matches more than one port you will need to specify the name of the perl directory in /var/db/pkg explicitly.