19Jul/110
perl regexp – threat whole string as one line
s - Treat the whole string as one line, so that even /./ will match a "newline" character.
#!/usr/bin/perl
my $multiline =
"In the town where I was born,\n" .
"Lived a man who sailed to sea,\n" .
"And he told us of his life,\n" .
"In the land of submarines.";
if ($multiline =~ /born,.Lived/s) {
print "found\n"; # found in deed
} else {
print "not found\n";
}
20Feb/111
iwlist scan perl wrapper
The output of /sbin/iwlist scan is too much for me in most of the cases: I just want to know which WiFis are present, quality and open/passneeded state.
So here is a small perl script for it, the ESSIDs printed in descending order of quality which changed from 1-70 to 1-100.
#!/usr/bin/perl use warnings; use strict; open(LIST, "/sbin/iwlist scan 2>&1 |") or die "Failed: $!\n"; my %wifis; my $essid; while () { if (/ESSID\:\"(.*)\"/) { $essid = $1; } elsif (/Quality=(\d*)\/70/) { $wifis{$essid}->{"quality"} = $1; } elsif (/Encryption key\:(\S*)/) { $wifis{$essid}->{"key"} = $1; } } sub by_quality { $wifis{$b}->{"quality"} <=> $wifis{$a}->{"quality"}; } print "\n"; foreach $essid ( sort by_quality keys %wifis) { printf '%*s %*s %-d', 30, $essid, 6, $wifis{$essid}->{"key"}=~/on/? "Pass" : "Open" , int($wifis{$essid}->{"quality"}) / 70.0 * 100; print "\n"; } print "\n";
A sample output I got at my flat:
cs0rbagomba@ramen ~ $ wifi_list
gara_dlink Pass 71
TP-Link01 Open 54
zaa Pass 50
DBnet Pass 50
anzo Pass 47
3Com Pass 22
TP-LINK_9D27F4 Pass 21
TP-LINK_TOMEC Pass 21
TNT Pass 17
TimeCapsule Pass 15
hpsetup Open 14
Pannon Cargo Pass 12
Airlive Pass 10
TP-LINK_DA3008 Open 10
Open 8
Szeretetre melto internet Pass 8
KZSNET Pass 8
CEO_iroda Pass 8
Vani2 Pass 5
GIGABYTE Open 4
csikos Pass 2
TP-LINK_E6395C Pass 2
Dante_88 Pass 2
RG60SE Open 2
default Open 2
WIFI99 Pass 1
Zsoka Pass 1
IKO Pass 1
cs0rbagomba@ramen ~ $
PS: TP-Link01 Open - sharing is caring 🙂 default settings rulz
12Aug/090
String templates with Perl
Here is a nice thing with perl.
- You have a string template, like an e-mail, with fields to change.
- This fields are stored in a CVS file.
- Perl changes this fields with hashes and regular expressions in no time.
Note: the field names are the keys in the hash, lines of the CVS are an array with hash refs.
#!/usr/bin/perl
use warnings;
use strict;
my @data = ();
# data.csv:
# bela,fired
# julcsi,killed
# jani,promoted
my $string_template = <<EOF;
Dear <name>,
You have been <action>.
Br: Someone.
EOF
# CSV to hash
open FILE, "data.csv" or die $!;
while (my $line = ) {
my %temp_hash = ();
($temp_hash{"name"}, $temp_hash{"action"}) = split (",", $line);
chomp $temp_hash{"action"};
push @data, \%temp_hash;
}
close FILE;
# replace & print
foreach (@data) {
my $s = $string_template;
$s=~s/<(.*?)>/$$_{$1}/g;
print "$s\n";
}