home

Quickies, by Andrea Olivato

text

Update DynDns host with current IP with Perl

Updates any DynDns host with current IP address. Useful if put in crontab and executed every 15mins.

#!/usr/bin/perl

# Load Needed Modules
use strict;
use XML::Simple;
use LWP::UserAgent;

# Get login data
my $username="DynDns_user";
my $password="DynDns_user";
my $hostname="yourhost.example.com";

# Create a new useragent
my $ua = new LWP::UserAgent;
$ua->agent('Mozilla/5.0 (X11; ; Linux i686; it; rv:1.9.1) Gecko/20090701');

# Create and perform a request to get current IP address
my $req = new HTTP::Request GET => "http://ip-address.domaintools.com/myip.xml";
my $content = $ua->request($req)->content();

# Create XML class and parse results
my $xml = new XML::Simple (KeyAttr=>[]);
my $data = $xml->XMLin($content);

# Get my current ip address from xml tree
my $ip = $data->{ip_address};
my $old;

# Open script log and read old ip address
open (MYFILE, '/tmp/ip-ch.log');
$old = ;
close (MYFILE);

# Open script log and write new ip address
open (MYFILE, '>/tmp/ip-ch.log');
print MYFILE $ip;
close (MYFILE);

# If new ip is different from previous one
if ($old != $ip) {

        # Create a new useragent
        my $ua = new LWP::UserAgent;
        $ua->agent('Mozilla/5.0 (X11; ; Linux i686; it; rv:1.9.1) Gecko/20090701');

        # Create and perform a request to update hostname associated ip
        my $req = new HTTP::Request GET => 'http://'.$username.':'.$password.'@members.dyndns.org/nic/update?hostname='.$hostname.'&myip='.$ip.'&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG';
        $ua->request($req)->content();
}
 

2 years ago

July 13, 2009
Comments (View)
text

Urlencode a string in #perl

Quick script able to urlencode a string without the needing of importing modules

$mystring = "String to encode";
$mystring =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
print $mystring;
# Returns String%20to%20encode

2 years ago

June 27, 2009
Comments (View)
text

Check if variable is an array in #perl

This is quite useful when parsing xml code and you don’t know if an object contains an array of tags or a single value.

eval {
	$#{$isthisanarray}
};
if ($@){
	print "Nuuu! It's not";
} else {
	print "Yes it is";
}

2 years ago

June 25, 2009
Comments (View)
text

Perl and Sqlite3 quick start example guide

1. Set up your sqlite database file

sqlite3 demo.db
SQLite version 3.6.13
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
create table datas (
        id integer,
        name text,
        date text
);

2. Execute simple operations with perl

#!/usr/bin/perl

# Import needed Modules
use DBI;
use strict;

# Connection to DB file created before
my $dbh = DBI->connect("dbi:SQLite:dbname=demo.db","","",$dbargs);

# Two simple insert Queries 
# I use ->do as I don't need to do anything with returned obj
$dbh->do("insert into `datas` (id, name,date) values (1,'something','1986-06-07')");
$dbh->do("insert into `datas` (id, name,date) values (2,'otherthing','2009-03-02')");

# A simple select statement
my $query = "SELECT * FROM datas ORDER BY id DESC";
my $query_handle = $dbh->prepare($query);
$query_handle->execute();

# Instead of simply fetching rows I prefer, for each one,
# to assign values to predefinite values.
# Bind columns is a wonderful function!
$query_handle->bind_columns(\my($id, $name, $date));
while($query_handle->fetch()) {
   print "$id, $name, $date \n"
}

# Close connection
$query_handle->finish;
undef($dbh);

2 years ago

June 15, 2009
Comments (View)
text

Encrypt & Decrypt strings in #Perl using Tea

Simple two way encrypt/decrypt script in Perl using Tea Algorithm. Light and fast as hell

#!/usr/bin/perl

use Crypt::Tea;
my $string = 'tobecrypted';
my $key = 'somekey';

# Print Original String
print $string."\n";

# Encrypt the string using my key
$user = encrypt($string,$key);

# Print Encrypted String
print $string."\n";

# Decrypt the string
$user = decrypt($string,$key);

# Print back Original String
print $string."\n";

2 years ago

June 5, 2009
Comments (View)
text

How to switch 2 $vars value in #Perl

How to simply switch two variables values in Perl.

#!/usr/bin/perl

# Assigning 2 vars
my $some = "some";
my $thing = "thing";

# Switching them
($some, $thing) = ($thing, $some);

# See what happened
print '$some is now "'.$some.'"';
print "\n";
print '$thing is now "'.$thing.'"';

2 years ago

May 25, 2009
Comments (View)
text

Dump variables in Perl

How to show content of unknown objects in perl using Dumper


use Data::Dumper;
print Dumper $my_unknown_object;

2 years ago

May 20, 2009
Comments (View)
text

Find & replace multiple files with Perl

How to search and replace a string within some files with perl, launched from a shell.

# I'm looking for 'some' in all my .js files
# and I want to replace it with 'foo'
perl -pi -w -e 's/some/foo/g;' *.js

2 years ago

May 20, 2009
Comments (View)
text

Creating a Twitter @reply from Perl

How to update your twitter account using Perl, specifying an ‘in reply to’ parameter.

#! /usr/bin/perl

# Configuration
my $TW_USER = "USERNAME";
my $TW_PASS = "PASSWORD";
my $INREPLYTO = "POST_ID"; # Retrieve this from Twitter
my $MESSAGE = "SOMETHING TO SAY";

# Import library and get an User Agent
use LWP::UserAgent;
my $ua = new LWP::UserAgent;

# Set up the request
my $req = new HTTP::Request POST => 'http://twitter.com/statuses/update.xml';

# Authenticating and setting up post data
$req->authorization_basic($TW_USER, $TW_PASS);
$req->content("status=$MESSAGE&in_reply_to_status_id=$INREPLYTO");

# Send the request
my $res = $ua->request($req);

# print $res->status_line; # Uncomment this for debugging

2 years ago

May 18, 2009
Comments (View)