How to debug a perl script

Enter the debugger by:

perl -d scheduledscrape.pl --path="game of thrones" --size=anysize

The script and arguments are invoked just as normal, but preceded by perl -d.
To keep continuing, one line at a time: n
Continue until return from the block: r

When a subroutine is shown, to go into it: Step into: s
When a subroutine is shown, to pass over it: n
When a subroutine is entered, to continue until return value: r

To get the value of a variable (or expression): p expr
Eg:

p $current

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Remove password from protected pdf file

So, my situation was that I wanted to check whether the credit card bill amount I was receiving was valid. I wanted to calculated for myself. The problem was that the pdf credit card statement being sent out by my bank was password and signature protected. It wouldnt allow copying text from it, which is rather weird. I have a genuine need to archive the pdf without password because after a couple of months, who’s going to remember what password it had, especially if you have a couple of different cards, each with its own password schema?

So the first step was to remove the password from it:
Install and run qpdf:

apt-get install qpdf
qpdf -decrypt --password=mypassword /mnt/c/temp/899XXXXXXXXXX32_11-02-2017.PDF /mnt/c/temp/1.pdf

This removes the password. The signature is still intact, but hooray, the pdf now allows copying text from it. I paste the text into my favorite text editor, and use a perl script to calculate for me.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Generating a list of virtualmin public folders for backup

The following perl script creates this at /root/bash-advanced-scripts/backupqueue.lst

#!/usr/bin/perl
my $filename = '/root/bash-advanced-scripts/backupqueue.lst';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
my @files = </etc/webmin/virtual-server/domains/*>;
foreach my $file (@files) {
    open   (FILE, "$file");
    while(my $line= <FILE> ){
        if ($line =~ /(^public_html_path=)(.*)/) {
            print "$2\n";
            print $fh "$2\n";
        }
    }
    close FILE;
}
close $fh;
print "Wrote to  $filename.\n";

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Wrap output of ps command

Consider a normal ps command:

#ps auxw
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
joel     21383 46.5  1.5  84972 31956 ?        S    23:13   1:13 /usr/bin/perl /home/joel/domains/epi.jo.in/public_html/scheduledscrape.pl --path=http://www.t
www-data 21384  0.0  0.4 342828  9216 ?        S    23:13   0:00 /usr/sbin/apache2 -k start

To wrap output to get full lines, use ww option:

#ps auxww
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  29848  3300 ?        Ss   Feb11  13:22 /sbin/init
joel     21383 45.8  1.5  84972 32012 ?        S    23:13   1:15 /usr/bin/perl /home/joel/domains/epi.jo.in/public_html/scheduledscrape.pl --path=http://www.tv.com/shows/doctor-who-2005/episodes/ --size=anysize
www-data 21384  0.0  0.4 342828  9216 ?        S    23:13   0:00 /usr/sbin/apache2 -k start
www-data 21385  0.0  0.4 342800  9132 ?        S    23:13 

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Creating configuration files with perl

Code:

#!/usr/bin/perl
use Config::Simple;
$cfg = new Config::Simple(syntax=>'ini');
$cfg->param('auth.user', 'sherzodR');
$cfg->param('auth.pass', 'mypass');
$cfg->write("new.cfg");

File structure:

#cat new.cfg
; Config::Simple 4.58
; Tue Jul  7 11:20:58 2015

[auth]
pass=mypass
user=sherzodR

Reading the values:

#!/usr/bin/perl
use Config::Simple;
$cfg = new Config::Simple();
$cfg->read('new.cfg');
$user = $cfg->param("auth.user");
$pass=$cfg->param("auth.pass");
printf ("Username:%s  Password:%s\n", $user,$pass);

On running:

 #./testconfig.pl
Username:sherzodR  Password:mypass

Reading and then updating the values:

use Config::Simple;
$cfg = new Config::Simple();
$cfg->read('new.cfg');
$user = $cfg->param("auth.user");
$pass=$cfg->param("auth.pass");
printf ("Username:%s  Password:%s\n", $user,$pass);


$cfg->param('auth.user', 'newuser');
$cfg->write("new.cfg");

On running:

#./testconfig.pl
Username:sherzodR  Password:mypass
[[email protected]] ~/scripts #./testconfig.pl
Username:newuser  Password:mypass

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Block Cloudflare IPs

I noticed that every single WordPress bruteforce attempt on the server came from a Cloudflare IP. So I had to write this:

#!/usr/bin/perl
# Program to download cloudflare ip ranges and block them
use strict;
use warnings;
use LWP::Simple;
my $url='https://www.cloudflare.com/ips-v4';
my $file = '/tmp/cloudflareip.txt';
getstore($url, $file);
open my $info, $file or die "Could not open $file: $!";
while( my $line = <$info>)  {   
    chomp $line; 
    my $cmd='iptables -A INPUT -s '.$line.' -j DROP';
    print $cmd."\n";
    `$cmd`;
}
close $info;
print "Listing current iptables rules:\n";
my $cmd='iptables -L -n';
`$cmd`;

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Associate .pl files with Perl

Scenario: You would like to double click and execute .pl perl script files.

Run the following in Windows command prompt:

assoc .pl=PerlScript
ftype PerlScript=C:\Perl64\bin\perl.exe "%1" %*
set PATHEXT=%PATHEXT%;.PL

To allow IO redirection to work properly, you need to import the following Registry key:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"InheritConsoleHandles"=dword:00000001

Ref: http://stackoverflow.com/questions/4727480/how-do-i-make-my-perl-scripts-act-like-normal-programs-on-windows
This also talks about how to add a drop handler.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Download all subtitles for media files in a directory

This script is written in perl, and is cross platform compatible, i.e. works in Linux, Windows or any other OS that can run perl scripts.

Install ActiveState perl
Download the script from my Github: https://github.com/droidzone/subtitlegetter
Copy the context menu handler .cmd file to the approproiate location
Import the .reg file after modifying any paths in it.

Now you can right click any folder in explorer and download automatically subtitles for all files in the directory.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

WordPress plugin updater script released

I have released the latest version of WordPress plugin updater script on Github
Wiki: https://github.com/droidzone/wordpress_plugin_updater/wiki
Code: https://github.com/droidzone/wordpress_plugin_updater

WordPress plugin updater is a useful perl script that when run on a folder containing your wordpress plugins, checks each plugin for newer versions in the WordPress repository, and updates each plugin to the latest version.

It accepts as argument a single folder or multiple folders of wordpress installations. This saves you the trouble of visiting your dashboard to update plugins, especially if you manage multiple sites, and have hundreds of plugins. Having older versions of plugins is a security hole, and hence this script helps secure your server.

You can easily run the script as a cron job at hourly-daily intervals and thus schedule updating your plugins.

Installation

Usage


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Perl simple file writing script

Try the following:

open FIL,">temp";
print FIL "Hola";
print FIL "Nada";
close FIL; 

Note:
No commas are allowed after the file handle, before the string to be written. Line breaks are not inserted unless specified.

open FIL,">temp";
print FIL "Hola";
print FIL "Nada";
close FIL; 

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.