User:H/Range block finder.pl

This script will search all the ranges that could effect an IP looking for a block, and will report the details of that block when it finds it. This script requires perl to run and you will most likely need to install Net::Netmask.

use strict; use LWP::UserAgent; use Net::Netmask; use URI::Escape;

my $ip = ''; my $ua = LWP::UserAgent->new('agent' => 'Range block finder .0001b');

my @possible_masks = ( '255.255.255.0/24', '255.255.0.0/16', '255.0.0.0/8', '255.255.255.254/31', '255.255.255.252/30', '255.255.255.248/29', '255.255.255.240/28', '255.255.255.224/27', '255.255.255.192/26', '255.255.255.128/25', '255.255.254.0/23', '255.255.252.0/22', '255.255.248.0/21', '255.255.240.0/20', '255.255.224.0/19', '255.255.192.0/18', '255.255.128.0/17', '255.254.0.0/15', '255.252.0.0/14', '255.248.0.0/13', '255.240.0.0/12', '255.224.0.0/11', '255.192.0.0/10', '255.128.0.0/9', '254.0.0.0/7', '252.0.0.0/6', '248.0.0.0/5', '240.0.0.0/4', '224.0.0.0/3', '192.0.0.0/2', '128.0.0.0/1' );

$ip =~ m|(\d+)\.(\d+)\.(\d+)\.(\d+)|; my(@ip) = ($1,$2,$3,$4);

my (@masks_to_check); push(@masks_to_check,$ip); foreach my $mask (@possible_masks) { $mask =~ m|(\d+)\.(\d+)\.(\d+)\.(\d+)/(\d+)|; my(@mask_ip) = ($1,$2,$3,$4); my $mask = $5; my $count; my @new_ip; foreach my $mask_element (@mask_ip) {   my $ip_element = $ip[$count]; if ($mask_element > $ip_element) {     push(@new_ip,$ip_element); }   else {     push(@new_ip,$mask_element); }   $count++; } push(@masks_to_check,(join('.',@new_ip).'/'.$mask)); }

foreach my $mask_to_check (@masks_to_check) { warn "Checking $mask_to_check\n"; my $result = check_user($mask_to_check); if ($result) {   warn "Block found:

${$result}{'user'} blocked by ${$result}{'blocker'} till ${$result}{'expiry'}(${$result}{'type'})

${$result}{'summary'}\n";   last;    }  }

sub check_user # Determine if the user is blocked, if so gather information about the block {            # and return it  my ($user,$page) = @_; my $url = 'http://en.wikipedia.org/w/index.php?title=Special:Ipblocklist&ip='.uri_escape($user); my $data = $ua->get($url)->content; # Get blocklist info for user if ($data =~ m|\) blocked <a href|)      # If the user is currently blocked    {    # Get name of blocking admin    ($data =~ m|\d{2}, <a href="/wiki/User:(.*?)" title=|) || ($data =~ m|\d{2}, <a href="/w/index\.php\?title=User:(.*?)&amp;|); #"    my $blocker = uri_unescape($1);    # Get blocking summary    ($data =~ m| \((.*?)\) |);    my $comment = $1;    # Get expiry time of block    my $expiry;    if ($data =~ m|expires (\d+:\d+, \d+ \D+ \d+)|)    # Match expiry time if one exists      {      $expiry = $1;      }    elsif($data =~ m|infinite|) # If there is no expiry and the word 'infinite' is found      {      $expiry = 'indef'; # Set to indef      }    # Get block type flags    my(@flags);    (push(@flags,'anon. only')) if ($data =~ m|anon\. only|);                  # Match anon only    (push(@flags,'account creation blocked')) if ($data =~ m|account creation blocked|);     # Match account creation blocked    (push(@flags,'autoblock disabled')) if ($data =~ m|autoblock disabled|);           # Match autoblock disabled    my $block_type = ''; # Build empty string    # If any flag exists build a flag string.    $block_type = join(', ',@flags) if (scalar(@flags));    return     {      'user'		=> $user,      'blocker'		=> $blocker,      'expiry'		=> $expiry,      'type'		=> $block_type,      'summary'		=> $comment     }; # Queue a remove_name job to run ASAP    }  return 0;  }

sub timeconv { my($expiry, $block_time) = @_; my $duration = $expiry - $block_time; my $formatter = DateTime::Format::Duration->new(   pattern => '%Y years, %m months, %e days, %H hours, %M minutes, %S seconds',    normalize => 1,    base => $block_time,  ); my %normalized = $formatter->normalize($duration); my @periods = ('years','months','days','hours','minutes','seconds'); my $output; if ($normalized{'minutes'} || $normalized{'seconds'}) { $output = sprintf('until %s %s ', $expiry->ymd, $expiry->hms); } else { foreach (@periods) { $output .= sprintf('%s %s, ', $normalized{$_}, $_) if $normalized{$_}; if ($normalized{$_} == 1) { my $singular = $_; $singular =~ s/s$//; $output =~ s/$_/$singular/; }   }    $output =~ s/, $/ /; # special cases if ($output eq '1 day, 7 hours ') { $output = '31 hours '; } elsif ($output eq '4 days, 3 hours ') { $output = '99 hours '; } elsif ($output eq '4 days, 4 hours ') { $output = '100 hours '; } }  return $output; }