Archive for 2011

Getting colors to work for ls and vim on Solaris 11

Just some quick notes, for myself and in case someone else ever wants this. In this day and age, I hate having to setup color on terminals ….colors make things very easy to see, quickly. So here are my notes so i don’t have to go spend time figuring it out yet again lol.

in your .profile or .bashrc
export TERM=xtermc
alias ls=”/usr/gnu/bin/ls -lah –color=auto”

in your .vimrc
syntax on
set showmatch
set bg=dark
set incsearch
set tabstop=4
set shiftwidth=4
set expandtab
set bs=2

bs=2 makes your backspace erase instead of inserting weird chars
syntax=on will enable color
the rest of it is just my personal settins in vim :)

My vmware workstation Archlinux Desktop

Host OS is Windows 7, running vmware workstation, and i’m 99% of the time in my ArchLinux virtual machine … and I love it.

Anyway, here is a screenshot of my current desktop, xfce4.

Archlinux – fixing vmware errors in log

 

 

When using open-vmware-tools, I was having the following errors reported every 30 seconds.
Below is how I fixed it.

[root@ArchLinux vmware-tools]# tail -f /var/log/messages.log
Sep 28 13:18:46 localhost vmsvc[696]: [ warning] [vmsvc] HostinfoOSData: Error: no distro file found
Sep 28 13:18:46 localhost vmsvc[696]: [ warning] [guestinfo] Failed to get OS info.
Sep 28 13:19:16 localhost vmsvc[696]: [ warning] [vmsvc] HostinfoReadDistroFile: Cannot work with empty file.
Sep 28 13:19:16 localhost vmsvc[696]: [ warning] [vmsvc] HostinfoReadDistroFile: Cannot work with empty file.
Sep 28 13:19:16 localhost vmsvc[696]: [ warning] [vmsvc] HostinfoOSData: Error: no distro file found
Sep 28 13:19:16 localhost vmsvc[696]: [ warning] [guestinfo] Failed to get OS info.
Sep 28 13:19:16 localhost vmsvc[696]: [ warning] [vmsvc] HostinfoReadDistroFile: Cannot work with empty file.
Sep 28 13:19:16 localhost vmsvc[696]: [ warning] [vmsvc] HostinfoReadDistroFile: Cannot work with empty file.
Sep 28 13:19:16 localhost vmsvc[696]: [ warning] [vmsvc] HostinfoOSData: Error: no distro file found
Sep 28 13:19:16 localhost vmsvc[696]: [ warning] [guestinfo] Failed to get OS info.

I simply put the following into a file located at:  /etc/release

ArchLinux Kernel 3.x

That stopped the error messages from showing up in /var/log/message.log

ArchLinux on VMware WorkStation on WinXP (Host)

I love my job, I really do.  However I hate that I have to run windows to administer unix/linux machines (just seem counter intuitive ya know ?).  Ah but in the corporate world, you have to bend to the will of the masters.  They are ok with running Linux in a vmware setting though (I know, I know, I know … ).  Enter vmware workstation.  I shelled out the money myself for vmware workstation so that I could run Linux on the desktop.

So far, it’s running very well…and I don’t have a powerhouse machine by todays standards by any means… only 2 gigs of ram in this machine.  For anyone thinking about running Arch on VmWare Workstation, I say go for it, it works great. The Arch Wiki has all the information you really need, albeit in perhaps a few places.   I tried various wm and desktop setups, settled with KDE.  I know, alot of the Arch peeps think “KDE” and snicker… but it works, and I have all the tools I need to get my work done :)  I’ve used Arch at home for about 5 years now, on servers and in a business enviroment.  I love using Arch with vmware center, in a server capacity. vmware center makes it simple to clone servers and with a few scripts, it’s great. Taking snapshots before “updating” makes updates painless and worry free, vmware workstation offers that same feature.  Cost for Vmware Workstation, $139.

Obligatory link: http://store.vmware.com/store/vmware/en_US/DisplayProductDetailsPage/ThemeID.2485600/productID.221027300/?SRC=EM_11Q3_CHRISTMASINJULY_WK_BUY&elq=e39f3a6b9a594c4ab6dc019650eda9fa   CODE: PREHOLSALE  Good through midnight Aug 4th.

My setup has dual monitors, and I have Archlinux spanning both monitors full screened, now, I can just pretend that M$ OS doesn’t really run on my machine… hey, I’m not proud.  :D

Bash – some .bashrc functions

# bu – A quick backup function

bu () { cp $1 ~/Backups/${1}-`date +%Y%m%d%H%M`.backup ; }

# hs – History Search

