Showing posts with label module. Show all posts
Showing posts with label module. Show all posts

Wednesday, December 17, 2008

Color Completion Using Zsh Modules On Linux Or Unix

Hey There,

How's this for a long-overdue follow up. If my powers of site searching don't fail me, we haven't touched on the Z Shell (zsh) since our post, from July 2008 about multiple stream output in zsh which links back to another post on zsh's extended globbing functionality. This post is more of a follow up to that one than the other, but who's counting? ;)

Here's another cool thing you can do with zsh that involves color. Personally, I'm happy with a two-color scheme that makes my eyes sizzle (like blue on red - or red on blue - both of which look somewhat 3 dimensional), but you have to make room for color in your life. It's not all black and white, even though it seems that way most of the time ;)

In order to make use of this functionality, you'll have to use the modular functionality of zsh. Today, we'll be looking at color completion, but this isn't to say that this is the limit of what you can do with the shell. Just the limit of what I can manage to type today... Doing color completion in zsh requires two modules: complist and colors. The first requires you to set the extendedglob option to true and the second isn't really necessary, unless you don't want to remember what number equals what color in the ansi color scheme. Below, some quick examples of how you can set this up at the command line (or put in your .zshrc if you find you like it and dont' want to hassle with this constantly). Note that both options require that you load up additional modules within zsh:

host # setopt extendedglob
host # zmodload -a colors
host # zmodload -a autocomplete
host # zmodload -a complist


The -a option isn't absolutely necessary, but keeps you from having to remember to type:

zmodload zsh/complist

and

autoload colors

which, as shown, require two different forms to enter. Since they may as well both be auto-loaded, zmodload's -a option makes sense (especially since it stands for autoload... kind of).

Here are a few handy aliases for you to use that can make your zsh shell experience more enjoyable (if color does that for you ;). Of course, we didn't make all these up ourselves. Most are standard and can be found in the manpage for zshmodules. If you forget this, since zsh has 500 separate sub-manpages (j.k. ;), you can always find the name for this manpage by just invoking:

host # man zsh

and you'll see the reference to the zshmodules manpage in there.

You'll note the use of "zstyle" (which is explained more-than-fully in the same zshmodules manpage) to set up all these shell color aliases (not all of these will work on all systems) and is not restricted to the use of list-colors and the zsh completion feature. There are a lot more ways, built in to zsh, to make use of color in the shell:

zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors 'reply=( "=(#b)(*$VAR)(?)*=00=$color[green]=$color[bg-green]" )'
zstyle ':completion:*:*:*:*:hosts' list-colors '=*=30;41'
zstyle ':completion:*:*:*:*:users' list-colors '=*=$color[green]=$color[red]'
zstyle ':completion:*' list-colors ''
<-- For when you get sick and tired of all the colors and just want them to go away!

Hope you enjoy these (if you use zsh - we're not trying to get you away from ksh or bash if you like 'em like most of us do) and have fun using colorful language the next time you code ;)

Cheers,

, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Sunday, June 29, 2008

Fast Perl HTML POD Creation On Linux and Unix

Hey There,

For our "Lazy Sunday" Post this week, we're going to look at some more, hopefully, useful and easy html creation, like we've done many times in the past with posts on listing out Linux or Unix crontabs and a few others. Today's Bash shell script will produce an index.html file (and a whole lot of others) that should end up as your one-stop-shop for the documentation that came with your system's Perl distribution. It's nothing fancy (although you can make it that way if you want) but it should serve its purpose well enough right out-of-the-box.

Of course, you should edit the POD_HTML_DIR variable, at the least, so that the script can find your Perl modules and use pod2html to convert them. Other than that, running it straight-up, like so:

host # ./pods.sh

should produce output like below, in the following two pictures (click on them to make them larger if you need to :)

Perl Module Repository Index Page
Perl Module Repository Module Page

Hope you enjoy the script and it brings you some convenience! Note that I'm 100% aware that there is no error handling done for files that can't be created (i.e. when pod2html fails, a blank html page is hyperlinked to rather than no hyperlink, or html, being created), but that should make the whole experience more educational if you want to add that. You can refer to our earlier posts on Bash's implementation of errno and the Bash-specific PIPESTATUS variable to help handle those errors and do what you want with them ;)

