Wednesday, April 30, 2014

Bash - String Lenght

Getting string length in Bash Script is very easy by using # operator:

$ some_variable="This is Test String"
$ echo ${#some_variable}
19

${#variable_name} gives us string length.

Tuesday, April 29, 2014

Linux Add Group

To add group linips to Linux type:

$ groupadd linips

General format of command groupadd is:

groupadd [options] groupname

options are:

  • --help 
  • -g, --GID gid -to assign Group ID
  • -f, --force - -f with -g acts like -g not exist - GID another unique
  • -K, --key KEY=VALUE
  • -o, --non-unique - to add group with non unique GID
  • -p, --password PASSWORD
  • -r, --system - To create system group




Monday, April 28, 2014

Bash - Ping ip range and email result

In Bash script is a very simple to write script for checking availability of network devices. In this case, the script will ping an address range and rusults will be sent to your email address.

#!/bin/bash
om="mailtempfile"
dat="$(date)"
eval "echo 'This is Computer status from Linips Script at $dat' >${om}"
eval "echo '' >>${om}"

for i in {1..6}
do
    out="$(ping 192.168.1.$i -c 5|tail -1|awk '{print $4}'|cut -d '/' -f 2)"
    case $i in
    1)
        comp="Router One     "
        ;;
    2)
        comp="Router Two     "
        ;;
    3)
        comp="Router Three   "
        ;;
    4)
        comp="Server One     "
        ;;
    5)
        comp="Server Two     "
        ;;
    6)
        comp="Server Three   "
        ;;
    esac
    eval "echo '192.168.1.$i   $comp    $out'>>${om}"
done
mail -s "Net status $dat" myemail@adress <mailtempfile


Or, the same script, with using Bash script arrays:

#!/bin/bash
comp[1]="Router One      "
comp[2]="Router Two      "
comp[3]="Router Three    "
comp[4]="Server One      "
comp[5]="Server Two      "
comp[6]="Server Three    "

om="mailtempfile"
dat="$(date)"
eval "echo 'This is Computer status from Linips Script at $dat' >${om}"
eval "echo '' >>${om}"

for i in {1..6}
do
    out="$(ping 192.168.1.$i -c 5|tail -1|awk '{print $4}'|cut -d '/' -f 2)"
    eval "echo '192.168.1.$i   ${comp[$i]}    $out'>>${om}"
done
mail -s "Net status $dat" myemail@address <mailtempfile



Received email:

This is Computer status from Linips Script at Mon Apr 28 08:13:45 CEST 2014

192.168.1.1   Router One         2.493
192.168.1.2   Router Two         17.712
192.168.1.3   Router Three       13.669
192.168.1.4   Server One         4.601
192.168.1.5   Server Two         8.199
192.168.1.6   Server Three       9.302


And finally, the greatest strenght of this script is that it runs automatically, so you need to add one line in the /etc/crontab file: (example is for script running every 20 min)

*/20  *       * * *     root     /home/user/pingscript..sh

Sunday, April 27, 2014

Bash String Concatenation

If you writing Bash scripts, it's probably happened that you need to concatenate two or more strings. There are several ways to do this:

1. Variable insert

$ a="Hello"
$ b="$a World"
$ echo $b
Hello World

2. Writing variables one after another

$ a="Hello"
$ b=" World"
$ c=$a$b
$ echo $c
Hello World

3. Using += operator

$ a="Hello"
$ a+=" World"
$ echo $a
Hello World




Saturday, April 26, 2014

Linux Get CPU Info

Probably the simplest way to get CPU info in Linux is to display /proc/cpuinfo (and example output)

$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 15
model           : 2
model name      : Intel(R) Celeron(R) CPU 2.40GHz
stepping        : 9
cpu MHz         : 2423.933
cache size      : 128 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe up pebs bts sync_rdtsc cid xtpr
bogomips        : 4851.34
clflush size    : 64

if we need only to display model name: 

$ cat /proc/cpuinfo|grep "model name"
model name      : Intel(R) Celeron(R) CPU 2.40GHz

In the case that there are multiple processors, info of second will continue after first one and so on... processor: 0, processor: 1,...

Friday, April 25, 2014

Solution for maintaining old Ubuntu Linux computer

If you have an old server with Ubuntu Linux distribution, which for some reason you can not update to newer version and you need to maintain it. During new program installation many errors tells us that  aptitude or apt-get can not find the path to the server from which we need to download required packets. Solution is very simple for the following Ubuntu Linux distributions: 

  • Ubuntu 4.10 (Warty Warthog)
  • Ubuntu 5.04 (Hoary Hedgehog)
  • Ubuntu 5.10 (Breezy Badger)
  • Ubuntu 6.06.2 LTS (Dapper Drake)
  • Ubuntu 6.10 (Edgy Eft)
  • Ubuntu 7.04 (Feisty Fawn)
  • Ubuntu 7.10 (Gutsy Gibbon)
  • Ubuntu 8.04.4 LTS (Hardy Heron)
  • Ubuntu 8.10 (Intrepid Ibex)
  • Ubuntu 9.04 (Jaunty Jackalope)
  • Ubuntu 9.10 (Karmic Koala)
  • Ubuntu 10.04.4 LTS (Lucid Lynx)
  • Ubuntu 10.10 (Maverick Meerkat)
  • Ubuntu 11.04 (Natty Narwhal)
  • Ubuntu 11.10 (Oneiric Ocelot) 
  • Ubuntu 12.04.3 LTS (Precise Pangolin)
All repositories for those distributions are located at: old-releases.ubuntu.com

How to make changes

Edit your /etc/apt/sources.list : 
change all urls from: http://<>.archive.ubuntu.com/ubuntu  to http://old-releases.ubuntu.com/ubuntu

And your old Ubuntu Linux is ready for update :)


Thursday, April 24, 2014

BASH - customize ping output

Problem is simply: how to get the output of ping command suited to our needs. In this example we will extract average ping time in Bash scripting language.

First let's see output of ping command: ping 192.168.1.1 -c 5 (-c 5 tells to ping command to run five times, else it will run infinitely.)

$ ping 192.168.1.1 -c 5
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.62 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.477 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.482 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.482 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=0.487 ms

--- 192.168.1.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.477/0.911/2.629/0.859 ms

Average ping time is in last line. Use tail -1 to extract only last line. 

$ ping 192.168.1.1 -c 5|tail -1
rtt min/avg/max/mdev = 0.477/0.911/2.629/0.859 ms

Then extract values, count 'arguments' in this line separated by space: 'rtt' is the first, 'min/avg/max/mdev' 2nd, '=' 3rd, '0.477/0.911/2.629/0.859' 4th and 'ms' 5th. 

$ ping 192.168.1.1 -c 5|tail -1|awk '{print $4}'
0.477/0.911/2.629/0.859

and finally, from this output of four '/' separated values, take the second value: this is done by cut command: 

$ ping 192.168.1.1 -c 5|tail -1|awk '{print $4}'|cut -d '/' -f 2
0.911

and this is the value that we are looking for...