Wednesday, 9 January 2013

CLI Linux setup script

I use Linux as much as I can these days and only dip into windows when I really have to i.e. games etc. I am also a bit of a distro hopper although I have settled on Linux Mint 13 for some time now, It has all the usability and support of Ubuntu but with a much nicer UI than Unity. Cinnamon is a fork of the Gnome Shell and gives a more traditional feel but with a lot of nice effects and and usability features.

I have also been playing with 2 Raspberry PI's lately and occasionally have to reformat them and start again due to my mistakes or a pivot in the project I'm running. As we all know a bare install is like a pair of new shoes, it takes a while to get them how you like them and i generally forget about some infrequently used but vital tool until I need it and then have to break my flow to go grab it and install it, well no more!

Below is a short bash script that I can copy from here when I need it, past into a file & run to get the basics setup quickly, It will even grab my beloved .bashrc file and copy it to my home directory so i have access to all my aliases and have my command prompt just so.


#!/bin/bash

title="Linux Personalisation Script"
prompt="Pick an option:"
options=("Update Installation" "Upgrade Distribution" "Add user jamie & set password" "Edit group file" "Install Fortune, Git, Terminator(A Great Console), TTYtter(cli twitter client), VLC player" "Install Nvidia drivers" "Install LAMP Server (Apache2, PHP5, MySql)" "Get Fruitbats Custom .bashrc File" "Get xorg.conf " )

