diff options
author | Nathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk> | 2015-09-18 11:51:28 +0100 |
---|---|---|
committer | Nathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk> | 2015-09-18 11:51:28 +0100 |
commit | b0a14e013b318fa87714414b8ece95780527bddf (patch) | |
tree | a9427383ab6e38a4421c9c4313043f6dfcb8e90e |
Initial commit
-rwxr-xr-x | apt-stuff | 6 | ||||
-rwxr-xr-x | botchcli | 17 | ||||
-rwxr-xr-x | ciscodomainshex | 16 | ||||
-rwxr-xr-x | costvm | 68 | ||||
-rwxr-xr-x | count | 93 | ||||
-rwxr-xr-x | ddp | 70 | ||||
-rwxr-xr-x | equote | 31 | ||||
-rwxr-xr-x | geneni | 50 | ||||
-rwxr-xr-x | genrdns | 48 | ||||
-rwxr-xr-x | macto | 17 | ||||
-rwxr-xr-x | memusg | 46 | ||||
-rwxr-xr-x | mtu | 69 | ||||
-rwxr-xr-x | pullgits | 15 |
13 files changed, 546 insertions, 0 deletions
diff --git a/apt-stuff b/apt-stuff new file mode 100755 index 0000000..c55a887 --- /dev/null +++ b/apt-stuff @@ -0,0 +1,6 @@ +#!/bin/bash + +sudo apt-get update +sudo apt-get -y upgrade +sudo apt-get -y --purge autoremove +sudo apt-get clean diff --git a/botchcli b/botchcli new file mode 100755 index 0000000..5dfa0f7 --- /dev/null +++ b/botchcli @@ -0,0 +1,17 @@ +#!/bin/bash + +SUBCHAR=! + +[[ "$*" =~ $SUBCHAR ]] && WORK=$* || WORK="$* $SUBCHAR" + +echo -n "[botchcli] $WORK> " +while read LINE; do + if [ "x$LINE" == "x" ] ; then + echo -n + elif [ "x${LINE,,}" == "xquit" ] ; then + exit 0 + else + ${WORK/$SUBCHAR/$LINE} + fi + echo -n "[botchcli] $WORK> " +done diff --git a/ciscodomainshex b/ciscodomainshex new file mode 100755 index 0000000..d0c4982 --- /dev/null +++ b/ciscodomainshex @@ -0,0 +1,16 @@ +#!/usr/bin/python + +# From http://www.perkin.org.uk/posts/serving-multiple-dns-search-domains-in-ios-dhcp.html + +import sys + +hexlist = [] +for domain in sys.argv[1:]: + for part in domain.split("."): + hexlist.append("%02x" % len(part)) + for c in part: + hexlist.append(c.encode("hex")) + hexlist.append("00") + +print "".join([(".%s" % (x) if i and not i % 2 else x) \ + for i, x in enumerate(hexlist)]) @@ -0,0 +1,68 @@ +#!/usr/bin/env ruby +# encoding: utf-8 + +if ARGV.length == 0 then + puts "Usage: costvm CORES MEMORY DISKS" + exit +end + +CORES = ARGV.shift.chomp.to_i +MEM = ARGV.shift.chomp.to_i +DISKS = ARGV.shift.chomp.split(',') + +if CORES > 16 then + puts "Invalid machine. Too many cores: #{CORES}." + exit 1 +end + +if MEM > 180 then + puts "Invalid machine. Too much memory: #{MEM} GiB." + exit 1 +end + +if DISKS.length > 8 then + puts "Invalid machine. Too many disks: #{DISKS.length}." + exit 1 +end + +memmin = (CORES - 1) * 4 + +def max(a,b) + if a > b then + return a + else + return b + end +end + +cost = max(memmin, MEM) * 10 + +firstdisk = true + +DISKS.each do |disk| + grade, size = disk.split(':') + if size.nil? then + size = grade + grade = 'sata' + end + + size = size.to_i + + case grade + when 'sata' + if firstdisk then + cost += (max((size - 25), 0)/10.0).ceil * 2 + firstdisk = false + else + cost += (size/10.0).ceil * 2 + end + when 'sas' + cost += (size/5.0).ceil * 2 + when 'ssd' + cost += (size/0.5).ceil * 2 + when 'archive' + cost += (size/50.0).ceil * 2 + end +end + +puts "Machine cost: £#{cost}.00" @@ -0,0 +1,93 @@ +#!/usr/bin/env ruby + +require 'optparse' + +options = { + :seperator => :lines, + :sort => :none, + :reverse => false +} + +parser = OptionParser.new do |opt| + opt.banner = "Usage: count" + opt.on("-s", "--spaces", "Seperate records with spaces rather than newlines") do + options[:seperator] = :spaces + end + opt.on("-r", "--sort-by-record", "Sort output by record") do |arg| + if options[:sort] == :none then + options[:sort] = :record + else + puts "Flags -c and -l are incompatible" + exit 1 + end + end + opt.on("-c", "--sort-by-count", "Sort output by count") do |arg| + if options[:sort] == :none then + options[:sort] = :count + else + puts "Flags -c and -l are incompatible" + exit 1 + end + end + opt.on("-v", "--reverse", "Reverse sort") do + options[:reverse] = true + end + opt.on("-q", "--summary", "Print total only") do + options[:summary] = true + end + opt.on("-h", "--help", "Print usage") do + puts opt.banner + puts opt.summarize + exit 0 + end +end + +parser.parse! + +if options[:seperator] == :lines then + lines = $stdin.readlines.map(&:chomp) +else + lines = $stdin.readlines[0].chomp.split +end + +if options[:summary] then + puts "[#{lines.length}] Total" + exit 0 +end + +counts = Hash.new + +lines.each do |line| + if counts.include? line then + counts[line] += 1 + else + counts[line] = 1 + end +end + +width = Math.log10(counts.values.max).ceil + +sorted = case options[:sort] + when :none + counts.to_a + when :count + counts.sort_by{|record, count| count} + when :record + counts.sort_by{|record, count| record} + end + +sorted.reverse! if options[:reverse] + +sorted.each do |entry| + puts "[%#{width}d] %s" % entry.reverse +end + +maxrec = counts.keys.map(&:length).max + +(maxrec + width + 3).times do + print '-' +end +puts + +puts "[#{lines.length}] Total" +exit 0 @@ -0,0 +1,70 @@ +#!/usr/bin/env ruby + +cmd = ['dd'] +countarg = nil + +def decr(mult) + return { + 'c' => ['c', 1], + 'w' => ['c', 2], + 'b' => ['c', 512], + 'kB' => ['w', 500], + 'K' => ['w', 512], + 'MB' => ['kB', 1000], + 'M' => ['K', 1024], + 'GB' => ['MB', 1000], + 'G' => ['M', 1024], + 'TB' => ['GB', 1000], + 'T' => ['G', 1024], + 'PB' => ['TB', 1000], + 'P' => ['T', 1024], + 'EB' => ['PB', 1000], + 'E' => ['P', 1024], + 'ZB' => ['EB', 1000], + 'Z' => ['E', 1024], + 'YB' => ['ZB', 1000], + 'Y' => ['Z', 1024], + }[mult] +end + +def parsecount(countarg) + if countarg =~ /^(\d+)(c|w|b|kB|K|[MGTPEZY]B?)$/ + if $1 == '1' then + unit, num = decr($2) + count = "count=1#{unit}" + seeks = (0...num).map do |i| + "seek=#{i}#{unit}" + end + else + count = "count=1#{$2}" + seeks = (0...($1.to_i)).map do |i| + "seek=#{i}#{$2}" + end + end + return count, seeks + else + puts "BAD COUNT" + exit 1 + end +end + +ARGV.each do |arg| + if arg =~ /count=(.*)/ then + countarg=$1 + else + cmd << arg + end +end + +if countarg.nil? then + run = cmd.join(' ') + puts "*** " + run + system(run) +else + count, seeks = parsecount(countarg) + seeks.each do |seek| + run = (cmd + [seek, count]).join(' ') + puts "*** " + run + system(run) + end +end @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby + +LINELENGTH = (ARGV.shift || 72).to_i + +lines = $stdin.readlines + +lines = lines.map { |line| + if line.length > (LINELENGTH-2) then + newlines = [] + while line.length > (LINELENGTH-2) do + i = LINELENGTH-2 + while line[i] != " " do + i -= 1 + end + newlines << line[0...i] + line = line[i..-1] + end + newlines << line + newlines + else + line + end +} + +lines = lines.flatten + +lines = lines.map { |line| + "> #{line}" +} + +puts lines @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby + +def ip2num(ip) + ipa = ip.split(".").map(&:to_i) + return (ipa[0] * (256 ** 3)) + (ipa[1] * (256 ** 2)) + (ipa[2] * 256) + ipa[3] +end + +def num2ip(ipi) + outa = [] + outa[3] = ipi % 256 + outa[2] = (ipi / 256) % 256 + outa[1] = (ipi / (256 ** 2)) % 256 + outa[0] = (ipi / (256 ** 3)) % 256 + return outa.map(&:to_s).join(".") +end + +def boundarycheck(ip, cidr) + bound = 2**(32-cidr) + return (ip2num(ip) % bound) == 0 +end + +def addip(ip, x) + ipi = ip2num(ip) + ipi += x + return num2ip(ipi) +end + +if ARGV.length == 0 then + puts "Usage: geneni ip[/cidr] iface [alias start]" + exit +end + +IPCIDR = ARGV.shift.split("/") +IP = IPCIDR[0] +CIDR = IPCIDR[1].nil? ? 32 : IPCIDR[1].to_i +IFACE = ARGV.shift +START = (ARGV.shift or "1").to_i + +unless boundarycheck(IP, CIDR) then + puts "Invalid CIDR Boundary" + exit 1 +end + +(2**(32-CIDR)).times do |x| + puts "auto #{IFACE}:#{x+START}" + puts "iface #{IFACE}:#{x+START} inet static" + puts "\taddress #{addip(IP, x)}" + puts "\tnetmask #{num2ip(("1"*CIDR + "0"*(32-CIDR)).to_i(2))}" + #puts "\tnetmask 255.255.255.255" +end @@ -0,0 +1,48 @@ +#!/usr/bin/env ruby + +def ip2num(ip) + ipa = ip.split(".").map(&:to_i) + return (ipa[0] * (256 ** 3)) + (ipa[1] * (256 ** 2)) + (ipa[2] * 256) + ipa[3] +end + +def num2ip(ipi) + outa = [] + outa[3] = ipi % 256 + outa[2] = (ipi / 256) % 256 + outa[1] = (ipi / (256 ** 2)) % 256 + outa[0] = (ipi / (256 ** 3)) % 256 + return outa.map(&:to_s).join(".") +end + +def boundarycheck(ip, cidr) + bound = 2**(32-cidr) + return (ip2num(ip) % bound) == 0 +end + +def addip(ip, x) + ipi = ip2num(ip) + ipi += x + return num2ip(ipi) +end + +def reverseip(ip) + return ip.split('.').reverse.join('.') +end + +if ARGV.length == 0 then + puts "Usage: genrdns ip[/cidr]" + exit +end + +IPCIDR = ARGV.shift.split("/") +IP = IPCIDR[0] +CIDR = IPCIDR[1].nil? ? 32 : IPCIDR[1].to_i + +unless boundarycheck(IP, CIDR) then + puts "Invalid CIDR Boundary" + exit 1 +end + +(2**(32-CIDR)).times do |x| + puts "#{reverseip(addip(IP, x))}.in-addr.arpa" +end @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby + +TO = ARGV.shift.chomp.downcase +MAC = ARGV.shift.chomp.gsub(/[.:]/, '') +UPPER = ARGV.shift ? true : false + +if MAC.match(/[0-9a-fA-F]{12}/).nil? then + puts "Not a valid MAC" + exit 1 +end + +case TO + when 'cisco' + puts (UPPER ? MAC.upcase : MAC).scan(/[A-Fa-f0-9]{4}/).join('.') + when 'canonical' + puts (UPPER ? MAC.upcase : MAC).scan(/[A-Fa-f0-9]{2}/).join(':') +end @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# memusg -- Measure memory usage of processes +# Usage: memusg COMMAND [ARGS]... +# +# Author: Jaeho Shin <netj@sparcs.org> +# Created: 2010-08-16 +set -um + +# check input +[ $# -gt 0 ] || { sed -n '2,/^#$/ s/^# //p' <"$0"; exit 1; } + +# TODO support more options: peak, footprint, sampling rate, etc. + +pgid=`ps -o pgid= $$` +# make sure we're in a separate process group +if [ $pgid = $(ps -o pgid= $(ps -o ppid= $$)) ]; then + cmd= + set -- "$0" "$@" + for a; do cmd+="'${a//"'"/"'\\''"}' "; done + exec bash -i -c "$cmd" +fi + +# detect operating system and prepare measurement +case `uname` in + Darwin|*BSD) sizes() { /bin/ps -o rss= -g $1; } ;; + Linux) sizes() { /bin/ps -o rss= -$1; } ;; + *) echo "`uname`: unsupported operating system" >&2; exit 2 ;; +esac + +# monitor the memory usage in the background. +( +peak=0 +while sizes=`sizes $pgid` +do + set -- $sizes + sample=$((${@/#/+})) + let peak="sample > peak ? sample : peak" + sleep 0.1 +done +echo "memusg: peak=$peak" >&2 +) & +monpid=$! + + +# run the given command +exec "$@" @@ -0,0 +1,69 @@ +#!/bin/bash + +function actually_just_show { + echo "The MTU of $iface is $nowmtu." + exit 0 +} + +function get_default_iface { + iface=$(ip a | grep "state UP" | cut -d: -f2 | tr -d '[[:space:]]') + if [ "x" == "x$iface" ] + then + echo "No active iface" + exit 1 + fi +} + +function get_current_mtu { + nowmtu=$(</sys/class/net/$iface/mtu) +} + +function get_new_mtu { + case "$nowmtu" in + 1500) mtu=1492 + ;; + *) mtu=1500 + ;; + esac +} + +function guess_at_remainder { + for i in $@; do + case "$i" in + show) actually_just_show + ;; + [0-9]*) mtu=$i + ;; + [a-zA-Z]*) iface=$i + ;; + esac + done +} + +OPTIND=1 + +get_default_iface +get_current_mtu +get_new_mtu + +while getopts "i:m:" opt; do + case "$opt" in + i) iface=$OPTARG + ;; + m) mtu=$OPTARG + ;; + esac +done + +shift $((OPTIND-1)) + +[ "$1" = "--" ] && shift + +guess_at_remainder $@ + +echo -n "Set MTU of $iface to $mtu? [Y/n] " +read yn + +if [ "x${yn,,}" != "xn" ]; then + echo $mtu | sudo tee "/sys/class/net/$iface/mtu" > /dev/null +fi diff --git a/pullgits b/pullgits new file mode 100755 index 0000000..7da8eb6 --- /dev/null +++ b/pullgits @@ -0,0 +1,15 @@ +#!/bin/bash + +WD=$([ "x" == "x$1" ] && echo $PWD || echo $1) + +GITS=$(find $WD -type d | egrep '\.git$') + +echo $GITS + +exit 0 + +for i in $GITS +do + cd $i + git pull +done |