Archiwum

Posty oznaczone ‘sh’

Audible ping

Styczeń 11th, 2012 Brak komentarzy

If you don’t look at console output and want to hear that host is reachable then just add -a parameter to ping command (Linux).

$ ping -V
ping utility, iputils-sss20101006
$ ping -a 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=44 time=38.9 ms (beep)
64 bytes from 8.8.8.8: icmp_req=2 ttl=44 time=39.3 ms (beep)
64 bytes from 8.8.8.8: icmp_req=3 ttl=44 time=39.0 ms (beep)
^C
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 38.997/39.138/39.346/0.273 ms
Tagi:,

How to count files or directories

Styczeń 10th, 2012 Brak komentarzy

For this example I will use such tree:

.
├── dir1/
│   ├── dir6/
│   ├── dir7/
│   ├── dir8/
│   ├── file4
│   └── file5
├── dir2/
│   └── dir9/
│       ├── dir10/
│       │   ├── file9
│       │   └── file10
│       ├── file6
│       ├── file7
│       └── file8
├── dir3/
├── dir4/
├── dir5/
├── file1
├── file2
└── file3

List of all files and directories:

$ find . | sort | grep -v ^.$
./dir1
./dir1/dir6
./dir1/dir7
./dir1/dir8
./dir1/file4
./dir1/file5
./dir2
./dir2/dir9
./dir2/dir9/dir10
./dir2/dir9/dir10/file9
./dir2/dir9/dir10/file10
./dir2/dir9/file6
./dir2/dir9/file7
./dir2/dir9/file8
./dir3
./dir4
./dir5
./file1
./file2
./file3

Number of all files and directories:

$ find . | grep -v ^.$ | wc -l
20

List of all directories:

$ find . -type d | grep -v ^.$
./dir3
./dir5
./dir1
./dir1/dir6
./dir1/dir8
./dir1/dir7
./dir2
./dir2/dir9
./dir2/dir9/dir10
./dir4

Count of all directories:

$ find . -type d | grep -v ^.$ |wc -l
10

List of subdirectories in current directory:

$ find . -maxdepth 1 -type d | sort | grep -v ^.$
./dir1
./dir2
./dir3
./dir4
./dir5

Count of subdirectories in current directory:

$ find . -maxdepth 1 -type d | grep -v ^.$ | wc -l
5

List of all files:

$ find . -type f | sort
./dir1/file4
./dir1/file5
./dir2/dir9/dir10/file9
./dir2/dir9/dir10/file10
./dir2/dir9/file6
./dir2/dir9/file7
./dir2/dir9/file8
./file1
./file2
./file3

Count of all files:

$ find . -type f | wc -l
10

List of files only in current directory:

$ find . -maxdepth 1 -type f | sort
./file1
./file2
./file3

Count of files only in current directory:

$ find . -maxdepth 1 -type f | wc -l
3
Tagi:,

Create web thumbnail for SemanticScuttle using cutycapt

Grudzień 28th, 2011 Brak komentarzy

This simple script will create web thumbnail using cutycapt console command.

I created it to add thumbnails to my bookmarks page.

#!/bin/sh                                                                                                                                      
 
if [ "$#" != "1" ]; then
  exit
fi
 
# Temporary image
temp_image="$(tempfile).png" 
 
# Output image
output_image=`echo $1 | sed s/^www\.//`
 
# First, second and third output_image's letters used for directory creation
first_letter=`echo ${output_image} | cut -c1`
second_letter=`echo ${output_image} | cut -c2`
third_letter=`echo ${output_image} | cut -c3`
 
# Output directory
output_directory="thumbnails/${first_letter}/${second_letter}/${third_letter}"
 
cutycapt --url=http://$1 --out=$temp_image
convert -resize 120 -crop 120x90+0+0 $temp_image $temp_image
mkdir -p $output_directory
cp ${temp_image} ${output_directory}/${output_image}.png
unlink $temp_image

My bookmarks-thumbnail.inc.tpl.php from Semanticscuttle looks like this:

$url=parse_url($address);
$host=preg_replace("/^www\./",'',$url['host']);
$image="/thumbnails/". $host[0] . "/" . $host[1] . "/" . $host[2] . "/" . $host . ".png";
 
if (file_exists($image)) {
  echo '<img class="thumbnail" onclick="window.location.href=\''.$address.'\'" src="/thumbnails/'. $host[0] . '/' . $host[1] . '/' . $host[2] . '/' . $host . '.png" alt="" />
}

