util

by woomoo
0 deployments · 0 still active · last rev. 4 years ago

util functions

Compatible with: Ubuntu 14.04 LTS
						#!/bin/bash
#
# StackScript Bash Library
#
# Copyright (c) 2010 Linode LLC / Christopher S. Aker <caker@linode.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# * Neither the name of Linode LLC nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.

###########################################################
# System
###########################################################

function system_update {
  apt-get update
  apt-get -y install aptitude
  aptitude -y full-upgrade
}

function system_primary_ip {
  # returns the primary IP assigned to eth0
  echo $(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }')
}

function get_rdns {
  # calls host on an IP address and returns its reverse dns

  if [ ! -e /usr/bin/host ]; then
    aptitude -y install dnsutils > /dev/null
  fi
  echo $(host $1 | awk '/pointer/ {print $5}' | sed 's/\.$//')
}

function get_rdns_primary_ip {
  # returns the reverse dns of the primary IP assigned to this system
  echo $(get_rdns $(system_primary_ip))
}


###########################################################
# Other niceties!
###########################################################

function goodstuff {
  # Installs the REAL vim, wget, and enables color root prompt and the &quot;ll&quot; list long alias

  aptitude -y install wget vim bash-completion
  sed -i -e 's/^#PS1=/PS1=/' /root/.bashrc # enable the colorful root bash prompt
  sed -i -e &quot;s/^#alias ll='ls -l'/alias ll='ls -al'/&quot; /root/.bashrc # enable ll list long alias <3
}


###########################################################
# Utility functions
###########################################################

function restart_services {
  # restarts services that have a file in /tmp/needs-restart/

  for service in $(ls /tmp/restart-* | cut -d- -f2-10); do
    /etc/init.d/$service restart
    rm -f /tmp/restart-$service
  done
}

function random_string {
  if [ ! -n &quot;$1&quot; ];
    then LEN=20
    else LEN=&quot;$1&quot;
  fi

  echo $(</dev/urandom tr -dc A-Za-z0-9 | head -c $LEN) # generate a random string
}

function log {
  echo "$1 `date '+%D %T'`" >> /root/log.txt
}