Binary?!

So, my son asked me yesterday about how computers work, and we finally got to binary, which he had heard about in school. Then he asks me how numbers look like in binary, and stuff like that. Instead of looking them up, I came up with a perl script to show him any number he would want to see in binary. It’s nothing earth-shattering, but I thought someone may want get a kick out of it, and it really shows how powerful perl is. So, here goes:

#!/usr/bin/perl -w

use strict;

# Get the number from the command line or use default.
my $number = shift || 42;

printf "%b\n", $number;

Processes by uid in perl.

This is just a quick hack to find which uid owns how many processes on your system. Obviusly, this can be altered to just about anything, but it’s a good example of how perl can use Unix commands inside a script.

#!/usr/bin/perl -w

# Who owns how many processes

@pslist = qx/ps -ef/;

# Remove the header line
shift @pslist;

foreach $line (@pslist)
{
@splitline = split(" ",$line);
# Count usernames
$count{$splitline[0]}++;
}

foreach $user (sort keys (%count))
{
print "User $user owns $count{$user} processes \n";
}

Simple script for filtering out a proxy IP from web traffic.

#!/usr/bin/perl -w

# A script to filter an internal proxy IP in apache logs, plus counting IPs.

open (FH, "access_log") or die ("Open Failed: $!\n");

while (){
$infile = $_;
chomp($infile);
if ($ip_p = m/(192\.168\.10\.2)\ - - /)
{$ip_proxy += $ip_p};

# Addition: Count all IP's and display sum
if ($ip_a = m/(.*)\ - - /)
{$ip_all += $ip_a};
# End Addition
}

# Difference between the two.
$ip_diff = $ip_all - $ip_proxy;

# Diff divided by Proxy =~ Ratio
$ip_ratio = $ip_diff / $ip_proxy;

print "Total IP count is: ALL\n";
print "Proxy IP (192.168.10.2) count is: Proxy\n";
print "IP without Proxy count is: Diff\n";
print "----------------------------------------------\n";
print " ALL: Proxy: Diff:\n";
printf("%7d %7d %7d\n",$ip_all,$ip_proxy,$ip_diff);
print "\n";
print "The ratio of proxy requests to all other requests is 1:$ip_ratio.\n";
print "\n";

close FH;