Fast ICMP scanner (Perl oneliner)

Jan 08
2008

[localhost]$

time perl -MNet::Ping -e '$p = Net::Ping->new();
@hosts = qw(
127.0.0.1
172.16.0.1
172.16.0.2
172.16.0.5
172.16.0.254);
#the significant detail
$p->{"timeout"} = 0.005;
for (@hosts) { print "$_ is ".($p->ping($_) ? "up" : "down").$/ }'

127.0.0.1 is up
172.16.0.1 is up
172.16.0.2 is down
172.16.0.5 is up
172.16.0.254 is down

real 0m0.062s
user 0m0.040s
sys 0m0.000s

Ruby one-liners (file manipulation)

Jul 20
2007
# number each line of a file
ruby -ne 'puts "#{$.}\t#{$_}"' file.txt
 
# print all non-blank lines
ruby -pe '$_.chomp.empty? and next' file.txt
 
# number and print all non-blank lines
ruby -ne '$_.chomp.empty? or print $.,"\t", $_' file.txt
 
# number and print each blank line
ruby -ne 'puts $. if $_.chomp.empty?' file.txt
 
# reverse order of lines (`tac` style)
ruby -e 'puts File.open($< .filename).readlines.reverse' file.txt
 
# print matched string from lines, matching the pattern
ruby -ne 'puts $_.scan(/^\w+/)' /etc/passwd
 
# triple space a file and reverse order of lines
ruby -e '$,="\n\n\n"; puts File.readlines($<.filename).reverse.join' file.txt
 
# print first line of a file (emulate 'head -1')
ruby -ne 'puts $_; break' file.txt
ruby -pe '$. == 1 or break' file.txt
 
# print last line of a file (emulates 'tail -1')
ruby -ne 'END{puts $_}' file.txt
 
# print last line number (emulates 'wc -l')
ruby -e 'loop{gets or break}; puts $.' file.txt
 
# print only lines that match a regular expression (emulates 'grep')
ruby -pe 'next if not /regex/' file.txt
 
# print only lines that do not match a regular expression (emulates 'grep -v')
ruby -pe 'next if /regex/' file.txt
 
# print section of file between two regular expressions, /^root/ and /^nobody/
ruby -ne 'puts $_ if /^root/../^nobody/' file.txt
 
# print file and remove duplicate, consecutive lines from a file (emulates 'uniq')
ruby -ne '$_.eql? $; or puts $_;$; = $_;' file.txt
 
# print file except for blank lines
ruby -pe 'next if $_.chomp.empty?' file.txt
ruby -pe 'next if /^\s*$/' file.txt
ruby -pe 'next if $_.split(/\S+/).size < 2' file.txt
 
# print file except for lines, starting with digit (unclear and inefficient)
ruby -pe 'next if (48..57).to_a.include?($_.split(//)[0][0])' file.txt
 
# delete all leading blank lines at top of file
ruby -pe '$,="$." if not $_.chomp.empty?; $, or next' file.txt
 
# print section of file from regex to end of file
ruby -pe '$,="$." if /regex/; $, or next' file.txt
 
# delete leading and trailing whitespace from each line
ruby -pe '$_.strip!.sub!(/$/, "\n")' file.txt
ruby -ne 'puts $_.strip! + $/' file.txt
 
# delete leading whitespace from the beginning of each line
ruby -ne 'puts $_.lstrip! || $_' file.txt
 
# convert DOS newlines (CR/LF) to Unix format (LF)
ruby -i -pe 'sub(/\r\n/, "\n")' file.txt

1..666

Jul 19
2007

irb console

$.=0; (1..666).to_a.inject(1){|res, i| res += i}.to_s.split(//).collect{|i| $. += i.to_i}; STDOUT.puts$

Calendar

September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  

Tags