Skip to content

Instantly share code, notes, and snippets.

View nevans's full-sized avatar

nicholas a. evans nevans

View GitHub Profile
@nevans
nevans / gist:330605
Created March 12, 2010 18:40
what IPs are trying to hack my server
sudo last -i -f /var/log/btmp |
awk '{ per[$3] += 1 } END { for (i in per) print per[i], i }' |
sort -n |
tail -15
@nevans
nevans / gist:335569
Created March 17, 2010 18:36
count files and disk usage
echo -e 'count\t size\t name'
for dir in *; do
echo -e `find "$dir" -type f | wc -l` '\t' \
`du -sh "$dir" | awk '{ print $1 }'` '\t' \
"$dir"
done | sort -n
ip addr show |
grep 'inet' |
awk '{ print $2 }' |
awk -F / '{ print $1 }' |
sort
Is there a way to get mustache to do the following?
@nevans
nevans / collapse_array_of_ranges.rb
Created August 26, 2010 01:39
Collapse an array of ranges into the union of their ranges
def collapse_array_of_ranges(array)
# simplify, by sorting on range.begin
array.sort_by(&:begin).inject([]) do |a, r|
# compared with last range...
last_range = a[-1]
if last_range && last_range.include?(r.begin)
# ignore range if it is completely inside of last range
unless(last_range.include?(r.end))
# this range overlaps last range,
# thus, pull off last range and merge them
@nevans
nevans / environment.rb
Created September 3, 2010 15:23
Paperclip on rails 1.2.6
# stick this code at the bottom of your config/environment.rb
# (unless, of course, you already have something similar)
initializer_glob = File.join(RAILS_ROOT, "config/initializers/*.rb")
initializers = Dir[initializer_glob].map {|f| File.expand_path(f) }.sort
initializers.each {|f| require f }
@nevans
nevans / eydeploy.rb
Created January 6, 2011 15:36
how to use private git repos with bundler at Engine Yard
# put this into your config/eydeploy.rb
def bundle
if File.exist?("#{c.release_path}/Gemfile")
info "~> Gemfile detected, bundling gems"
lockfile = File.join(c.release_path, "Gemfile.lock")
bundler_installer = if File.exist?(lockfile)
get_bundler_installer(lockfile)
else
warn_about_missing_lockfile
@nevans
nevans / gist:1013141
Created June 7, 2011 20:56
some silly tracert diagnostics...
ip_prefix=xxx.yyy.zzz
count=4
for (( i=96; i <= 127; i=i+1 )); do ip=$ip_prefix.$i; echo Trying $ip; mtr -nr -c $count $ip; done
# or
for (( i=96; i <= 127; i=i+1 )); do ip=$ip_prefix.$i; echo Trying $ip; arping -c $count -w $count $ip; done
# ...
@nevans
nevans / method_vs_constant_lookup.rb
Created August 19, 2011 15:50
demonstrating the difference between method and constant lookup
#!/usr/bin/env ruby
module TopLevel
CONST_D = :d_from_top_level
module MiddleLevel
class BottomLevel
CONST_A = :foo
CONST_B = :bar
CONST_C = :c_from_bottom_level
@nevans
nevans / private_module_methods.rb
Created August 19, 2011 15:20
My favorite module methods pattern, and how it works with private
#!/usr/bin/env ruby
module M
extend self
def foo
"hello world"
end
private