echo -e "${LIGHTCYAN}$title"
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do 

    case "$REPLY" in

    1 ) sudo apt-get update;;
    2 ) sudo apt-get dist-upgrade;;
    3 ) sudo sudo useradd -m jamie && sudo passwd jamie;;
    4 ) sudo nano /etc/group;;
    5 ) sudo apt-get install fortune-mod git-core terminator ttytter vlc vlc-plugin-pulse mozilla-plugin-vlc proftpd-basic dropbox && dropbox start -i ;;
    6 ) sudo add-apt-repository ppa:ubuntu-x-swat/x-updates && sudo apt-get update && sudo apt-get install nvidia-current nvidia-settings;;
    7 ) sudo apt-get install apache2 php5 libapache2-mod-php5 mysql-server libapache2-mod-auth-mysql php5-mysql phpmyadmin;;
    8 ) cd /home/*/ && rm -f .bashrc && wget https://dl.dropbox.com/u/XXXXXXX/.bashrc;;  ##save below script to your own dropbox
    9 ) cd /etc/X11 wget https://dl.dropbox.com/u/17907226/xorg.conf;;
    

    $(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
    *) echo "Invalid option. Try another one.";continue;;
    
    

    esac

done


echo -e "${LIGHTCYAN} Well Done. Have the Bluemars.org stream as a reward. CTRL C to Exit Player."
echo "To hear more from bluemars.org just type bluemars at the prompt  "
cvlc "http://207.200.96.225:8020/listen.pls" 
 
endColor='\e[0m'


That's the basic script as you can see its nothing too fancy at the moment just a menu and some apt-get install's with an audio stream that plays on exit to test the VLC Player setup worked. Below is the .bashrc script im so fond of. I included it here as most people will be wary of grabbing an unknown file and I wouldn't blame you if you commented out that line and menu entry. I cant take credit for a large chunk of the script below as I grabbed it from pastebin and tailored it to my own liking as i do with most code, I find it a good way to learn new code.


# .bashrc
# Imported by Fruitbats Linux-setup-script-v2
# Source global definitions
if [ -f /etc/bashrc ]; then
 . /etc/bashrc
fi

# User specific aliases and functions
##################################
#    _   _ _                     #
#   /_\ | (_) __ _ ___  ___ ___  #
#  //_\\| | |/ _` / __|/ _ | __| #
# /  _  \ | | (_| \__ \  __|__ \ #
# \_/ \_/_|_|\__,_|___/\___|___/ #
##################################
alias ut='tar -zxvf' # untar
alias t='tar -cvf' # to compress files with dates: t file-$(date +%F).tar 
alias tsee='tar -tvf'
alias off="su -c 'shutdown -h now'"
alias restart="su -c 'reboot'"
alias x='exit'
alias cls='clear'
alias ..='cd ..'
alias ...='cd ../..'
alias pdir='cd $OLDPWD'
alias setup='sudo apt-get install'
alias install='sudo apt-get install'
alias docs='cd ~jc/Documents;ls -l'
alias downs='cd  ~jc/Downloads;ls -l'
alias tailmsg='tail -f /var/log/messages'
alias t='todo.sh -d /home/jamie/Documents/todo/todo.cfg'
alias ..='cd ..'
alias sshpi='ssh jamie@192.168.1.42'
alias sshpi2='ssh jamie@192.168.1.40'
alias bluemars='cvlc "http://207.200.96.225:8020/listen.pls"'


# chmod and permissions commands
alias mx='chmod a+x'
alias mynet="su -c 'nmap -O -sS 192.168.1.0/24'"
alias pg='ps aux | grep'  #requires an argument
#Use human-readable filesizes
alias du="du -h"
alias df="df -h"
#-------------------------------------------------------------
# The 'ls' family (this assumes you use a recent GNU ls)
#-------------------------------------------------------------
alias l='ls'
alias ll="ls -l --group-directories-first"
alias ls='ls -hF --color'  # add colors for filetype recognition
alias la='ls -Al'          # show hidden files
alias lx='ls -lXB'         # sort by extension
alias lk='ls -lSr'         # sort by size, biggest last
alias lc='ls -ltcr'        # sort by and show change time, most recent last
alias lu='ls -ltur'        # sort by and show access time, most recent last
alias lt='ls -ltr'         # sort by date, most recent last
alias lm='ls -al |more'    # pipe through 'more'
alias lr='ls -lR'          # recursive ls
alias tree='tree -Csu'     # nice alternative to 'recursive ls'

#-------------------------------------------------------------
# spelling typos - highly personnal and keyboard-dependent :-)
#-------------------------------------------------------------
alias xs='cd'
alias vf='cd'
alias moer='more'
alias moew='more'
alias kk='ll'

############################################################
#  _____ _              ___                           _    #
# /__   \ |__   ___    / _ \_ __ ___  _ __ ___  _ __ | |_  #
#   / /\/ '_ \ / _ \  / /_)/ '__/ _ \| '_ ` _ \| '_ \| __| #
#  / /  | | | |  __/ / ___/| | | (_) | | | | | | |_) | |_  #
#  \/   |_| |_|\___| \/    |_|  \___/|_| |_| |_| .__/ \__| #
#                                              |_|         #
############################################################  

# Greeting, motd etc...
#-------------------------------------------------------------
# Define some colors first:
LIGHTGRAY='\e[0;37m'
LIGHTBLUE='\e[1;34m'
LIGHTGREEN='\e[1;32m'
LIGHTCYAN='\e[1;36m'
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
NORMAL='\[\033[00m\]'
NC='\e[0m'              # No Color
PS1="$cyan[\u@\h] -> $LIGHTBLUE\w $NC \n \$ "
##################################################
#    ___                  _   _                  #
#   / __\_   _ _ __   ___| |_(_) ___  _ __  ___  #
#  / _\ | | | | '_ \ / __| __| |/ _ \| '_ \/ __| #
# / /   | |_| | | | | (__| |_| | (_) | | | \__ \ #
# \/     \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ #
##################################################
# edit bashrc
ebash ()
{
  cd;vim .bashrc
}
#source bashrc
sbash ()
{
  source .bashrc
}
# grep for active process
function psgrep()
{
        ps aux | grep -e $1
}

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# Makes extraction easier
function extract() {
     if [ -f $1 ] ; then
         case $1 in
             *.tar.bz2)   tar xvjf $1     ;;
             *.tar.gz)    tar xvzf $1     ;;
             *.bz2)       bunzip2 $1      ;;
             *.rar)       unrar x $1      ;;
             *.gz)        gunzip $1       ;;
             *.tar)       tar xvf $1      ;;
             *.tbz2)      tar xvjf $1     ;;
             *.tgz)       tar xvzf $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
}
 

# Get Host Information
#-------------------------------------------------------------
function hostinfo()   # Get current host related info.
{
    echo -e "\nYou are logged on ${RED}$HOST"
    echo -e "\nAdditionnal information:$NC " ; uname -a
    echo -e "\n${RED}Users logged on:$NC " ; w -h
    echo -e "\n${RED}Current date :$NC " ; date
    echo -e "\n${RED}Machine stats :$NC " ; uptime
    echo -e "\n${RED}Memory stats :$NC " ; free
    my_ip 2>&- ;
    echo -e "\n${RED}Local IP Address :$NC" ; echo ${MY_IP:-"Not connected"}
    echo -e "\n${RED}ISP Address :$NC" ; echo ${MY_ISP:-"Not connected"}
    echo -e "\n${RED}Open connections :$NC "; netstat -pan --inet;
    echo
}
 

#-------------------------------------------------------------

# DNS and DHCP information
netinfo ()
{
echo "#############Network Information###############"
echo ""
egrep -i "interface|fixed-address|subnet-mask|domain-name-servers|dhcp-server-identifier|domain-name" /var/lib/dhclient/dhclient.leases
echo ""
echo "###############################################"
echo ""
echo "####Current Public IP Address########"
echo ""
myip=`lynx -dump -hiddenlinks=ignore -nolist http://checkip.dyndns.org:8245/ | sed '/^$/d; s/^[ ]*//g; s/[ ]*$//g' `
echo "${myip}"
echo ""
echo "#####################################"
echo ""
}
#-------------------------------------------------------------
# Creates an archive from given directory
#-------------------------------------------------------------
mktar() { tar cvf  "${1%%/}.tar"     "${1%%/}/"; }
mktgz() { tar cvzf "${1%%/}.tar.gz"  "${1%%/}/"; }
mktbz() { tar cvjf "${1%%/}.tar.bz2" "${1%%/}/"; }
#-------------------------------------------------------------
export EDITOR=nano
export VISUAL=nano
#-----------------------------#
#      WELCOME SCREEN       #
#-----------------------------#
clear
 
