Copy Whole Disk over Network

rsync -axv -e ssh root@source:/ /mnt/root

Disk Encryption Using LUKS

Load device mapper, encrypt partition, optionally add second passphrase, open encrypted partition and format it using ReiserFS:
moprobe dm-mod
cryptsetup -c aes-xts-plain -y -s 512 luksFormat /dev/sda1
cryptsetup luksAddKey /dev/sda1
cryptsetup luksOpen /dev/sda1 root
mkfs.reiserfs -l ROOT /dev/mapper/root

PostgreSQL Timestamp and Unix Time Conversions

PostgreSQL timestamp to Unix time:
postgres=# select extract(epoch from timestamp with time zone '2011-01-02 03:04:05+06');
 date_part  
------------
 1293915845
(1 row)
Unix time to PostgreSQL timestamp:
postgres=# select to_timestamp(1293915845);
      to_timestamp      
------------------------
 2011-01-01 22:04:05+01
(1 row)

Capture and Replay Network Traffic

Example of capturing and replaying captured syslog traffic.

Capture:
tcpdump -v -s 0 -w syslog.dump -nn udp dst port 514
Rewrite:
tcprewrite --fixcsum \
           --enet-dmac=00:11:22:33:44:55 \
           --dstipmap=0.0.0.0/0:1.2.3.4/32 \
           --infile=syslog.dump \
           --outfile=syslog.redump
Replay:
tcpreplay -i eth0 --topspeed syslog.redump
tcpreplay -i eth0 --mbps=5.0 syslog.redump

POE::Component::Client::DNS

Examples of resolving a "large" number of domains in parallel using asynchronous DNS calls.

Version 3 API
#!/usr/bin/env perl
use strict;
use warnings;
use POE;
use POE::Component::Client::DNS;
my @ips = qw( 127.0.0.1 10.0.0.1 192.168.0.1 192.168.1.1 1.2.3.4
8.8.4.4 8.8.8.8 208.67.220.220 208.67.222.222 );
my $resolver = POE::Component::Client::DNS->spawn(
Alias => 'resolver',
Timeout => 10,
);
POE::Session->create(
inline_states => {
_start => sub { $_[KERNEL]->yield('lookup') for 1..5; },
lookup => sub {
my $ip = shift @ips;
return unless defined $ip;
# Example of using POE::Component::Client::DNS Version 3 API
my $response = $resolver->resolve(
event => 'response',
host => $ip,
type => 'PTR',
context => { },
);
$_[KERNEL]->yield(response => $response) if $response;
},
response => sub {
my $request_address = $_[ARG0]->{'host'};
my $net_dns_packet = $_[ARG0]->{'response'};
my $net_dns_errorstring = $_[ARG0]->{'error'};
unless ( defined $net_dns_packet ) {
print "$request_address: error ($net_dns_errorstring)\n";
$_[KERNEL]->yield('lookup');
return;
}
my @net_dns_answers = $net_dns_packet->answer();
unless ( scalar @net_dns_answers ) {
print "$request_address: no answer\n";
$_[KERNEL]->yield('lookup');
return;
}
foreach ( @net_dns_answers ) {
if ( $_->type eq "PTR" ) {
( my $fqdn = $_->rdatastr ) =~ s/\.$//;
print "$request_address: $fqdn\n";
last;
}
}
$_[KERNEL]->yield('lookup');
},
}
);
$poe_kernel->run();
exit 0;
view raw dns-3.pl hosted with ❤ by GitHub

Version 2 API
#!/usr/bin/env perl
use strict;
use warnings;
use POE;
use POE::Component::Client::DNS;
my @ips = qw( 127.0.0.1 10.0.0.1 192.168.0.1 192.168.1.1 1.2.3.4
8.8.4.4 8.8.8.8 208.67.220.220 208.67.222.222 );
my $resolver = POE::Component::Client::DNS->spawn(
Alias => 'resolver',
Timeout => 10,
);
POE::Session->create(
inline_states => {
_start => sub { $_[KERNEL]->yield('lookup') for 1..5; },
lookup => sub {
my $ip = shift @ips;
return unless defined $ip;
# Example of using POE::Component::Client::DNS Version 2 API
$_[KERNEL]->post( 'resolver',
'resolve',
'response',
$ip, 'PTR', 'IN' );
},
response => sub {
my $request_address = $_[ARG0]->[0];
my ( $net_dns_packet, $net_dns_errorstring ) = @{ $_[ARG1] };
unless ( defined $net_dns_packet ) {
print "$request_address: error ($net_dns_errorstring)\n";
$_[KERNEL]->yield('lookup');
return;
}
my @net_dns_answers = $net_dns_packet->answer();
unless ( scalar @net_dns_answers ) {
print "$request_address: no answer\n";
$_[KERNEL]->yield('lookup');
return;
}
foreach ( @net_dns_answers ) {
if ( $_->type eq 'PTR' ) {
( my $fqdn = $_->rdatastr ) =~ s/\.$//;
print "$request_address: $fqdn\n";
last;
}
}
$_[KERNEL]->yield('lookup');
},
}
);
$poe_kernel->run();
exit 0;
view raw dns-2.pl hosted with ❤ by GitHub