This solutions have couple of limitations and probably doesn’t need so many sub-directories but it will be tuned over time.

How to split (by size) and merge files

Luty 27th, 2011 Brak komentarzy

Lets say that we have one big file and want to split it into 10M parts.

$ ls -lh
razem 44M
-rw-r----- 1 milosz milosz 44M 2011-02-27 16:28 file.png

To divide file into smaller parts we will use split command.

$ split -d -b10M file.png file1.part
$ ls -lh
razem 87M
-rw-r--r-- 1 milosz milosz  10M 2011-02-27 16:39 file1.part00
-rw-r--r-- 1 milosz milosz  10M 2011-02-27 16:39 file1.part01
-rw-r--r-- 1 milosz milosz  10M 2011-02-27 16:39 file1.part02
-rw-r--r-- 1 milosz milosz  10M 2011-02-27 16:39 file1.part03
-rw-r--r-- 1 milosz milosz 3,3M 2011-02-27 16:39 file1.part04
-rw-r----- 1 milosz milosz  44M 2011-02-27 16:28 file.png

You can merge them in couple different ways.

$ cat file1.part0{0,1,2,3,4} > file_m.png
$ diff file.png file_m.png 
$
$ ls -1 file1.* | sort | xargs cat > file_m2.png
$ diff file.png file_m2.png
$
$ cat file1.part0* > file_m1.png
$ diff file.png file_m1.png 
$
Tagi:,

How to sort ip addresses

Luty 27th, 2011 Brak komentarzy

I don’t remember source but it is very easy to remember.

$ cat ip_file
10.0.0.2
10.3.0.1
192.168.0.3
192.168.9.11
192.168.9.10
10.2.2.1
10.2.2.21
10.2.2.17
192.168.2.1
10.0.0.1
10.0.0.100
$ sort -t. +0n -1n +1n -2n +2n -3n +3n ip_file
10.0.0.1
10.0.0.2
10.0.0.100
10.2.2.1
10.2.2.17
10.2.2.21
10.3.0.1
192.168.0.3
192.168.2.1
192.168.9.10
192.168.9.11

Just extracted it from some very old script. Worth to remember.

Tagi:,

Scripting SPS switches

Styczeń 20th, 2011 Brak komentarzy

Managing SPS switches could be real fun if you use expect to automate your tasks.

Script inserted below will backup configuration of specified switches. It has couple of shortcomings but works quite well. By modifying expect script you can read logs, copy running config to startup config and of course modify configuration.

#!/bin/sh
# Script designed to backup configuration of SPS switches
#
# Example usage:
# $ sh sps_backup.sh 10.2.2.3 10.2.2.4
#
 
# Commands
expect=`which expect`
sed=`which sed`
 
# Archive name
ar_date=`date +"%d.%m.%y_%H%M%S"`
ar_name="sps_${ar_date}.tgz"
 
# SSH
ssh=`which ssh`
ssh_port="22"
 
# get_credentials - Get username and password
get_credentials(){
 echo -n "Username: "
 read user
 
 oldmodes=`stty -g`
 stty -echo
 echo -n "Password: "
 read pass
 stty $oldmodes
 echo
 
 export pass
 export user
}
 
# get_config - create expect script
get_config() {
cat << EOF
#!${expect} -f
log_user 0
exp_internal 0
sleep 3
 
spawn ${ssh} ${host} -p ${ssh_port}
 
expect {
  "The authenticity of host" {
    send "yes\r"
  }
}
 
expect  "User Name:" {
  send "$user"
  send "\r"
}
expect  "Password:" {
  send "$pass"
  send "\r"
}
 
expect {
  "User Name:" {
    exit 1
  }
  "SPS*#" {
    send "show startup-config\r"
    while (1) {
      expect {
        timeout break
        "<return>" {
          puts "\$expect_out(buffer)"
          send " "
        }
        "SPS*#" {
          puts "\$expect_out(buffer)"
          break
        }
      }
    }
  }
}
send "exit\r"
 
exit 0
EOF
}
 
# Get login details
get_credentials
 
# Create temporary directory
temp_dir=`mktemp -d` || exit 1
 