hs() {
history | grep ${1} | uniq –skip-fields=1 | sort -biz | uniq –skip-fields=2
}

# stock – Stock information search.

stock ()
{
lynx -dump “http://www.google.com/finance?client=ob&q=${1}” \
| sed ‘s/.*’]’//’ | perl -00ne “print if /Watch this stock/i”  \
| sed ‘s/Watch this stock//’ | sed ‘s/Disclaimer//’  \
| sed ‘/^$/d’ | sed ‘s/Currency in USD//’
}

# smallurl – create a shorter url link.

smallurl ()
{
lynx –dump http://api.fwd4.me/?url=${1}
}

# grepp – Imitate grep -p functions from Tru64 OS.

grepp ()
{
perl -00ne “print if /$1/i” < $2
}

# clock – A stupid clock function

clock ()
{
while true;do clear;echo “===========”;date +”%r”;echo “===========”;sleep 1;done
}

# dirsize – Directory contents sorted by size.

dirsize ()
{
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | \
egrep ‘^ *[0-9.]*[MG]‘ | sort -n > /tmp/list
egrep ‘^ *[0-9.]*M’ /tmp/list
egrep ‘^ *[0-9.]*G’ /tmp/list
rm /tmp/list
}

#extract – Extract most types of compressed files easily

extract ()
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2)   tar xvf $1     ;;
*.tar.gz)    tar xvf $1     ;;
*.bz2)       bunzip2 $1      ;;
*.rar)       unrar x $1      ;;
*.gz)        gunzip $1       ;;
*.tar)       tar xvf $1      ;;
*.tbz2)      tar xvf $1     ;;
*.tgz)       tar xvf $1     ;;
*.zip)       unzip $1        ;;
*.Z)         uncompress $1   ;;
*.7z)        7z x $1         ;;
*)           echo “‘$1′ cannot be extracted via >extract<” ;;
esac
else
echo “‘$1′ is not a valid file”
fi
}

#############################################################
########### ENCRYPTION / DECRYPTION FUNCTIONS ###############
#############################################################

dc ()
{
# Passphrase decryption program
# Created by Dave Crouse 10-20-2009
# Decrypts symetrically encrypted file to std out.
gpg –no-options –output .tempdc.txt “$1″
cat .tempdc.txt | less; rm -f .tempdc.txt
}

ec ()
{
# Passphrase encryption program
# Created by Dave Crouse 01-13-2006
# Reads input from text editor and encrypts to screen.
clear
echo ” ”
echo “**************************************************”;
echo “*         Passphrase Encryption Program          *”;
echo “**************************************************”; echo “”;
which $EDITOR &>/dev/null
if [ $? != "0" ];
then
echo “It appears that you do not have a text editor set in your
.bashrc file.”;
echo “What editor would you like to use ? ” ;
read EDITOR ; echo “”;
fi
echo “Enter the name/comment for this message :”
read comment
$EDITOR passphraseencryption
gpg –armor –comment “$comment” –no-options –output \

passphraseencryption.gpg –symmetric passphraseencryption
shred -u passphraseencryption ; clear
echo “Outputting passphrase encrypted message”; echo “” ; echo “” ;
cat passphraseencryption.gpg ; echo “” ; echo “” ;
shred -u passphraseencryption.gpg ;
read -p “Hit enter to exit” temp; clear
}

# Passhprase edit encrypted file program
# Created by Dave Crouse 11-02-2009
# Allow editing of symetrically encrypted files.
eedit ()
{
gpg –force-mdc –no-options –output .tempeec $1
emacs .tempeec
#vi .tempeec
gpg –armor –force-mdc –no-options –output $1 –symmetric .tempeec
shred -u .tempeec~ ;shred -u .tempeec ; clear
#shred -u .tempeec
}

enc ()
{
gpg –armor –no-options –output tempenc.txt –symmetric “$1″
mv tempenc.txt “$1″
}

dec ()
{
gpg $1
}

encnotes ()
{
echo “dc = decrpyt to screen”
echo “ec = encrypt to screen”
echo “enc = encrypt file to disk – overwrite existing”
echo “dec = decrypt file to disk – creates new file”
}

Bash Passphrase Encryption to the screen.

Sometimes, I wanted to be able to type a letter, and use a passphrase to encrypt it.
I could then paste the output into an email, forum post, wiki …….whatever and have it unreadable unless someone had the passphrase.

 

 


