codesnips’s posterous

mysqldump

mysqldump -u DBUSER -p DBNAME > DBNAME.sql

Comments [0]

[Networking] Watching dhcp traffic

tcpdump -i <eth> -n port 67 and port 68

Comments [0]

[Ruby] Splat operator

expand array items into individual arguments.


>> ar
=> ["a", "b"]
>> ar.class
=> Array

>> def ar_in(*args)
>>    puts args, args.length
>> end

without splat
?> ar_in(ar)
a
b
1

this is what we want
>> ar_in('a', 'b')
a
b
2

which is possible by using the splat operator before the array

>> ar_in(*ar)
a
b
2

Comments [0]

[Ruby] Cut with ruby

ruby -a -ne 'puts $F[1], $F[2]'


-a will split the input into an array called $F. The above will print the second and the third item of the array (based on 0 index)

Comments [0]

[Ruby] Comment out every line in file

ruby -i.bak -pe '$_ = "#" + $_' *


-i.bak keeps the original file safe by making a backup

Comments [0]

[Ruby] Grep with Ruby

ruby -ne 'puts $_ if $_ =~ /text/' *


-n places an implicit while gets ... end block

gets(separator=$/)    => string or nil
------------------------------------------------------------------------
     Returns (and assigns to +$_+) the next line from the list of files
     in +ARGV+ (or +$*+), or from standard input if no files are present
     on the command line. Returns +nil+ at end of file. The optional
     argument specifies the record separator. The separator is included
     with the contents of each record. A separator of +nil+ reads the
     entire contents, and a zero-length separator reads the input one
     paragraph at a time, where paragraphs are divided by two
     consecutive newlines. If multiple filenames are present in +ARGV+,
     +gets(nil)+ will read the contents one file at a time.

        ARGV << "testfile"
        print while gets

     _produces:_

        This is line one
        This is line two
        This is line three
        And so on...

     The style of programming using +$_+ as an implicit parameter is
     gradually losing favor in the Ruby community.

Comments [0]

[Networking] Tcpdump capture traffic on port 8773 via eth1 interface

tcpdump -i eth1 -s0 -A -v port 8773

Comments [0]

[Networking] Tcpdump show all interfaces

tcpdump -D

Comments [0]

[Ruby] Generate IP addresses

(10..50).each { |n| ips << "10.1.1.#{n} " }

Comments [0]

[Java] jar unpacking and packing

jar xvf ../file.jar

jar cvf ../file.jar.patched 

Comments [0]