hehehe

Share Button

A little bit of fun for a Thursday :)

[flashvideo width=”320″ height=”240″ filename=”http://www.archive.org/download/nc101_hackers/nc101_hackers.flv” returnpage=”http://froebe.net/blog/2007/08/30/a-little-bit-of-fun-for-a-thursday/” /]

This is the very first episode of the Net Cafe series. It was shot on location at a cybercafe in San Francisco called CoffeeNet. It looks at the hacker culture and their influence on the early growth of the internet. Guests include Dan Farmer, author of SATAN and COPS; Eliaz Levi (aka Aleph 1), webmaster of underground.org and Bugtraq; also “Omega” and “White Knight” from Cult of the Dead Cow. Originally broadcast in 1996.

This item is part of the collection: Net Cafe

From: The Internet Archive

Share Button

Tying it all together: Extracting SQL Batches, Highlighting Code with Kate, and Sending an email with attachments

In How to read batches of SQL from a file in Perl, we learned how to read a SQL file and extract individual batches and created the Perl module dbS::Sybase::Parse::SQL_File. In How to colorize your code using Perl and Syntax::Highlight::Engine::Kate, we learned how to make HTML highlighted arbitrary code and created Perl module dbS::misc. Finally, in Sending email with attachments from Perl – the easy way, we learned how to send email with attachments and created Perl module dbS::EMail.

Now, we are going to tie them all together so we can send ourselves colorized SQL code and a screenshot of an application going screwy!

#!/usr/bin/perl

use strict;
use warnings;

use File::Basename;

use dbS::EMail;
use dbS::misc;
use dbS::Sybase::Parse::SQL_File;

our $PROC = basename($0);

$|++;


my $message = "";

if ( my $sql_batch = dbS::Sybase::Parse::SQL_File::get_batch("/dbms/sybase/ASE-15_0/scripts/installmontables", undef, 1) ) {
   while (my $query = $sql_batch->next ) {
      $message .= $query;
      $message .= "go\\n\\n";
   }
} else {
   warn("unable to open the SQL file\\n");
}


if ( dbS::EMail::simple_send(
        'jason@froebe.net.nospam',
        $PROC . ': test HTML email',
        any_to_html('SQL', $message),
        undef,
        { type => 'html', 'files' => "/tmp/AppBug.jpg" }
   )) {
   print "SUCCESS!  Email sent\\n";
} else {
   print "ERROR:  Email send failed\\n";
   printf "ERROR:  Email Transport: %s\\n", $dbS::EMail::error if $dbS::EMail::error;
   printf "ERROR:  Email Transport Log: %s\\n", $dbS::EMail::log if $dbS::EMail::log;
}
Share Button

Sending email with attachments from Perl – the easy way

Since I’ve already had my Linux box set up as an SMTP server, I can just use the built in sendmail capabilities of Mail::Sendmail. I could easily have it use an external SMTP server by specifying it in the $mail{smtp} hash variable.

In this example, I’m going to send a hard coded HTML string to my dbS::EMail along with a jpeg of the Enterprise. Why wrap Mail::Sendmail in a perl module and not use it directly? I wish to make it as simple as possible for myself by hiding the complexities.

#!/usr/bin/perl

use strict;
use warnings;

use dbS::EMail;

my $message < <EOF

This past Sunday my 30GB iPod photo finally died.  It lasted just over three years and it was a true veteran.  It was dropped, kicked, washed (accidentally), and chewed on by Tonks.  Sunday afternoon after returning from the <a href="http://froebe.net/blog/2007/08/26/more-photos-of-our-house/">build site of the new house, I went back out to the car and dropped the iPod three times in 60 seconds.  Low and behind it finally died.  I took it apart, reseated everything but when the iPod started, I heard the click click of a dead hard drive.  After an hour of trying to coax it back to life, <a href="http://www.froebe-fibers.com" onclick="return alinks_click(this);" class="alinks_links" title="Rebecca Froebe" style="background: transparent url('http://froebe.net/blog/wp-content/plugins/alinks/images/external.png') no-repeat scroll right center; padding-right: 13px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" rel="external">my wife</a> offered to buy a new 30GB Video iPod for my birthday.

I loaded it up with podcasts, music and video.  What I’ve noticed is that when I go iPod -> Music -> Podcasts -> <a href="http://chris.pirillo.com/media/">The Chris Pirillo Show</a>, I saw a normal podcast and no video.  After a few seconds of thinking I messed something up, I didn’t, I tried iPod -> Video Podcasts -> <a href="http://chris.pirillo.com/media/">The Chris Pirillo Show</a> and video was found <img src="http://froebe.net/blog/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" />   I would have thought that it wouldn’t make any difference if I played the video podcast from “Podcasts” or “Video Podcasts”, but apparently it does.

EOF

if ( dbS::EMail::simple_send('jason@froebe.net.nospam', $PROC . ': test email with attached jpg' , $message, undef, { type => 'text', 'files' => "/home/jfroebe/enterprise.jpg" } ) ) {
   print "SUCCESS!  Email sent\\n";
} else {
   print "ERROR:  Email send failed\\n";
   printf "ERROR:  Email Transport: %s\\n", $dbS::EMail::error if $dbS::EMail::error;
   printf "ERROR:  Email Transport Log: %s\\n", $dbS::EMail::log if $dbS::EMail::log;
}

exit;

The following is the dbS::EMail Perl Module. Note that when we are sending regular text, we are still sending MIME encoded. That’s not a bad thing but could annoy some people on antique email clients. Attachments are handled by sending a filename path to the module which will attempt to determine what type of file it is by using File::MMagic and then encode the file using MIME::Base64. All subroutines starting with the underscore “_” are considered internal subroutines and shouldn’t be accessed outside of the module.

package dbS::EMail;

use warnings;
use strict;

use File::Basename;
use File::MMagic;
use Mail::Sendmail;
use MIME::Base64;
use MIME::QuotedPrint;

our $PROC = basename($0);

BEGIN {
    use Exporter ();

    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
    $VERSION = 1.0.0;

    @ISA = qw(Exporter);
    @EXPORT_OK = qw(&amp;simple_send $error $log);
}

#=========================
our $error;
our $log;
our $boundary = "====" . time() . "====";
our $start_attachment = "--" . $boundary;
our $end_ALL_attachments = "--" . $boundary . '--';
#=========================
sub _attach_file {
   my ($mail, $filename) = @_;

   my $FH;
   my $mm = new File::MMagic;
   my $content_type = $mm->checktype_filename($filename);

   if ( open($FH, "< ", $filename) ) {
      my $tmp_file = basename($filename);

      binmode $FH;
      undef $/;

      my $mime_header =
         $start_attachment
         . "\\nContent-Type: $content_type; name=\\"$tmp_file\\"\\n"
         . "Content-Transfer-Encoding: base64\\n"
         . "Content-Disposition: attachment; filename=\\"$tmp_file\\"\\n";

      $mail->{body} .=
         $mime_header
         . encode_base64(< $FH>)
         . "\\n";
   } else {
      warn("Unable to attach file: $filename\\n");
   }

   return $mail;
}

sub _email_content_type {
   my $mail = shift;

   $mail->{'content-type'} = "multipart/mixed; boundary=\\"$boundary\\"";

   return %$mail;
}

sub _email_type {
   my ($parms, $mail) = @_;

   if ($parms->{'options'}->{'type'} &amp;&amp; $parms->{'options'}->{'type'} =~ m/^html$/i ) {
      my $mime_header =
         $start_attachment
         . "\\nContent-Type: text/html; charset=\\"iso-8859-1\\"\\n"
         . "Content-Transfer-Encoding: quoted-printable\\n";

      $mail->{'body'} =
         $mime_header
         . "\\n"
         . encode_qp($parms->{Body})
         . "\\n";
   } else {
      my $mime_header =
         $start_attachment
         . "\\nContent-Type: text/plain; charset=\\"iso-8859-1\\"\\n"
         . "Content-Transfer-Encoding: quoted-printable\\n";

      $mail->{'body'} =
         $mime_header
         . encode_qp($parms->{Body})
         . "\\n";
   }

   return %$mail;
}

sub _email_file {
   my ($parms, $mail) = @_;

   if ($parms->{'options'}->{'files'}) {
      if ( ref($parms->{'options'}->{'files'}) eq 'ARRAY') {
         # We need to attach multiple files
         foreach my $filename ( $parms->{'options'}->{'files'} ) {
            $mail = _attach_file($mail, $filename);
         }
      } else {
         # We need to attach a single file
         $mail = _attach_file($mail, $parms->{'options'}->{'files'});
      }
   }

   return %$mail;
}

sub _send_email {
   my $parms = shift;

   if ($parms &amp;&amp; $parms->{To} &amp;&amp; $parms->{Subject} &amp;&amp; $parms->{Body} ) {
      $parms->{From} = 'sis-dba@example.com' unless $parms->{From};

      my %mail = (
         To      => $parms->{To},
         From   => $parms->{From},
         Subject   => $parms->{Subject}
      );

      %mail = _email_content_type(\\%mail);
      %mail = _email_type($parms, \\%mail);
      %mail = _email_file($parms, \\%mail) if $parms->{'options'}->{'files'};

      $mail{'body'} .= $end_ALL_attachments;

      if ( sendmail(%mail) ) {
         return 1;
      } else {
         $error = $Mail::Sendmail::error;
         $log = $Mail::Sendmail::log;
      }
   } else {
      warn ("ERROR:  dbS::EMail\\n");
      warn ("\\t'To' parameter is required\\n") unless $parms->{To};
      warn ("\\t'Subject' parameter is required\\n") unless $parms->{Subject};
      warn ("\\t'Body' parameter is required\\n") unless $parms->{Body};
   }
}

sub simple_send {
   my ($To, $Subject, $Body, $From, $options) = @_;

   return _send_email( {
      To      => $To,
      Subject   => $Subject,
      From   => $From,
      Body   => $Body,
      options   => $options
   } );
}

1;
Share Button

How to colorize your code using Perl and Syntax::Highlight::Engine::Kate

Yesterday I was scratching my head on how to send an email to myself when an application failed or a certain error occurred. You see, I wanted to be able to read an email on my crackberry (Blackberry) that showed that a problem occurred and more importantly, I wanted the error messages and the snippet of code where the problem occurred.

The first part of sending the email was very simple by using Mail::Sendmail, but I wanted to extend it a bit by sending highlighted code. The problem is that the documentation of Syntax::Highlight::Engine::Kate isn’t too clear on how to use it.

The Syntax::Highlight::Engine::Kate is surprisingly easy to use once you understand that Kate just parses whatever string you give it for tokens and lets you handle how you want each type of token highlighted. In truth, you could easily forsake HTML and have it spit out XML or something else if you so desired.

package dbS::misc;
use strict;
use warnings;

our $PROC = basename($0);

BEGIN {
    use Exporter ();

    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
    $VERSION = 1.0.0;

    @ISA = qw(Exporter);
    @EXPORT = qw(&any_to_html);
}

sub any_to_html {
    my ($type, $content) = @_;

    require Syntax::Highlight::Engine::Kate;

    my $hl = new Syntax::Highlight::Engine::Kate(
        language => $type,
        substitutions => {
            "< " => "< ",
            ">" => ">",
            "&" => "&",
            " " => " ",
            "\\t" => "   ",
            "\\n" => "\\n",
        },
        format_table => {
            Alert => ["<span style="color: #0000ff;">", "</span>"],
            BaseN => ["<span style="color: #000000;">", "</span>"],
            BString => ["<span style="color: #00c9ff;">", "</span>"],
            Char => ["<span style="color: #00ffff;">", "</span>"],
            Comment => ["<span style="color: #007f7f;"><em>", "</em></span>"],
            DataType => ["<span style="color: #0000ff;">", "</span>"],
            DecVal => ["<span style="color: #00007f;">", "</span>"],
            Error => ["<span style="color: #00ff00;"><strong><em>", "</em></strong></span>"],
            Float => ["<span style="color: #00007f;">", "</span>"],
            Function => ["<span style="color: #000000;">", "</span>"],
            IString => ["<span style="color: #00ff00;">", ""],
            Keyword => ["</span><span style="color: #000e00;"><strong>", "</strong></span>"],
            Normal => ["", ""],
            Operator => ["<span style="color: #00ff00;">", "</span>"],
            Others => ["<span style="color: #00b060;">", "</span>"],
            RegionMarker => ["<span style="color: #0096ff;"><em>", "</em></span>"],
            Reserved => ["<span style="color: #009bff;"><strong>", "</strong></span>"],
            String => ["<span style="color: #00ff00;">", "</span>"],
            Variable => ["<span style="color: #0000ff;"><strong>", "</strong></span>"],
            Warning => ["<span style="color: #0000ff;"><strong><em>", "</em></strong></span>"],
        },
    );

    return $hl->highlightText($content);
}

1;

I wrote a wrapper subroutine for Syntax::Highlight::Engine::Kate so I can just call any_to_html(‘parsing template’, $string) and it will return HTML code.

#!/usr/bin/perl

use strict;
use warnings;

use File::Basename;
use Syntax::Highlight::Engine::Kate;

use dbS::misc;
use dbS::Sybase::Parse::SQL_File;

our $PROC = basename($0);

$|++;

my $message = "";

if ( my $sql_batch = dbS::Sybase::Parse::SQL_File::get_batch("/dbms/sybase/ASE-15_0/scripts/installmontables", undef, 1) ) {
    while (my $query = $sql_batch->next ) {
        $message .= $query;
        $message .= "go\\n\\n";
    }
} else {
    warn("unable to open the SQL file\\n");
}

print $message

The language option (a parsing template) and format_table option (token into html code) are required but the substitutions option is optional but I strongly recommend using it else the output will look odd.

The following is the output of $SYBASE/$SYBASE_ASE/scripts/installmontables using our little application. Note that we are reading the batches one by one (just because we can) even though it isn’t necessary for this example.

Continue reading “How to colorize your code using Perl and Syntax::Highlight::Engine::Kate”

Share Button

ASE Shared Disk Cluster at TechWave

Somehow I missed posting about David Wein’s ASE Shared Disk Cluster demo that he performed at TechWave this year! Sorry Dave! I honestly thought I posted about it.

SPECIAL PLENARY SESSION — Practical Innovations in Data Management & Data Integration: Helping IT Teams Sleep at Night While CEOs Scale the Business
Tuesday, 1-2:15 p.m. PDT. View the recorded webcast

Sybase ASE Shared Disk Cluster Demonstration
The ASE Shared Disk Cluster demo takes place at 26:51 in the video or can choose “ASE Product Roadmap Overview” in the left corner of the screen. David’s demonstration begins at 43:05 (look for “David Wein – Demo”).

While I’m hesitant about recommending that the ASE Shared Disk Cluster be placed in a production environment for the first release, it appears to be technically superior to Oracle’s current RAC system. 🙂

Share Button

Revision Control Systems… avoid them or use them?

We use subversion a great deal but I don’t understand why there are people that refuse to use it.  The TechRepublic article, Avoid these six common development mistakes, sheds a bit of light:

….

4. Failing to see the value in a version control system

Most developers are all-too-familiar with this scenario: You roll out a new version of a critical Web application, and your cell phone rings constantly the next morning with cries of site problems.

As long as you’re willing to admit that you may have made a mistake or two, a version control system can be your best friend in this situation. By using version control systems, such as CVS, IBM Rational ClearCase, or Microsoft Visual SourceSafe, you can easily revert to the previous version of the application and move it to production. This gives you time to locate the problems within the development environment while the production site is readily available. This beats frantically searching through production code to locate the problem.

In addition, a source control system allows concurrent development so different team members may work with the same code. When the developers check their changes into the system, the changes are merged together.

One reason why some developers don’t like version control systems is because it adds a layer to the development process, which means the submission and retrieval of code to and from the source control system can be slow. This extra waiting time can tax the patience of some developers.
Read more…

Hmmm.  It might be more of a matter of convenience for some developers not to use or learn how to use a revision control system.  Looks like I’ll have to write a few wrappers to simplify it.

Share Button

Why would you want to use Perl/Java/etc instead of isql?

I’m not trying to raise any ire in anyone, so please bear with me….

The issue of using isql or some other tool (such as Perl or Java) was brought up in the sybase-l mailing list

A benefit of handling each batch separate entity instead of all the batches as a whole in isql, we have the ability to isolate each batch. That means, the we can place logic around each batch or run them in parallel or a number of other reasons. This doesn’t actually require Perl and you could definitely do this in Java, but you would probably want to shy away from using isql for executing each batch. You may have hundreds or thousands of batches and starting isql for each batch would be very time consuming.

I have no problems with using awk/sed/whatever for maintenance scripts. I do have a problem of relying too heavily on the isql program from Sybase or Microsoft. Why? isql does not handle errors very well and often can fail due to bugs in the isql/openclient or network issues. sqsh is a bit better but I wouldn’t trust either in a production environment.

Yeah, I know lots of people run isql/sqsh in production environments but when isql fails, are you able to identify the problem and workaround it without having your script fail?

For example, if you were using isql to import a SQL file of say 10,000 batches (table ddls, etc), would you really want to restart the whole job if the 998th batch failed because of a deadlock?  Wouldn’t you rather just retry the 998th batch?

In many cases, you might not be able to restart the whole job, so you would have to create a new SQL file copy/paste it from the original.

Sure, you could pipe the output of isql/sqsh and parse it but they don’t show all the errors and a general disconnect error message doesn’t really help.

I’m lazy by nature, so doing something manually when it can be scripted to be have more error handling is pretty much a good thing.

Share Button

Modifying sp__revindex so it doesn’t print the “go”

I’m in the process of creating a suite of perl modules that use Ed Barlow‘s sp__rev* procedures. I’ve noticed that the @dont_format parameter isn’t being used in sp__revindex so I made use of it to drop the “go” from being included in the result set.

The reason why I don’t want the “go” is because I want just the ddl of the index and not a SQL script. The default behavior is to show the “go”.

Continue reading “Modifying sp__revindex so it doesn’t print the “go””

Share Button

Science Toys You Can Make With Your Kids :)

Make toys at home with common household materials, often in only a few minutes, that demonstrate fascinating scientific principles.  SciToys.com


Hehe….   mad scientist munchkin currently in pre-training!

Share Button