Scripts

Sysadmin

Recursion

There is a useful recursive shell script do stuff in every single folder, will act like:

 
find . -exec "what ever" {} \;  

With far more flexibility but slower

#!/bin/bash
file(){
 
# There you put your bit, I have just put "pwd"
pwd
 
}
 
folder(){
for i in `ls -A` ; do
  if [[ -d "$i" ]] ; then
    cd $i
    file
    folder
  fi
done
cd ..
}
 
folder

log analyse

There is a usefull perl script to read logs in real time (like tail -f), run some filter and output without buffering. You will have to install this library: http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm

This could be done in shell like :

tail -f somelog | "stuff do be done" | tee -a output.log

But if the stuff to be done start to be to complex perl is a great solution.

#!/usr/bin/perl
 
use strict;
use File::Tail;
 
my $INPUTLOG="/var/log/smtp.log";
my $OUTPUTLOG="/tmp/out.log";
my $outf;
 
open( $outf, ">>$OUTPUTLOG") || die "$0: couldn't open $OUTPUTLOG";
select($outf); $|=1; select(STDOUT);
 
my $file=File::Tail->new(name=>$INPUTLOG, maxinterval=>1, interval=>1);
while (defined(my $line=$file->read)) {
 
  # DO WHAT EVER FOR EVERY LINES
  # EX: grep for an ip  
  if ( $line =~ m/.*(\d+.\d+.\d+.\d+).*/ ) {
    #PRINT THE RESULT IN THE OUTPULOG file
    print $outf "$1\n";
    #AND PRINT ON THE SCREEN
    print "$1\n";
  }
}

check port

If you can't use nmap there is a perl script to test an ip and port
./checkport.pl ip port

#!/usr/bin/perl -w
 
use strict;
use Socket;
 
# initialize host and port
my $host = shift || '192.168.1.1';
my $port = shift || 22;
 
my $proto = getprotobyname('tcp');
 
# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
 
# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";
 
my $line;
while ($line = <SOCKET>) {
        print "$line\n";
        exit;
}
close SOCKET or die "close: $!";
 
#end of story

backup

Before editing a configuration file, make a backup

#!/bin/bash
 
if  [[ $# -ne 1 ]] ;then
  echo "You should enter only one file to backup"
  exit
fi
 
 
#Backup
#Sleep 1 make sure won't have duplicate file
sleep 1
out="$1.bak_$(date +%m-%d-%y_%H:%M:%S)"
cp -p "$1" "$out"
chmod a-w "$out"
 
#Make sure your backup is correct. 
diff "$1" "$out" 2>/dev/null
[[ $? -ne 0 ]] && { echo "Backup $1 failled" ; exit ; }
 
#end of story

web

get ip - v1

Ip and date will be store in the file “ip_out.csv” using csv to store information.
The structure is “date”,”time”,”ip”.

<form action="ip.php" method="get">
<input type="submit" name="ip" value="getip" />
<input type="submit" name="reset" value="reset" />
</form>
 
<?
//defined the output file
$out_file="ip_out.csv";
 
//if the value is reset then remove the file
if (isset ($_GET["reset"])){
  unlink($out_file);
}
 
//Get the remote ip address
$ip = getenv(REMOTE_ADDR);
 
//Define the date and time
$date = date("d/m/Y");
$time = date("h:i:s");
 
//Open the file and write the content of "date time and ip address
$out = fopen ("$out_file",'a');
fwrite ($out,"$date,$time,$ip\n");
fclose($out);
 
//Open the file to read it
$in = fopen ("$out_file",'r');
$cmp = 0 ;
while (!feof($in)) {
//Put in the file in one var $content
  $contents .= fread($in, 8192);
//Creat a table by using ";" as separator
  $a= split("\n", $contents);
//Reverse the table to get the last output on the top
  $result = array_reverse($a);
  foreach ($result as $value) {
//Creat a table for each line using , as separator
    $b = split (",",$value);
    if (ereg ("[0-9]+",$b[0])){
       echo "$b[0]:$b[1] -> $b[2]<br>";
    }
  }
} 
 
?>

get ip - v2

<?
function get_remote_ip() {
  return $_SERVER['REMOTE_ADDR'];
}
 
echo "Your IP address is: " . get_remote_ip();
?>

Fill form

#!/usr/bin/perl
use WWW::Mechanize;
use strict;
 
my $mech = WWW::Mechanize->new( autocheck => 1 );
 
$mech->get( "http://google.com" );
 
 
$mech->submit_form(
            form_number => 1,
            fields      => { q => 'big-up.org', }
        );
 
    print $mech->content;

fun

Creat a fake tree

$ mkdir -p tree/{a/{aa/{aaa/aaaa/aaaaa,aac},ab},b/{ba,bb/{bba,bbb/bbba/bbbba/bbbbba},bc},c,d/{da,db/{dda,ddb/ddba},dc},e}
$ find .
.
./tree
./tree/a
./tree/a/aa
./tree/a/aa/aaa
./tree/a/aa/aaa/aaaa
./tree/a/aa/aaa/aaaa/aaaaa
./tree/a/aa/aac
./tree/a/ab
./tree/b
./tree/b/ba
./tree/b/bb
./tree/b/bb/bba
./tree/b/bb/bbb
./tree/b/bb/bbb/bbba
./tree/b/bb/bbb/bbba/bbbba
./tree/b/bb/bbb/bbba/bbbba/bbbbba
./tree/b/bc
./tree/c
./tree/d
./tree/d/da
./tree/d/db
./tree/d/db/dda
./tree/d/db/ddb
./tree/d/db/ddb/ddba
./tree/d/dc
./tree/e
 
scripts.txt · Last modified: 2010/02/10 13:46 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki