Posts Tagged script

backup.bat semplice script in DOS

Si, talvolta anche io ho a che fare con windows..

@echo off
:: variables
set drive=G:\Backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y

echo ### Backing up My Documents...
%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents"

echo ### Backing up Favorites...
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites"

echo ### Backing up email and address book (Outlook Express)...
%backupcmd% "%USERPROFILE%\Application Data\Microsoft\Address Book" "%drive%\Address Book"
%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Identities" "%drive%\Outlook Express"

echo ### Backing up email and contacts (MS Outlook)...
%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook" "%drive%\Outlook"

echo ### Backing up the Registry...
if not exist "%drive%\Registry" mkdir "%drive%\Registry"
if exist "%drive%\Registry\regbackup.reg" del "%drive%\Registry\regbackup.reg"
regedit /e "%drive%\Registry\regbackup.reg"

:: use below syntax to backup other directories...
:: %backupcmd% "...source directory..." "%drive%\...destination dir..."

echo Backup Complete!
@pause

PCM3TNXD3NGK

Share

, , , , , , , , ,

No Comments

Proxy: Analisi dei log

In questi giorni mi sono trovato a implementare un nuovo firewall per la rete dell’ufficio, mi appunto quindi qui di seguito uno script che uso per analizzare i log di squid e farmi un’ idea riguardo dove viene sperperata la banda (e da chi)   ;-)

#!/usr/bin/perl -w
#
#  $log_file is the squid log
#

$log_file = '/var/log/squid/sqlog';

#
#  We need to create our own logs
#

$wlogs="/root/wlog";

#
#  and finally the location of the html files this will create
#

$webloc="/localweb/html/squidlog";

#  If this isn't run off-hours, create them in a temporary location, and
#  wrap this in a shell script that copies them after they are created-
#  analyzing a large log can take a long time.
#

$www_files = 0;   # Pages not in cache
$files_cached = 0; # Pages from cache

#
# 

($null,$minute,$hour,$day,$month,$year,$null,$null,$null)=localtime(time);
$year = $year + 1900;
$month=$month + 1;

#
# 

open(SQUIDLOG,$log_file) or die "Can't open $log_file: $!";
open(MAININDEX, ">$webloc/index.html");

#
#  Main index.html for the analysis

print MAININDEX <<EOF;
<HTML><HEAD><TITLE>Squid-Log until $month/$day/$year<\/TITLE>
</HEAD><BODY>
<h2>Web access analysis</h2>
EOF

# It's tempting to just suck the logfile into an array and work with it.
# That would eliminate the need for the sub-logs in $wlog
# However, Squid log files tend to be VERY large.  If you had tons of
# memory, that would be a quicker and easier method, but typically
# the proxy server machine is a weaker server (it doesn't need
# much to run Squid).
#