for host in  ${*}; do
  temp_file="${temp_dir}/${host}"
 
  # Get config
  get_config ${host} | ${expect} -f - | while read line
  do
    echo $line >> $temp_file
  done
 
  # Remove unnecessary stuff
  if [ -f ${temp_file} ]; then
    sed -ci -e "/^User.*/d"             $temp_file
    sed -ci -e "/^Pass.*/d"             $temp_file
    sed -ci -e "/More.*return*/d"       $temp_file
    sed -ci -e "/show startup-config/d" $temp_file
    sed -ci -e "/^SPS.*#$/d"            $temp_file
    sed -ci -e "s/^M//g"                $temp_file
    sed -ci -e "/^ $/d"                 $temp_file
    sed -ci -e "s/^\ //g"               $temp_file
    sed -ci -e "/^$/d"                  $temp_file
  fi
 
  # Check if we got config
  # It will contain "password ..." line
  file_check=`cat ${temp_file} | grep "^password"}`
  if [ -n "${file_check}"]; then
    echo -e "${host}\tOK"
  else
    echo -e "${host}\tError"
    rm ${temp_file}
  fi
done
 
# Create archive
directory=`pwd`
cd ${temp_dir} && tar cfz ${directory}/${ar_name} .
 
# Clean up
rm    ${temp_dir}/*
rmdir ${temp_dir}
 
echo "Archive contents:"
tar tfz ${directory}/${ar_name}

For example to see logs on SPS switch you could use such code:

send "show logging\r"
while (1) {
 expect {
  timeout break
  "<return>" {
   puts "$expect_out(buffer)"
   send " "
  }
 
  "SPS*#" {
   puts "$expect_out(buffer)"
   break
  }
 }
}
send "exit\r"

If have couple of SRW switches then there is a little problem. If you try connecting using ssh you will see error:

ssh_rsa_verify: RSA modulus too small: 512 < minimum 768 bits
key_verify failed for server_host_key

To solve it you need to download ssh source, change SSH_RSA_MINIMUM_MODULUS_SIZE in ssh.h and compile it by hand.

After that you can access lcli (it’s a little bit tricky but easily scriptable) :

expect "Password:"
send "$env(user)"
send "\t"
send "$env(pass)"
send "\r"
expect "logout"
send \x1a
expect "TERMINATED"
send \r
expect ">"
send "lcli\r"
expect "User Name:"
send "$env(user)"
send "\r"
expect "Password:"
send "$env(pass)"
send "\r"
expect "#"

Just one note: There are couple of assumptions used, one of them is switch hostname=switch model (like hostname SPS224G4).

Tagi:, , , ,

How to get rid of spaces in file names

Styczeń 12th, 2011 Brak komentarzy

If you want to get rid of spaces in file/directory names then just look at this simple script:

#!/bin/bash
 
ls | grep "\ " | while read item
do
  new=`echo ${item} | tr [:blank:] _`
  old=${item}
  mv "${old}" ${new}
done

Script works in current directory for files and directories.

Tagi:

How to downscale video for your portable device

Styczeń 12th, 2011 Brak komentarzy

Sometimes I need to downscale video (like music) for portable device.
After some research I ended up using mencoder within such script:

#!/bin/sh
 
OUTPUT_DIR="/home/milosz/converted"
 
mencoder ${1} -ovc xvid -xvidencopts bitrate=300 -vf scale=320:-3 -ofps 25 -oac lavc -lavcopts acodec=mp3:abitrate=64 -o ${OUTPUT_DIR}/${1##*/}
Tagi:, ,

HTTP compression test

Grudzień 7th, 2010 Brak komentarzy

To check if your Nginx server has properly configured compression just execute command:

$ curl -s -I --compressed  http://my.web.site | grep Content-Encoding

Empty result means that it doesn’t support compression.
Proper results should look like this:

Content-Encoding: gzip

KDE – Using notification area within a shell script

Grudzień 7th, 2010 Brak komentarzy

Using KDE notification area within shell script gives a lot of useful possibilities. You can easily get feedback from scripts running in background and checking logs or running services.

To create above notification as example I used quite simple script that uses kdialog command. and basic HTML tags.

#!/bin/sh
 
message(){
cat << EOF
Say <strong>hello</strong> using your <font color=red>nofication</font> area.
Date: <em>`date`</em>
Used space: `df -h | grep sda1 | awk '{ print $5 }'`
Load average: <u>`uptime | awk '{ print $10 $11 $12 }' | sed s/\,/\ /g`</u>
EOF
}
 
timeout=20
kdialog=`which kdialog`
 
${kdialog} --passivepopup "`message`" ${timeout}
Tagi:,