Posted by root
Fri, 22 Feb 2008 21:23:00 GMT
use strict;
use warnings;
my $dom = shift || die "missing name\n";
my $res = qx{whois $dom};
die $1 . "for '$dom'\n" if $res =~ /(no\s+whois\s+server\s+)/i;
die "$dom is free\n" if $res =~ /no\s+match|does\s+not\s+exist/i;
die "$dom is reserved\n";
Posted in Perl, scripts | Tags money | no comments
Posted by root
Mon, 04 Feb 2008 21:57:00 GMT
#!/bin/bash
file=$1
if [ -e $file ]
then
vim $file
else
touch $file
echo '#!/usr/bin/perl' > $file
echo '' >> $file
echo 'use strict;' >> $file
echo 'use warnings;' >> $file
echo 'use diagnostics;' >> $file
echo '' >> $file
chmod +x $file
vim $file
fi
Posted in Perl, scripts | Tags UNIX | no comments
Posted by root
Sun, 03 Feb 2008 11:26:00 GMT
Copy all those formats (pdf|chm|txt|html) from $HOME/Desktop to the current working directory:
find ~/Desktop/ -iname "*.pdf" \
-o -iname "*.chm" \
-o -iname "*.txt" \
-o -iname "*.html" \
-exec cp -v {} . \;
more about find command
Posted in scripts | Tags UNIX | no comments
Posted by root
Sat, 02 Feb 2008 12:59:00 GMT
Perl script (thanks to the author of HTML::WikiConverter):
use strict;
use warnings;
use HTML::WikiConverter;
use Perl6::Slurp;
my $file_name = shift;
-f $file_name or die "No file given!\n";
my $wiki = new HTML::WikiConverter(dialect=>'MediaWiki');
print $wiki->html2wiki(slurp $file_name);
Posted in Perl, scripts | Tags converter, wiki | no comments
Posted by root
Sat, 19 Jan 2008 05:28:00 GMT
use strict;
use Getopt::Std;
use File::Basename;
getopts('scf:p:');
our($opt_s, $opt_c, $opt_f, $opt_p);
my $file = $opt_f;
my $size = $opt_p || 1;
my $len = 1024;
my $c = 1;
my ($buf, $counter);
-f $file or usage();
if($opt_s){
$size = 1024 * $size;
open IN, "< $file" or die "cannot open_r $file $!";
open OUT, "> $file.$c" or die "cannot open_w $file $!";
binmode IN;
binmode OUT;
while(read(IN, $buf, $len)){
$counter++;
if($counter > $size){
$counter = 0;
$c++;
close OUT;
open OUT, "> $file.$c" or die "cannot open_w $file $!";
binmode OUT;
}
print OUT $buf
}
} elsif($opt_c) {
my @files = grep { -f and /^$file\.\d+$/ } glob '*';
my $newfile = "splitter_$file";
open OUT, ">> $newfile" or die "cannot open_w $file $!";
binmode OUT;
map { $_ =~ s/^$file\.// } @files;
for(sort {$a<=>$b} @files ){
open IN, "$file.$_" or die "cannot open_r $file $!";
binmode IN;
print OUT $_ while <IN>;
close IN;
}
} else {
usage()
}
sub usage{
my $pro = basename($0);
print <<SQ;
$pro (-s|-c) -p piece size -f filename
-s split a file into pieces
-c collect a file from pieces
-p chunk size (defaults to 1MB)
-f file to be processed
SQ
exit 1
}
Posted in Perl, scripts | Tags splitter | no comments
Posted by root
Sun, 13 Jan 2008 07:58:00 GMT
Rename each file in each sub-directory, substituting ‘%20’ with ’ ‘(space).
class File
def self.rename_basename(abs_name, old_str, new_str)
f_name = File.basename(abs_name).gsub Regexp.quote(old_str), new_str
d_name = File.dirname(abs_name)
File.rename(abs_name, d_name + '/' + f_name)
rescue
raise ArgumentError, "No such file #{filename}", caller
end
end
Dir["*/**"].each do |f|
if File.basename(f).match('%20')
File.rename_basename(f, '%20', ' ')
end
end
Posted in Ruby, scripts | Tags tools | no comments
Posted by root
Sat, 29 Dec 2007 15:28:00 GMT
export PATH=$PATH:$HOME/bin:/usr/sbin:/sbin:$HOME/network
export TERM=linux
export gemdoc=`gem environment gemdir`/doc
function prompt_set {
local GRAY="\[\033[1;30m\]"
local LIGHT_GRAY="\[\033[0;37m\]"
local CYAN="\[\033[0;36m\]"
local LIGHT_CYAN="\[\033[1;36m\]"
local NO_COLOUR="\[\033[0m\]"
case $TERM in
xterm*|rxvt*)
local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
local TITLEBAR=""
;;
esac
local temp=$(tty)
local GRAD1=${temp:5}
PS1="$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\u$GRAY@$CYAN\h$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\$(date +%H:%M)$GRAY:$CYAN\w\
$LIGHT_CYAN)$CYAN-$GRAY-$LIGHT_GRAY "
PS2="$LIGHT_CYAN-$CYAN-$GRAY-$NO_COLOUR "
}
prompt_set
alias get_visits="ssh l 'tail /home/postgres/stats.txt'"
alias __='history | tail -2 | head -1'
alias r="temproot `id -u`'"
alias sql='mysql --password=pass'
alias ..='cd ..';
alias ...='cd ../..';
alias ,='cd -'
alias e=exit
alias e=exit
alias v=vim
alias l='ls -lc -h --color=yes'
alias c=clear
alias top='top -d1'
alias hc='history -c'
alias gre=grep
alias gr=gre
alias gpre=gr
alias grp=gpre
alias le=less
alias mroe=more
alias mreo=mroe
alias h=history
alias pe='perl -e'
alias pc='perl -c'
alias t=date
alias d=date
function FOR {
local count=$1
start=0
shift
while [ $start -lt $count ]
do
$*
sleep 1
clear
let start=$start+1
done
}
Posted in scripts | no comments
Posted by root
Sat, 29 Dec 2007 15:26:00 GMT
$”=” @ARGV”;/^\D+/||print for`nmap$”`
Posted in Perl, scripts | no comments
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 scripts | Tags administration, UNIX | no comments
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 scripts | Tags administration, scripts | no comments