while (<SQUIDLOG>) {

#
# For each log file line, we're going to split it up into @line
#
# These variables get set from each element of the @line array
# and reset to blank as each new line is read.

  $internal_ip="";
  $linkvisited="";
  $sitevisited="";
  $linkdate="";
  $human_date="";
  $minute="";
  $hour="";
  $cached_line="";

#
# A log file line looks this
# 982962881.790 2 192.168.2.114 TCP_IMS_HIT/304 226 GET / - NONE/- text/html
#

  $logfile_line=$_;
  $logfile_line =~ s/ /\+/g;
  next if ! $logfile_line ;

#
  @line = split /\+/, $logfile_line;
#
  foreach $tempvar (@line) {
     if (($tempvar =~ m/\d+\.\d+/) and  ( ! $linkdate) ) {
        # This is the date stamp; 982962881.790
           ($null, $minute, $hour, $day, $month, $year, $null,
                               $null, $null)= localtime($tempvar);
           $month++;
           $year=$year+1900;
           if ($minute < 10) { $minute = '0'.$minute;}
           if ($month < 10) { $month = '0'.$month;}
           if ($day < 10) { $day = '0'.$day;}
        #
           $logday="${year}_${month}_${day}";
           $accumdate{"$logday"} += 1;
           $linkdate=$tempvar;
           $human_date=$logday;
     }

     if (($tempvar =~ m/192\.168\./) and (! $internal_ip) ) {
             $ip = $tempvar;
             $accumip{$ip} += 1;
             $internal_ip=$ip;
     }

     if (($tempvar =~ m/http:\/\//) and (! $linkvisited) )
     {
             $document = $tempvar;
             $document =~ s/.*http:..//;
             $document =~ s/\/.*//;
             $linkvisited=$document;
             #
             # We want just the base domain name
             # if the hit was http://whatever.someplace.com/this.html
             # then $linkvisited is whatever.someplace.com
             # and $sitevisited will be someplace.com
             #
             @comp=split(/\./,$document);
             $cnt2=@comp;
             $document="$comp[$cnt2 - 2].$comp[$cnt2 - 1]";
             #
             # but sometimes it's just an ip address
             $document=$linkvisited if ($document =~ /^\d+\.\d+$/ );
             #
             $sitevisited=$document;
             $accumurl{"$document"} +=1;

     }

     if (($tempvar =~ m/TCP/) and (!$cached_line) )
     {
             $cache = $tempvar;
             if ($cache =~ m/HIT/) {
                $files_cached++;
             }
             if ($cache =~ m/MISS/) {
                 $www_files++;
             }
     }
  }
#
# finished with the line from the squid log, now rewrite to our sub-logs
#
# Sometimes log files are incomplete or otherwise messed up
# We could just skip 'em, but I'd rather know we had a problem
  $human_date="unknown" if not $human_date;
  $linkvisited="unknown" if not $linkvisited;
  $internal_ip="unknown" if not $internal_ip;
  $sitevisted="unknown" if not $sitevisted;
  $minute="unknown" if not $minute;
  $hour="unknown" if not $hour;

  open(DLOG,">>$wlogs/$document");
  $our_log="$human_date|$linkvisited|$internal_ip|$sitevisited|
                                                   $minute|$hour\n";
  print DLOG $our_log;
  close DLOG;
  open(DLOG,">>$wlogs/$human_date");
  print DLOG $our_log;
  close DLOG;
  open(DLOG,">>$wlogs/$internal_ip");
  print DLOG $our_log;
  close DLOG;
}

# log file is now completely read; generate output

$total_files = $www_files + $files_cached;
if (!($total_files<=0))
{
        $www_percent = $www_files / $total_files * 100;
        $cache_percent = $files_cached / $total_files * 100;
}

printf MAININDEX "<p>Total: $total_files files.<br>$files_cached ( %6.2f %% )
    files cached.<br>$www_files ( %6.2f %% ) files downloaded\n",
                                         $cache_percent, $www_percent;
print MAININDEX "<h2>By Date</h2><table>\n";

#
# Now by date
#
foreach (sort keys %accumdate) {
   #
   # $_ will be like 2001_02_25
   #
   $cnt=$accumdate{$_};
   print MAININDEX "<tr><td>$_</td><td><a href=\"$_.html\">$cnt</a></td>
                                                                      \n";
   open(HTMLDETAIL,">$webloc/$_.html");
   print HTMLDETAIL "<HTML><HEAD><TITLE>Squid-Log until
                                           $month/$day/$year<\/TITLE>\n";
   print HTMLDETAIL  "</HEAD><BODY><h2>$_</H2><table>\n";
   open(SUBLOG,"$wlogs/$_");
   while (<SUBLOG>) {
    chomp;
   ($ht,$url,$ip,$site,$min,$hr)=split /\|/;
   print HTMLDETAIL "<tr><td>$ht $hr:$min</td><td>$ip</td><td>
                                   <a href=\"$site.html\">$site</a></td>";
   }
  print HTMLDETAIL "</table></body></HTML>";
  close HTMLDETAIL;
}
#
# and by local user
#
print MAININDEX "</table><h2>By Local User</h2>";
print MAININDEX "<table>";
foreach (sort keys %accumip) {
   #
   # $_ will be a local ip address
   #
   $cnt=$accumip{$_};
   print MAININDEX "<tr><td>$_</td><td><a href=\"$_.html\">
                                                     $cnt</a></td>\n";
   open(HTMLDETAIL,">$webloc/$_.html");
   print HTMLDETAIL "<HTML><HEAD><TITLE>Squid-Log until
                                        $month/$day/$year<\/TITLE>\n";
   print HTMLDETAIL  "</HEAD><BODY><h2>$_</H2><table>\n";
   open(SUBLOG,"$wlogs/$_");
   while (<SUBLOG>) {
    chomp;
   ($ht,$url,$ip,$site,$min,$hr)=split /\|/;
   print HTMLDETAIL "<tr><td>$ht $hr:$min</td><td>$ip</td><td>
                               <a href=\"$site.html\">$site</a></td>";
   }
  print HTMLDETAIL "</table></body></HTML>";
  close HTMLDETAIL;
}
print MAININDEX "</table><h2>By URL</h2>";
print MAININDEX "<table>";
#
# and then finally by URL
#
foreach (sort keys %accumurl) {
   #
   # $_ will be like ibm.com or 204.34.156.9
   #
   $cnt=$accumurl{$_};
   print MAININDEX "<tr><td>$_</td><td><a href=\"$_.html\">
                                                    $cnt</a></td>\n";
   open(HTMLDETAIL,">$webloc/$_.html");
   print HTMLDETAIL "<HTML><HEAD><TITLE>Squid-Log until
                                        $month/$day/$year<\/TITLE>\n";
   print HTMLDETAIL  "</HEAD><BODY><h2>$_</H2><table>\n";
   open(SUBLOG,"$wlogs/$_");
   while (<SUBLOG>) {
    chomp;
   ($ht,$url,$ip,$site,$min,$hr)=split /\|/;
   #
   # this is different than the other sections because
   # we want to be able
   # to actually go to the url as originally acessed.
   #
   print HTMLDETAIL "<tr><td>$ht $hr:$min</td><td>$ip</td><td>
                                    <a href=\"http://$url\">$url</a></td>";
   print  "<tr><td>$ht $hr:$min</td><td>$ip</td><td>
                                    <a href=\"http://$url\">$url</a></td>";
   }
 print HTMLDETAIL "</table></body></HTML>";
 close HTMLDETAIL;
}
# all done
print MAININDEX "</table>";
print MAININDEX "</BODY></HTML>\n";
close MAININDEX;
Share

, , , , ,

No Comments

Convertire flv in avi con Linux

Mi appunto un semplice script con cui convertire files da formato flash *.flv in formati `umani` *.avi

#!/bin/sh

if [ -z "$1" ]; then
echo “Usage: $0 {-divx|-xvid} list_of_flv_files”
exit 1
fi

# video encoding bit rate
V_BITRATE=1000

while [ "$1" ]; do
case “$1″ in
-divx)
MENC_OPTS=”-ovc lavc -lavcopts \
vcodec=mpeg4:vbitrate=$V_BITRATE:mbd=2:v4mv:autoaspect”
;;
-xvid)
MENC_OPTS=”-ovc xvid -xvidencopts bitrate=$V_BITRATE:autoaspect”
;;
*)
if file “$1″ | grep -q “Macromedia Flash Video”; then
mencoder “$1″ $MENC_OPTS -vf pp=lb -oac mp3lame \
-lameopts fast:preset=standard -o \
“`basename $1 .flv`.avi”
else
echo “$1 is not Flash Video. Skipping”
fi
;;
esac
shift
done