#!/bin/bash
# Passphrase encryption program
# Created by Dave Crouse 01-13-2006
# Reads input from text editor and encrypts to screen.
clear
echo " Passphrase Encryption Program";
echo "--------------------------------------------------"; echo "";
which $EDITOR &>/dev/null
if [ $? != "0" ];
then
echo "It appears that you do not have a text editor set in your
.bashrc file.";
echo "What editor would you like to use ? " ;
read EDITOR ; echo "";
fi
echo "Enter the name/comment for this message :"
read comment
nano passphraseencryption
gpg --armor --comment "$comment" --no-options --output \
passphraseencryption.gpg --symmetric passphraseencryption
shred -u passphraseencryption ; clear
echo "Outputting passphrase encrypted message"; echo "" ; echo "" ;
cat passphraseencryption.gpg ; echo "" ; echo "" ;
shred -u passphraseencryption.gpg ;
exit

Using “cut” to remove extra data.

At work we have to use Windows … so I’m stuck using mirc, and sometimes when pasting code back and forth, we want to quickly remove the extra data from the front when cutting text/data from the mirc window. It usually has a timestamp and name that need to be removed.
This does the trick quickly. You have to love the simplicity of shell commands. The “cut” command has some nice options that rarely get used, one is to list everything from field X on.. in this example start at the third field and list the rest is shown by “-f 3-”.
#!/usr/bin/bash
# stripmirc – strips fields from mirc copy/paste
# crouse

export TERM=xtermc
timestamp=`date +%T`
tempfile=”/tmp/mirc.$timestamp.tempfile”
touch ${tempfile}

# TRAP for the cleanup routine in case bad stuff happens.
trap cleanup 1 2 3 6
# Function for deleting files not needed.
cleanup ()
{
rm ${tempfile}
}

echo “Type CTRL D to end”
cat > ${tempfile}
echo “”;echo “”;echo “”;
cat ${tempfile} | cut -d” ” -f 3-
rm ${tempfile}
exit 0

Bash Phone Book Script – Version 2

Phone Book Script – Version 2
This version uses sqlite3 and bash.
Still has things i could fix i suppose …but it works for what i wanted.

I wanted to play with sqlite and bash, so I decided to revamp my older bash phone book script.
And before you ask……. yes, I use this everyday. I work from the command line all day long, and
for me simple is better, console is better than gui, and it did what I wanted it to… thought I’d share
it just in case someone else had this particular itch they needed to scratch.

Yes.. I know the code could be condensed, shortened …whatever, but I prefer “quickly understandable”
vs “obfuscated” code myself…. so knock yerself out if you want too… but I’ll leave that to you.


#!/bin/bash
# Crouse's sqlite+bash phonebook application.
# 11-22-2010 crouse

# Check if requirements are installed.
clear
if [[ -z $( type -p sqlite3 ) ]]; then echo -e "REQUIRED: sqlite3 -- NOT INSTALLED !";exit ;fi

# Check if database already exists, if not, create it.
database="/home/$USER/.phonebook.db"
if [ -e "$database" ];then
echo ""
else
echo "Datase does not exist - Creating now"
sqlite3 $database "create table phonebook (id INTEGER PRIMARY KEY,First TEXT,\
Last TEXT,Street TEXT,City TEXT,State TEXT,Zip TEXT,Phone TEXT,Email TEXT, Notes);"
echo "Database created at $database"
fi

# Check options given to the program and "do stuff".
case ${1} in

# oops--you didn't give me anything
"") echo "Crouse's sqlite+bash phonebook"
echo "================================"
echo "Usage: ${0##*/} ";
echo "";
echo " --------------------"
echo " Create an entry"
echo " $0 -c"
echo " --------------------"
echo " Search by last name \"-l";
echo " -l Smith";
echo "";
echo " Search by phone number";
echo " -p 515-555-5555";
echo ""
echo " Search by email address";
echo " -em EMAIL@SOMEADDRESS";
echo ""
echo " Search by first name with no flags";
echo " $0 dave";
echo " --------------------"
echo " Edit an entry"
echo " $0 -e"
echo "================================"
echo ""
;;

# Create an entry
"-c")
clear
echo "Phone Book - Insert data into database"
echo "--------------------------------------"
echo "FORMAT: First Name,Last Name,Street,City,State,Zip,Phone,Email,Notes"
echo " "

#Insert data into database
read -p "First Name: " First
read -p " Last Name: " Last
read -p " Street: " Street
read -p " City: " City
read -p " State: " State
read -p " Zip: " Zip
read -p " Phone: " Phone
read -p " Email: " Email
read -p " Notes: " Notes

sqlite3 $database "insert into phonebook (First,Last,Street,City,State,Zip,Phone,Email,Notes) \
values ('$First','$Last','$Street','$City','$State','$Zip','$Phone','$Email','$Notes');"

