Perl

Perl - notes, samples and how to's,

Perl Documentation

http://perldoc.mochabomb.com/

Perl Handy One Liners

This is a list of handy one liners - adding onto the ones out there...

grep for text in a multiline blob - this redefines the newline as a single space char - case insensitive (i), and over line breaks (s)

cat somefile | perl -lne '$/ = " "; print if /text_to_match/is;'

Count lines ala "wc -l" - uses the diamond operator

user@vps2 ~/ $  ps -ef | perl -lne 'while (<>) { ++$i; } print $i;'
93
user@vps2 ~/ $  ps -ef | wc -l
93

Find IP's that failed 25 times or more and send to /etc/hosts.deny - 25 is set at the end of the statement:

root@host ~ #  egrep -e 'IN Blocked' /var/log/messages | tail -5000 | perl -lne '/(\d+\.\d+\.\d+\.\d+)/; print $1;' | sort | uniq -c | sort -n | perl -lne '/(\d+)\s+(\d+\.\d+\.\d+\.\d+)/; $d=`date`; chomp $d; print "# Too many failures $d\nALL: $2" if ($1 >= 25);' >> /etc/hosts.deny

openDNS Update script

OpenDNS Updater script - there is not a linux updater client like there is for windows, but the update is simple. Here is the detail at the OpenDNS forums here: Linux Update Client.
Here I added some code to check if the IP is different, attempt to update, then write result to a log file. There are some extra print statements that cron will generate emails, can comment them out ....

#!/usr/bin/perl

# Gregg Lain May 5th, 2009
# gregg@mochabomb.com
# Update OpenDNS when IP changes

$updateOpenDNS = 0;
$updateresult = 0;
$writeLogEveryXHours = 2;  # integer, 1 to 23

$DATE = (localtime);
$MIN = ((localtime(time))[1]); 
$HOUR = ((localtime(time))[2]);

$LOGFILE="/home/user/working/IPupdater.log";
open LOG, ">>$LOGFILE";

$OLDIPFILE="/home/user/working/oldIP.txt";
open OLDIP, "<$OLDIPFILE";

$TMPIPFILE="/home/user/working/tmpIP.txt";
$ipcheck = `wget http://mochabomb.com/publicscripts/ip.php -o /dev/null -O $TMPIPFILE`;
open TMPIP, "<$TMPIPFILE";

# OpenDNS Account Info
$USERNAME='USERNAME';
$PASSWD='PASSWD';

$oldip = join '', ;
chomp $oldip;

$tmpip = join '', ;
chomp $tmpip;
$tmpip =~ s/Remote IP: //;

# Attempt to update OpenDNS..
if ( $tmpip ne $oldip ) {
     $updateOpenDNS = 1;
     $newip = $tmpip;   # to make thing simple..
     $updateresult = `wget -O - -q --http-user=$USERNAME --http-passwd=$PASSWD https://updates.opendns.com/nic/update`;
     $updateresult =~ s/\\n//g;
     $updatewasgood = 1 if ( $updateresult =~ good );  # Flag if result was good or not..
}


# Now that we have results, log it some way or another.
# We had a good update - save this IP, log result...
if (( $updatewasgood == 1 ) && ( $updateOpenDNS == 1 )) {  
     close OLDIP;    # need to close and reopen, else file is wiped out...
     open OLDIP, ">$OLDIPFILE";
     print OLDIP $newip;              #update the file with new IP
     print "OpenDNS Update Result: $result\n";
     print "Old IP was $oldip; was updated to $newip\n";
     print LOG "$DATE OpenDNS updated. old IP: $oldip, new IP $newip OpenDNS said: $updateresult\n";  # update log file
}

# We had bad update - log result...
if (( $updatewasgood == 0 ) && ( $updateOpenDNS == 1 )) {
     close OLDIP;
     print "ERROR: OpenDNS Update Result: $result\n";
     print "Old IP was $oldip; was NOT updated to $newip\n";
     print LOG "$DATE ERROR: OpenDNS update FAILED. old IP: $oldip, new IP $newip\n";  # update log file
}


if ( $updateOpenDNS == 0 ) { 
     print "No update made\n"; 
}

# Log every X hours - unless we ran a update.. just so we see its working...
$leftover = $HOUR % $writeLogEveryXHours;
if (( $leftover == 0 ) && ( $updateOpenDNS == 0 ) && ( $MIN == 15 )) {
     print LOG "$DATE OpenDNS updater running.. Updated IP: $oldip, Current IP $tmpip\n";  # update log file
}
      

# Housecleaning...
close TMPIP;
close OLDIP;
close LOG;


shift, split

#!/usr/local/bin/perl -w

$uptime = `uptime`;
$uptime =~ s/[ \n\t\f\s,]+/ /g;
$uptime =~ s/^[ \n\t\f\s]//;
chomp($uptime);

@array = split(/ /, $uptime);
while (@array) {
$thing = shift(@array);
print "$thing\n";
}