e’ possibile passargli una lista di *.flv da convertire appunto in *.avi con:


./converti.sh -divx *.flv

Sul sistema devono chiaramente essere presenti ffmpeg, mencoder, mplayer, lame e quant’altro.

Share

, , , , , , , , ,

No Comments

Configurazione Rapida LAN con netsh.exe

Spesso accade di dover accedere con il nostro laptop windows (io no!) a reti locali differenti dove magari non c’? un server DHCP, pertanto ogni volta riconfigurare la nostra scheda ethernet pu? risultare annoiante. Ecco qui un semplice script .bat e un file di configurazione per modificare automaticamente i parametri.

Supponiamo di avere in ufficio un server DHCP mentre a casa un tipico HAG Fastweb con ovviamente una ben definita ACL, creiamo quindi il file home.bat e il conf confHome.txt per casa e uno script office.bat con il relativo conf confOffice.txt per il nostro ufficio:

  • File home.bat conterr?:

@echo Cambia rete
@echo off
netsh exec confHome.txt
@echo rete cambiata

  • Il file confHome.txt (i parametri sono personali) conterr? ad esempio:

interface
ip
set address name="Connessione alla rete locale (LAN)" source=static addr=192.168.1.15 gateway=192.168.1.254 mask=255.255.255.0 gwmetric=0
set dns name="Connessione alla rete locale (LAN)" source=static addr=213.102.12.0 register=primary
add dns name="Connessione alla rete locale (LAN)" addr=213.140.2.21
quit

  • Lo script office.bat sar? invece:

@echo Cambia rete
@echo off
netsh exec confOffice.txt
@echo rete cambiata

  • Il file confOffice.txt conterr? quindi:

interface
ip
set address name="Connessione alla rete locale (LAN)" source=dhcp
set dns name="Connessione alla rete locale (LAN)" source=dhcp
quit

Ovvimente il file.bat ed il relativo conf devono stare nella stessa directory ma il link all’eseguibile potete piazzarlo un p? dove vi pare.

Share

, , , , , , , , , ,

No Comments

#!/bin/bash :)

Mi appunto un semplice comando con il quale trovare del testo contenuto in un file, a sua volta contenuto in una directory e in eventuali sottodirectory:

find /etc -type f -exec grep -l ‘mybox’ {} \; > /tmp/find.out

Con questo comando andiamo a cercare in tutti i file di /etc/ e sottodirectory la parola ‘mybox’, il risultato verra’ stampato in /tmp/find.out.

Nota bene fra le graffe non vi e’ nessuno spazio.

Share

, , , , , , ,

No Comments