Comment savoir si une machine est 32 bits ou 64 bits ?

Méthode 1

cat /proc/cpuinfo

model name    : AMD Athlon(tm) 64 Processor 3500+

Méthode 2 (avec Perl)

Lorsque cela n’est pas parlant comme pour la eeebox:

model name    : Intel(R) Atom(TM) CPU N270   @ 1.60GHz

perl -e 'print ~123;'

  • 32 bits:

4294967172

  • 64 bits:

18446744073709551492

Source: CJT

Perl – Rechercher une expression et garder plusieurs lignes

Imaginez le texte :

2008-01-02: first entry
2008-02-03: second entry on two lines
    here is the additional line
2008-03-04: third entry
   has
   three
   extra lines
2008-04-05: fourth entry has just one on line again

Question :

Comment faire pour avoir le groupe de lignes qui contient un mot, par exemple « three » ?

Solution :

my @stuff;
while (<IN>) {
    if (/^\s/) { $stuff[-1] .= $_; }
    else { push @stuff, $_;  }
}
print grep { /three/ } @stuff;

Donne :

2008-03-04: third entry
   has
   three
   extra lines

Source

Créer des liens SSH avec Firefox 3

Configurer votre client léger
Ouvrir Firefox à l’addresse suivante:

about:config

network.protocol-handler.app.ssh; /home/user/fox.sh
network.protocol-handler.external.ssh; true
network.protocol-handler.warn-external.ssh; true

/home/user/fox.sh

#!/bin/sh
gnome-terminal -e "ssh `perl -MURI -e 'print $u=URI->new($ARGV[0])->host;' $1`"

Intégrer des liens SSH à l’HTML

Il suffit maintenant d’intégrer des liens de cette forme:

a href="ssh://hostname"

En cliquant sur le lien une fenêtre de terminal s’ouvrira.

App

Apache – Blacklistage d’IPs

  • Dans le core ou vhost :

RewriteEngine on
Rewritemap blacklist txt:/path/to/blacklist.txt
RewriteCond ${blacklist:%{REMOTE_ADDR}} =b
RewriteCond %{request_uri} !=/sorry.html
RewriteRule .* /sorry.html                        [R,L]

>cat /path/to/blacklist.txt
193.220.137.2 b
81.199.171.20 b

  • Sans réécriture :

<Limit GET HEAD POST>
order allow,deny
deny from 202.70.112.0/20
allow from all
</LIMIT>

Générateur de classes IP (par pays) en ligne : http://blockacountry.com

  • Dynamique (ex. par pays) :

Avec la liste ip-to-country et un script Perl du style :
#!/usr/bin/perl -w
use strict;
my %ip;
my $old_ip=0;
while (<>) {
my ($ip) = split;
next if ($ip eq $old_ip);
my ($a,$b,$c,$d) = split(/\./,$ip);
my $n = ((((($a * 256) + $b) * 256) + $c) * 256) +$d;
open (FILE,"< ip-to-country.csv") || die $!;
while (<FILE>) {
my ($beg,$end,$country) = (split (/,/,$_))[0,1,4];
$beg =~ s/"//g;
$end =~ s/"//g;
if (($beg <= $n) and ($end >= $n)) {
$ip{$ip}++;
print "ip = $ip\n";
last;
}}
close(FILE);
$old_ip=$ip;
}
exit(0);

Perl – timestamp <-> date

  • TimeStamp vers Date

$ perl -e "print scalar(localtime(1173279767))"

  • Date vers TimeStamp

$ perl -MPOSIX -e "print (mktime(10,45,11,31,4,107));"

Avec :
# mktime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0)
#
# The month (mon), weekday (wday), and yearday (yday) begin at zero.
# The year (year) is given in years since 1900.
# Sunday is 0, not 1;
# January 1st is 0, not 1.
#
# I.e. January is 0, not 1;
# I.e. The year 1995 is 95; the year 2001 is 101.
# I.e. Calendar time for December 12, 1995, at 10:30 am : mktime( 0, 30, 10, 12, 11, 95 );
#
# Greenwich Mean Time (GMT)
# Daylight saving Time (DST)
# Coordinated Universal Time (CUT)
##################################

Source

Un peu de domotique

Après avoir acheté pour une trentaine d’euros un ensemble carte 8 relais + controleur USB (cf.photo), me voici parti dans la prog. du port série.

1. Module pour faire de l’USB un port série :|

usbserial + ftdi_sio (/dev/ttyUSB0)

2. Le Perl script :

#!/usr/bin/perl -w

use Device::SerialPort;
use strict ;

my $str = «  » ;
my @cmd = () ;

# Set up the serial port
# 9600, 81N on the USB ftdi driver
my $port = Device::SerialPort->new(« /dev/ttyUSB0″);
$port->databits(8);
$port->baudrate(9600);
$port->parity(« none »);
$port->stopbits(1);
$port->handshake(« none »);

$port->write_settings || die(« Could not set up port »);

# 8 relays test
foreach my $nb_relay (1 .. 8 ) {
push (@cmd, [ 'on', 255, ${nb_relay}, 1 ,
'off', 255, ${nb_relay}, 0 ] ) ;
}

# Test
foreach my $c (@cmd) {
# Turn relay on
$str = join(  », map {pack(‘C’,$_)} (@{$c}[1..3]) ) ;
print « Relay $c->[2] => $c->[0]\n »;
$port->write( $str. »\r\n » );
$port->write_done();
sleep(1);
# Turn relay off
$str = join(  », map {pack(‘C’,$_)} (@{$c}[5..7]) ) ;
print « Relay $c->[2] => $c->[4]\n »;
$port->write( $str. »\r\n » );
$port->write_done();
sleep(1);
}
$port->close();

3. Photo

4. Video

html2xml²

Transformer un html en xml, auquel l’on appliquera une transformation xslt pour au final avoir l’xml de nos rêves.

Il faut pour cela:

  • html2xml.pl (version 0.6) notre parseur perl
  • de la documentation sur xslt afin de créer une feuille de style .xsl

1) .html -> .xml

html2xml.pl fichier.html ficher.xml

2) .xml -> .xml

xsltproc feuilledestyle.xsl fichier.xml fichier_final.xml

Web

Parsing XML et insertions MySQL avec Perl

Installation des modules DBI et XML::XPath via CPAN

perl -MCPAN -e 'install DBI'
perl -MCPAN -e 'install XML::XPath'

ou

apt-get install libxml-xpath-perl

data.xml
<?xml version="1.0" encoding="UTF-8"?>
<data>
<people>
<name>alexandre</name>
<surname>alex</name>
<age>30</age>
</people>
<people>
<name>alice</name>
<surname>ali</name>
<age>30</age>
</people>
</data>

parser.pl
#!/usr/bin/perl -w
use strict;
use DBI;
use XML::XPath;
use XML::XPath::XMLParser;
my $dbh = DBI->connect ("DBI:mysql:dbname",
"mysql_user", "mysql_password",
{ RaiseError => 1, PrintError => 0});
my $xp = XML::XPath->new (filename => $ARGV[0]);
my $nodelist = $xp->find ("//people");
foreach my $row ($nodelist->get_nodelist ())
{
$dbh->do (
"INSERT INTO test (field1, field2, field3) VALUES (?,?,?)",
undef,
$row->find ("field1")->string_value (),
$row->find ("field2")->string_value (),
$row->find ("field3)")->string_value ()
);
}
$dbh->disconnect ();

Using XML with MySQL

Recent Tweets

Catégories