Happy Sunday,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash

#
# pods.sh - make perdocs into one html repository
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

trap 'cd $PWD;rm -rf $POD_HTML_DIR;exit 2' 1 2 3 15

PWD=`pwd`
PERL_MANPAGE_BASE=/usr/lib/perl5/5.8
POD_HTML_DIR=podhtml
INDEX_PAGE=index.html

if [ -d $POD_HTML_DIR ]
then
rm -rf $POD_HTML_DIR
fi

mkdir podhtml
echo -n "Working on it..."

find $PERL_MANPAGE_BASE -name "*.pm"|while read line do
do
echo -n "."
output=`echo $line|sed 's/^.*\/\(.*\)$/\1/'`
pod2html $line >podhtml/${output}.html 2>/dev/null;
done

cd $POD_HTML_DIR

echo "<html><head><title>Perl Module Document Repository</title></head><body>" >$INDEX_PAGE
ls |grep html|while read line
do
echo "<a href=\"$line\">$line</a><br>" >>$INDEX_PAGE
done

echo "</body></html>" >> $INDEX_PAGE
echo "done"


, Mike

Wednesday, April 16, 2008

Perl Script To Graph Iostat Output on Unix

The ravages of iostat depicted on graph

Note: Click the picture up above to check it out in "moderately-huge" size!

Hey there,

It's been a while since we had a nice picture on top of a post, so today we're putting out a Perl script to graph "iostat" output on Unix.

If you need to graph anything else out, feel free to check our previous posts on graphing disk, paging, memory and cpu statistics on Unix, and our follow-up to account for some "sar" differences in its paging, memory and cpu output on Linux.

I'm happy to notice (yes, it took me a long long time) that we've actually gotten a write-up on those graph scripts on about.com's Perl Site and, in honor of the write up, I'm going to do two things (not including stopping the hyperlinks in this post once and for all ;) -

1. Explain the deprecated use of the GifGraph module. This is actually kind of sad. The only reason I used it is because it was already installed on the box I was working on, I had everything set up the way I liked and didn't want to mess up my little might-as-well-be-Linux installation I had on my ultra5 running Solaris 8 ;) No special reason. The new stuff is probably a lot easier to deal with and less of a headache :)

2. Write this last one using the Chart module, which hasn't been updated since January 24th, 2006 and which required me to compile all sorts of extra libraries and build and install all sorts of extra programs. And I wrote it on a newer computer and OS that I had to muck with to make it work.

After all the hassle, I'm happy to say that it actually works, but I still can't get the "gif" or "png" method's to function properly, so I'm creating a jpg (from libjpeg -- libgif and libpng just weren't good enough, I guess). I also promise (mostly to myself) to never write a script using a deprecated module on the latest version of Perl ever again... unless I have to for some inexplicable reason like... I don't know. Not a whole lot sounds that bad right now ;)

This script should work equally well on Linux or Unix, as long as you format your output to be in this order:

time rds/sec wrs/sec kbrds/sec kbwris/sec <--- The time, reads per second, writes per second, kb read per second and kb written per second. Of course you could just change the array values to suit the way your iostat output already is.

Anyhow. Enjoy, and hopefully you'll find this useful. If you haven't updated your Perl in a while (or enjoy an older version of Sun or Linux), this might be perfect just the way it is :)


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/bin/perl

#
# iograph.pl - Takes no arguments. Won't even give you the satisfaction of getting upset
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

$basedir="/home/ymdg001/z";
$source=`date +%m%d%y`;

chomp($source);
use Chart::Bars;

open(hash, "<${basedir}/host_1.txt");
@hash = <hash>;
close(hash);

$line = "host_1 ReadWrite IO stats for $source";

$iostats = Chart::Bars->new (1024, 768);

%pickle = (
title => $line,
x_label => 'Time',
y_label => 'Rates',
x_ticks => 'vertical',
legend => 'left',
brush_size => 2,
pt_size => 12,
grey_background => 'true'
);

