commit 86ee27cb9821ebdc90f55430e4fcff5955beac98 Author: Sami Samhuri Date: Tue Nov 10 23:12:38 2009 -0800 [NEW] Initial commit of bin diff --git a/configurize b/configurize new file mode 100755 index 0000000..a25cb24 --- /dev/null +++ b/configurize @@ -0,0 +1,26 @@ +#!/bin/sh + +CONFIG="$HOME/config" + +NUMFILES=$# +while [ $# -gt 0 ]; do + FILE="$1" + shift + NEWFILE="${FILE#.}" + echo " * Configurizing $NEWFILE ..." + mv "$FILE" "$CONFIG/$NEWFILE" + ln -s "$CONFIG/$NEWFILE" "$FILE" + cd "$CONFIG" + git add "$NEWFILE" + git commit -m "[NEW] Configurized '$NEWFILE'" + cd - >/dev/null +done + +echo " * $NUMFILES new files/directories" + +echo " * Pushing configs to remote repository ... " +cd "$CONFIG" +git push origin master +cd - >/dev/null + +echo " * Done" diff --git a/drop b/drop new file mode 100755 index 0000000..bae4af2 --- /dev/null +++ b/drop @@ -0,0 +1,2 @@ +#!/bin/sh +[[ x"$1" = x ]] && cp -R . ~/Dropbox/${PWD##*/} || cp -R "$@" ~/Dropbox/ diff --git a/newscript b/newscript new file mode 100755 index 0000000..c35e2db --- /dev/null +++ b/newscript @@ -0,0 +1,57 @@ +#!/bin/sh + +[[ $# -lt 1 ]] && exit 1 + +SCRIPT_DIR=${HOME}/bin + +TYPE=$1 +case "$TYPE" in + -bash) + TYPE=".bash" + SHEBANG="#!/bin/bash" + shift + ;; + -pl) + TYPE=".pl" + SHEBANG="#!/usr/bin/perl -w\nuse strict;" + shift + ;; + -py) + TYPE=".py" + SHEBANG="#!/usr/bin/python" + shift + ;; + -sh) + TYPE=".sh" + SHEBANG="#!/bin/sh" + shift + ;; + -zsh) + TYPE=".zsh" + SHEBANG="#!/bin/zsh" + shift + ;; + *) + TYPE="" + ;; +esac + +while [[ -n ${1} ]] ; do + SCRIPT=${SCRIPT_DIR}/${1}${TYPE} + if [[ -n ${TYPE} ]] && [[ ! -e ${SCRIPT} ]]; then + echo -e "${SHEBANG}" > ${SCRIPT} + chmod +x ${SCRIPT} + elif [[ ! -e ${SCRIPT} ]]; then + touch ${SCRIPT} + chmod +x ${SCRIPT} + elif [[ -w ${SCRIPT} ]] ; then + echo "Script ${SCRIPT} already exists, opening to edit anyways." >&2 + else + echo "Script ${SCRIPT} is not writable, ignoring." >&2 + SCRIPT="" + fi + SCRIPTS="${SCRIPTS} ${SCRIPT}" + shift +done + +${EDITOR} ${SCRIPTS} diff --git a/scp-resume.sh b/scp-resume.sh new file mode 100755 index 0000000..2b83710 --- /dev/null +++ b/scp-resume.sh @@ -0,0 +1,108 @@ +#!/bin/sh +# +# Speed improvements by using blocks by nitro.tm@gmail.com +# +# This script assumes that you have access to the 'dd' utility +# on both the local and remote host. + +# dd transfer blocksize (8192 by default) +blocksize=8192 + +usage() +{ + echo + echo "Usage: `basename $0` -u(pload) \$localfile \$remotefile [\$sshargs]" + echo " `basename $0` -d(ownload) \$remotefile \$localfile [\$sshargs]" + echo + echo " \$remotefile should be in the scp format, i.e.: [user@]host:filename" + echo " \$sshargs are option further ssh options such as a port specification" + echo " (-p 1234) or use of compression (-C)" + echo + echo " -u:" + echo " \$remotefile may be [user@]host: for uploading to your remote home directory" + echo " -d:" + echo " \$localfile may be a period (.) when downloading a remote file to the" + echo " current working directory." + echo + exit 1 +} + +[ -z "$1" -o -z "$2" -o -z "$3" ] && usage + +option=$1 +case $option in + -[uU]*) + localfile=$2 + remote=$3 + shift 3 + sshargs="$*" + + userhost=${remote%:*} + remotefile=${remote#*:} + + if [ ! -f "$localfile" ]; then + echo "!! File not found: $localfile" + usage + fi + if [ x"$userhost" = x"$remote" ]; then usage; fi + if [ x"$remotefile" = x"$remote" -o -z "$remotefile" ]; then remotefile=`basename "$localfile"`; fi + + echo "==>> Getting size of remote file:" + localsize=`ls -l "${localfile}" | awk '{ print $5 }'` + remotesize=`ssh $sshargs "$userhost" "[ -f \"${remotefile}\" ] && ls -l \"${remotefile}\"" | awk '{ print $5 }' ` + + [ -z "$remotesize" ] && remotesize=0 + echo "=> Remote filesize: $remotesize bytes" + + if [ $localsize -eq $remotesize ]; then + echo "=> Local size equals remote size, nothing to transfer." + exit 0; + fi + + remainder=$((remotesize % blocksize)) + restartpoint=$((remotesize - remainder)) + blockstransferred=$((remotesize / blocksize)) + + echo "=> Resuming upload of '$localfile'" + echo " at byte: $restartpoint ($blockstransferred blocks x $blocksize bytes/block)," + echo " will overwrite the trailing $remainder bytes." + + dd bs=$blocksize skip=$blockstransferred "if=${localfile}" | \ + ssh $sshargs "$userhost" "dd bs=$blocksize seek=$blockstransferred of=\"$remotefile\"" + + echo "done." + ;; + -[dD]*) + localfile=$3 + remote=$2 + shift 3 + sshargs="$*" + + userhost=${remote%:*} + remotefile=${remote#*:} + + if [ x"$localfile" = x"." ]; then localfile=`basename "$remotefile"`; fi + if [ ! -f "$localfile" ]; then + localsize=0; + else + localsize=`ls -l "${localfile}" | awk '{ print $5 }'` + fi + [ x"$remotefile" = x"$remote" ] && usage + [ -z "$localsize" ] && localsize=0 + + remainder=$((localsize % blocksize)) + restartpoint=$((localsize - remainder)) + blockstransferred=$((localsize / blocksize)) + + echo "=> Resuming download of '$localfile'" + echo " at byte: $restartpoint ($blockstransferred blocks x $blocksize bytes/block)" + echo " filesize: $localsize; will overwrite the trailing $remainder bytes." + ssh $sshargs "$userhost" "dd bs=$blocksize skip=$blockstransferred \"if=${remotefile}\"" | + dd bs=$blocksize seek=$blockstransferred "of=$localfile" + + ;; + *) + usage + ;; +esac + diff --git a/watchit.pl b/watchit.pl new file mode 100755 index 0000000..367ad85 --- /dev/null +++ b/watchit.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -w +use strict; + +sub mail_status { + $date = `date +%Y%m%d-%h:%m`; + open(MAIL, "|/usr/lib/sendmail -t"); + print MAIL "To: $info{to}\n"; + print MAIL "From: $info{from}\n"; + print MAIL "Subject: $info{host} status report for $date\n"; + print MAIL "$info{host} has been up:\n$info{uptime}\n\n"; + print MAIL "memory usage (mb):\n$info{mem}\n\n"; + print MAIL "disk usage:\n$info{disk}\n\n"; + print MAIL "service status:\n$info{services}\n\n"; + close (MAIL); +} + +my ($year, $month, $day) = (`date +%Y`, `date +%m`, `date +%d`); +my $logDir = "/var/log"; +my @logFiles = qw( messages apache2/access_log apache2/error_log ); +my @targetFiles = qw( messages apache_access apache_error ); +my $remoteHost = "home.nofxwiki.net"; +my $remoteDir = "/home/sjs/log/nofxwiki.net/$year/$month"; +my $filePrefix = "$day-"; +my @services = qw( apache2 clamd courier-authlib courier-imapd courier-imapd-ssl iptables mysql spamd sshd ); + +my $file; +my $remoteFile; +for (my $i = 0; $i < @logFiles; $i++) { + $file = $logFiles[$i]; + $remoteFile = $torgetFiles[$i]; + open $file, "tail -f $logDir/$file |" \ + or die "Could not open 'tail -f $file', bailing!\n"; + open "ssh-$file", "| ssh $remoteHost 'cat >> $remoteDir/$remoteFile'" \ + or die "Could not open 'ssh $remoteHost 'cat >> $remoteDir/$remoteFile\n'', bailing!\n"; +} + +my $ticks = 0; +my %info; +while (1) { + foreach my $file (@logFiles) { + while (my $line = <$file>) { + print "ssh-$file" $lines; + } + } + + sleep 300; # 5 minutes + $ticks++; + if ($ticks == 72) { # 360 min = 6 hr + $ticks = 0; + $info{host} = `hostname --fqdn`; + $info{uptime} = `w`; + $info{mem} = `free -m`; + $info{disk} = `df -h`; + + foreach my $svc (@services) { + open SVC, "/etc/init.d/$svc status |"; + @info{services} += ; + close SVC; + } + &mail_status(%info); + } +} \ No newline at end of file diff --git a/whatismyip b/whatismyip new file mode 100755 index 0000000..c521f3f --- /dev/null +++ b/whatismyip @@ -0,0 +1,13 @@ +#!/usr/bin/env ruby + +require 'open-uri' + +open('http://whatismyip.com') do |f| + puts "Open failed: #{f.status.inspect}" unless f.status.first == "200" + # WhatIsMyIP.com - 207.81.136.119 + title = f.gets while title !~ /WhatIsMyIP\.com - (\d[\d.]+)</ + ip = $1.chomp + ENV['SHELL'] = '/bin/bash' + `echo -n "#{ip}" | pbcopy` + puts ip +end