#clear
echo "-----------------------------"
echo "Data Entered into Database"
;;

# Edit an entry
"-e")
clear
read -p "Enter id# to edit: " idnum
echo ""

sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook \
WHERE id = '${idnum}';
.quit
EOF

echo ""
echo "Hitting enter will just leave that line as is."
echo ""
echo "-----------------------------------------------"
#Insert data into database
read -p "First Name: " First
read -p " Last Name: " Last
read -p " Street: " Street
read -p " City: " City
read -p " State: " State
read -p " Zip: " Zip
read -p " Phone: " Phone
read -p " Email: " Email
read -p " Notes: " Notes

echo ""

if [[ "$First" != "" ]]; then
sqlite3 $database "UPDATE phonebook SET First = '${First}' WHERE id='${idnum}';"
fi

if [[ "$Last" != "" ]]; then
sqlite3 $database "UPDATE phonebook SET Last = '${Last}' WHERE id='${idnum}';"
fi

if [[ "$Street" != "" ]]; then
sqlite3 $database "UPDATE phonebook SET Street = '${Street}' WHERE id='${idnum}';"
fi

if [[ "$City" != "" ]]; then
sqlite3 $database "UPDATE phonebook SET City = '${City}' WHERE id='${idnum}';"
fi

if [[ "$State" != "" ]]; then
sqlite3 $database "UPDATE phonebook SET State = '${State}' WHERE id='${idnum}';"
fi

if [[ "$Zip" != "" ]]; then
sqlite3 $database "UPDATE phonebook SET Zip = '${Zip}' WHERE id='${idnum}';"
fi

if [[ "$Phone" != "" ]]; then
sqlite3 $database "UPDATE phonebook SET Phone = '${Phone}' WHERE id='${idnum}';"
fi

if [[ "$Email" != "" ]]; then
sqlite3 $database "UPDATE phonebook SET Email = '${Email}' WHERE id='${idnum}';"
fi

if [[ "$Notes" != "" ]]; then
sqlite3 $database "UPDATE phonebook SET Notes = '${Notes}' WHERE id='${idnum}';"
fi
;;

# Search by last name
"-l")
echo ""
sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook \
WHERE Last LIKE '%${2}%';
.quit
EOF
;;

# Search by phone
"-p")
echo ""
sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook \
WHERE Phone LIKE '%${2}%';
.quit
EOF
;;

# Search by email
"-em")
echo ""
sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook \
WHERE Email LIKE '%${2}%';
.quit
EOF
;;

# Search by first name for all others
*)
echo "";
sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook \
WHERE First LIKE '%${1}%';
.quit
EOF
;;

esac
echo ""
exit 0


Crouse’s sqlite+bash phonebook
================================
Usage: pb

——————–
Create an entry
/usr/local/bin/pb -c
——————–
Search by last name “-l
-l Smith

Search by phone number
-p 515-555-5555

Search by email address
-em xxxxx@usalug.net

Search by first name with no flags
/usr/local/bin/pb dave
——————–
Edit an entry
/usr/local/bin/pb -e
================================

screenhelp – show notes about screen.

This isn’t so much a script as it is some helpful notes on running screen.
When people are learning to use screen, it can be daunting, so I created some notes that people could use to quickly find out how to do X. Thought I’d share. :)
Continue reading ‘screenhelp – show notes about screen.’ »

Bash – mysql backup script

This script backs up all databases on the server it’s running on. Usually ran as a cron job.

Hope someone else can use this as well.

#!/bin/bash
# Backup ALL mysql databases
# by crouse - http://archlinux.me/crouse
x=`mysql --user=root --password=REMOVED -B -e "show databases"  | wc -l `; y=$((x-2));
for i in `mysql --user=root --password=REMOVED -B -e "show databases"  | tail -${y}`; do
mysqldump --opt --user=root --password=REMOVED $i | gzip -9 > /root/MYSQLBACKUPS/$i.`date --iso-8601`-mysqlbackup.gz
ls -t /root/MYSQLBACKUPS/$i.* | awk 'NR>7' | xargs rm -f
done
# Mail updates
touch sendthis
echo "" > sendthis
echo "Archlinux.me Nightly Mysql Backup Information" >> sendthis
echo "---------------------------------------------" >> sendthis
echo " " >> sendthis
ls -la /root/MYSQLBACKUPS/ >> sendthis
cat sendthis | mail -s mail SOMEEMAIL@archlinux.us -s "Archlinux.me - Nightly Mysql Backup Notice"
rm sendthis
exit