$iostats->set (%pickle);
@labels = ('ReadsPerSec', 'WritesPerSec', 'KBReadsPerSec', 'KBWritesPerSec');
$iostats->set (legend_labels => \@labels);

$w = 0;
$y = 0;
$z = 0;
$zz = 0;
$r = 0;

foreach $cornedbeef (@hash) {
@rhino = split(" ", $cornedbeef);
chomp($rhino[0]);
$spleen[$w] = "$rhino[0]";
$w++;
chomp($rhino[1]);
$pepper[$y] = "$rhino[1]";
$y++;
chomp($rhino[2]);
$hot[$z] = "$rhino[2]";
$z++;
chomp($rhino[3]);
$sauce[$zz] = "$rhino[3]";
$zz++;
chomp($rhino[4]);
$nim[$r] = "$rhino[4]";
$r++;
}

@data = (\@spleen, \@pepper, \@hot, \@sauce, \@nim);
$iostats->jpeg ( "host_1iorw${source}.jpg" , \@data );


, Mike




Sunday, December 30, 2007

Checking For Valid Email Addresses In Your CGI Forms

This weekend's obligatory code post is a little something I slapped together during a security remediation on a completely different script (it was actually written in ksh).

I actualy enjoy working with the security department on issues like this, because it gets me thinking about the potential flaws in the stuff I write and how I can improve on what I've already done. I don't think I could work as a security specialist; at least not stuck parsing scripts all day looking for minor errors (or major ones). It's not in my blood. I need to be able to get up and wander off to a server room, or someone else's desk, and give my eyes a break.

I put in a little extra functionality, at the end, since I was writing this in Perl, to actually try to resolve the domains once they managed to get past the cursory "grammar check." Hopefully you'll be able to integrate some or all of this into any CGI forms or scripts you've written or are in the process of writing.

This is not a complete CGI script; more of a function that you can use and add-in. If you do decide to do so, and use this for any forms processing, remember to add a section to strip out illegal characters. This script was written with very exacting specifications from the requestor. Check out the last part of this earlier post which goes into more detail about taking care of than end of business.

I think I covered all the bases. At this point, I probably still wouldn't want to hand this over to the security department ;) It has its flaws, for sure, but I like to put out works-in-progress if only to help kick-start some creative thinking on my (and/or your) part. In the end, it makes for better quality of work.

Best Wishes,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/local/bin/perl

#
# 2007 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

use Net::DNS;

$res = new Net::DNS::Resolver;

select(STDERR);

while (<STDIN>) {
chomp; # In certain cases you may need to change this line to chop;
$_ =~ s/\r$//;
$_ =~ s/ *$//;
($cid,$email,$rest) = split(/\|/,$_,3);
$cid =~ s/ //g;
if ($email !~ /\@/) {
print "No \@ symbol in address: $cid, $email\n";
} else {
($name,$dom) = split(/\@/,$email,2);
if ($name eq "") {
print "No name on left of @ symbol: $cid, $email\n";
} elsif ($dom eq "") {
print "No domain on right of @ symbol: $cid, $email\n";
} elsif ($dom =~ /\@/) {
print "Too many @ symbols: $cid, $email\n";
} elsif ($email =~ / /) {
print "Space character in email address: $cid, $email\n";
} elsif ($dom =~ /[,]/) {
print "Comma right of @ symbol: $cid, $email\n";
} else {
$dom =~ tr/A-Z/a-z/;
$dom =~ s/\.$//;
$good = 0;
if ($dcache{$dom} != 1) {
($nm,$aliases,$addrtype,$length,@addrs) = gethostbyname("$dom");
if ($nm ne "") {
$good = 1;
} else {
@mx = mx($res, $dom);
if ($#mx >= 0) {
$good = 1;
} else {
print "Bad domin right side of @ symbol: $cid, $email\n";
}
}
if ($good) {
$dcache{$dom} = 1;
} else {
next;
}
}
print STDOUT "$cid\|$name\@$dom\|$rest\n";
}
}
}


, Mike