echo -ne "${cyan}""Hello, $USER. today is, "; date
echo -e "${LIGHTGREEN}"; cal -3;
echo -e "${red}"; fortune;
echo -e "${cyan}"; uname -snmro ;
echo ""
#------------------------------


In closing the raspberry PI that I got, then ignored for months as I couldn't find a project for it, has turned out to be a great spur to learning a variety of other skills, so far I've made a dent in :-
  1. Learning python.
  2. Learning Bash scripting
  3. Started Codeacademy
  4. Learnt to solder
  5. Started to cross stitch this AdaFruit resistor chart/cushion cover
Well 2 blog posts in one month, it must be January :)

Saturday, 5 January 2013

Another new year, another post, This time to test the nice Code formatting script SyntaxHighlighter
and to show off the code I am playing with on the Raspberry pi, plus the add-on board the Berryclip, gifted to me by my long suffering friend +Carl Hughes.

Im attempting to make a python script that will flash or buzz morse code generated from text  entered by the user. 

Sample code to make the BerryClip flash Led1 the letter A in Morse code, to sound the buzzer you can use gpio pin (8).

#!/usr/bin/python

# Import required libraries
import RPi.GPIO as GPIO
import time

# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)
# List of LED GPIO numbers
LedSeq = [4,17,22,10,9,11]

# Set up the GPIO pins as outputs and set False
for x in range(6):
  GPIO.setup(LedSeq[x], GPIO.OUT)
  GPIO.output(LedSeq[x], False)
# Enable Buzzer
GPIO.setup(8, GPIO.OUT)
GPIO.output(8, False)

# Morse code 
# A
GPIO.output(LedSeq[0], True) #Start Led1
GPIO.output(8, True) #Start Buzzer
time.sleep(.25) #duration
GPIO.output(LedSeq[0], False) # Stop Led1
GPIO.output(8, False) # Stop Buzzer
time.sleep(.15)
GPIO.output(LedSeq[0], True)
GPIO.output(8, True)
time.sleep(.5)
GPIO.output(LedSeq[0], False)
GPIO.output(8, False)
time.sleep(.15)

# Reset GPIO settings
GPIO.cleanup()

Sunday, 14 August 2011

Return to blogger

After a spell away from blogger I have returned to make my life A completely google life, It's funny how I left face book as I didn't trust them not to sell every thing about me to some ad agency or marketer but I trust google implicitly even thought there buisness is all about ads.

In a circular move I'll link you to my new website www.nightsdawn.co.uk which currently has a link to my tumblr blog but ill change to this one soon.

Tuesday, 23 November 2010

The programming itch

Ah looks like I got the itch again, The itch that drives me to pick up a language and try to create something with it, Usually a game but I never get that far I normally quite way before that point.

So I have picked up M$ C# express edition which is very much not $$$ being free for all to use but lacking a few features of it bigger brother Visual studio, I was tempted by Java but the learning support structure just isn't as rich as it is for C# which is a shame.

My current stab at learning is different from normal where I grab a starter tutorial and work through variables, arrays etc for the umpteenth time and instead have dived straight in to a ready made RPG game kit where you learn by modifying the code as you go. I think this might be a good approach for me and my slightly ADD brain :)

Heres the MS site wit the RPG starter kit for windows and XBOX 360 RPG Starter kit

Monday, 8 November 2010

Fire pit


Fire pit, originally uploaded by Fishamatician.

A cozy fire at the beach a few years ago, I had forgotten all about it till I started to sort my photos out. Turns out I have 47Gb of photos on the HDD!

Sunday, 7 November 2010

Desk at night


Desk at night, originally uploaded by Fishamatician.

There are far to many lights on in there at night!

Georgette pebbles


Georgette pebbles, originally uploaded by Fishamatician.

Mediterranean spur thigh tortoise cooling off in the pebble pool.