Month: February 2008

Want interactive command in non-interactive mode? – use expect !!

Want interactive command in non-interactive mode? – use expect !!

Introduction- Since long time, I was thinking to write post on expect!!. and finally I start writing this post.. I am using expect tool for automation, scheduling and running commands on remote machine by using ssh in non-interactive mode. It’s very useful and handy tool for System admin specially Unix and Linux admin.

What is Expect ?

Expect is a Unix automation and testing tool, written by Don Libes. Expect has regular expression pattern matching and general program capabilities, allowing simple scripts to intelligently control programs such as telnet, ftp, and ssh, all of which lack a programming language, macros, or any other program mechanism. The result is that Expect scripts provide old tools with significantly new power, and flexibility.

Example (1) non interactive SSH login –

root@arunbagul:/home/arun# cat non-interactive.sh
#!/usr/bin/expect -f
spawn ssh arun@192.168.0.1
expect “password:”
sleep 1
send “mypassword\r”
interact

root@arunbagul:/home/arun#

root@arunbagul:/home/arun# ./non-interactive.sh
spawn ssh arun@192.168.0.1
arun@192.168.0.1’s password:
Linux arunbagul.com 2.6.22-14-generic #1 SMP Tue Feb 12 07:42:25 UTC 2008 i686

The programs included with the Ubuntu system are free software;
You have new mail.
Last login: Sat Mar 1 15:15:05 2008 from arunbagul.com
arun@arunbagul:~$ id=1200(arun) gid=1200(arun)

arun@arunbagul:~$

Example (2) non interactive SCP copy –

root@arunbagul:/home/arun# cat scp.sh
#!/usr/bin/expect -f

# set Variables
set source [lrange $argv 0 0]
set dest [lrange $argv 1 1]
set timeout -1

# connect via scp…
spawn scp $source $dest

#wait for the password to ask…
expect “password:”
sleep 1

#provide password after askinng

send “mypassword\r”

interact
#done
root@arunbagul:/home/arun#

* Now copy data to 192.168.0.1 system –

root@arunbagul:/home/arun# ./scp.sh /tmp/myfile.txt arun@192.168.0.1:/tmp/arun/
spawn scp /tmp/myfile.txt arun@192.168.0.1:/tmp/arun/
arun@192.168.0.1’s password:
myfile.txt 100% 8 0.0KB/s 00:00
root@arunbagul:/home/arun#

* Now copy data from 192.168.0.1 system to your system –

root@arunbagul:/home/arun#./scp.sh arun@192.168.0.1:/tmp/arun/myfile.txt /tmp/
spawn scp arun@192.168.0.1:/tmp/arun/myfile.txt /tmp/
arun@192.168.0.1’s password:
myfile.txt 100% 8 0.0KB/s 00:00
root@arunbagul:/home/arun#
Now you know, how this tool is powerful !!.. You can use this tool for automation purpose and where you don’t want to interact with system now and then use it…

Note:- In above two example script is non interactive means it will not ask you to inter password …. expect will do it for you…!!

Thank you,
Arun

CUPS – Open source printer server

CUPS – Open source printer server

CUPS (Common Unix Printing) system is one of the open source printer server. CUPS provides a portable printing layer for UNIX-based operating systems. It has been developed by Easy Software Products to promote a standard printing solution for all UNIX vendors and users. CUPS provides the System V and Berkeley command-line interfaces.

CUPS uses the Internet Printing Protocol (IPP) as the basis for managing print jobs and queues. The Line Printer Daemon (LPD) Server Message Block (SMB), and AppSocket (JetDirect) protocols are also supported with reduced functionality. CUPS adds network printer browsing and PostScript Printer Description (PPD) based printing options to support real-world printing under UNIX.

1] Why CUPS Printer Server ? –

CUPS is designed to eliminate the printing problem. One common printing system can be used by all UNIX variants to support the printing needs of users. Printer vendors can use its modular filter interface to develop a single driver program that supports a wide range of file formats with little or no effort. Since CUPS provides both the System V and Berkeley printing commands, users (and applications) can reap the benefits of this new technology with no changes. CUPS is based upon an emerging Internet standard called the Internet Printing Protocol (IPP), which a standard protocol for printing as well as managing print jobs and printer options like media size, resolution etc. Like all IP-based protocols, IPP can be used locally or over the Internet to printers hundreds or thousands of miles away. Unlike other protocols, however, IPP also supports access control, authentication, and encryption, making it a much more capable and secure printing solution than older ones.

Backends – Backends perform the most important task of all they send the filtered print data to the printer. CUPS provides backends for printing over parallel, serial, and USB ports, and over the network via the IPP, JetDirect (AppSocket), and Line Printer Daemon (LPD) protocols. Additional backends are available in network service packages such as the SMB backend included with the popular SAMBA software. backends are also used to determine the available devices. On startup each backend is asked for a list of devices it supports.

— printer installation, configuration, quota managment, page log and accounting etc.. will be updated soon

Thank you,

Arun Bagul

How to convert shell script in to binary executable

How to convert shell script in to binary executable

Introduction –

Some time you want your shell script to be binary executable. You can do this by using the tool called Shell script compiler (shc).
shc
creates a stripped binary executable version of the shell script specified with -f on the command line. There is no speed increase from using shc.
Its main purpose is to prevent your shell scripts from being easily modified or inspected. shc can wrap scripts written for any shell.

1] How to install shc on Ubuntu/Debian system –

root@arunbagul:~# apt-get install shc
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following NEW packages will be installed:
shc
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 19.7kB of archives.
After unpacking 90.1kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com gutsy/universe shc 3.8.6-2 [19.7kB]
Fetched 19.7kB in 1s (14.8kB/s)
Selecting previously deselected package shc.
(Reading database … 88932 files and directories currently installed.)
Unpacking shc (from …/archives/shc_3.8.6-2_i386.deb) …
Setting up shc (3.8.6-2) …

root@arunbagul:~#

2] Sample shell script –

root@arunbagul:~# vim myscript.sh
root@arunbagul:~# cat myscript.sh
#! /bin/bash

echo -n “Welcome to shell script…”
echo -e “\n——————————–”
echo -n “Enter the number: ”
read num1

echo -n “Enter the number: ”
read num2

sum=$(expr $num1 + $num2)

echo -n “Sum to $num1 and $num2 = $sum”

echo -e “\ndone.”
root@arunbagul:~# chmod 755 myscript.sh
root@arunbagul:~#

root@arunbagul:~# ./myscript.sh
Welcome to shell script…
——————————–
Enter the number: 40
Enter the number: 50
Sum to 40 and 50 = 90
done.

root@arunbagul:~#

3] Steps to convert shell script to binary executable-

root@arunbagul:~# pwd
/root
root@arunbagul:~# ls
myscript.sh
root@arunbagul:~# shc -r -f myscript.sh

root@arunbagul:~# ls

** check in current working directory the binary file of you shell is created. You can deploy this script on any system (linux/Unix) for that you need to mention the options “-r” as shown in above command. please read shc man pages for more details.

Thank you,
Arun Bagul

Taking backup on Tape

Taking backup on Tape

#!/bin/bash -x
# This script is useful for taking backup on Tape
# Tested on CentOS 3.8, CentOS 4.4
# For further info please login http://indianGNU.org or
# mail ravi <at> indianGNU.org

##### DECLARE AND INITIALIZE VARIABLES #####
### File with the full path names ###
DATE=`date +%m-%d-%g`
LOGDIR=/usr/local/admin/backup
BKDIR=/root/backup
TAPE1=$BKDIR/tape1
TAPE2=$BKDIR/tape2
TOC1=$LOGDIR/backup.$DATE/toc1
TOC2=$LOGDIR/backup.$DATE/toc2
ERR=$LOGDIR/backup.$DATE/errorlogs
GTAR=/bin/gtar
TAPE=/dev/st0
MT=/bin/mt
CP=/bin/cp
mkdir /usr/local/admin/backup/backup.$DATE

### Loop the the backup directory files ###
sleep 60
#$TIME > $TOC1
if [ -f “$TAPE1” ]; then
$MT -f $TAPE rewind;
$GTAR -cvf $TAPE -V “Tape1-Full.$DATE” $TAPE1
$CP $TAPE1 $TAPE2 $LOGDIR
for DIR1 in `cat $TAPE1`
do
$GTAR –exclude “simulation” -cvf $TAPE $DIR1 >> $TOC1 2>> $ERR
sleep 60;
done
$GTAR -cvf $TAPE $TOC1
$MT -f $TAPE rewoff
fi

### Loop the the backup directory files ###
sleep 180
#$TIME > $TOC2
if [ -f “$TAPE2” ]; then
$MT -f $TAPE rewind;
$GTAR -cvf $TAPE -V “Tape2-Full.$DATE” $TAPE2
for DIR2 in `cat $TAPE2`
do
$GTAR –exclude “simulation” -cvf $TAPE $DIR2 >> $TOC2 2>> $ERR
sleep 60;
done
$GTAR -cvf $TAPE $TOC2
$MT -f $TAPE rewoff
fi

#################################################
# Needed Additions to the backup script are: #
# Put table of contents on the end of the tape #
# Rotate through four sets of table of contents #
# in /usr/local/admin/backups #
#################################################

Thanks & Regards

Ravi Bhure 

Me as a Magneteer!!

Me as a Magneteer!!

I was associated with Magnet for 17 months. It was nice experience working with Magnet.I joined Magnet on 10th OCT, 2006, with around 6/7 months experience in networking and Linux. I learnt a lot during this period in Magnet. I have contributed in all areas related to System, Linux and Security in Magnet Technologies. Mr. Nirav (Nirav Mehta, Chief mentor of Magnet Technologies) who is born leader, he always inspire all the Magneteer, through conducting work shop, seminars very often and frequently. He is one of the leading person in Open Source and Free software community. I have participated in few open source conference in India, and for that Nirav Mehta and kartik Mistry has helped me. The other people associated with me are Vishal Kothari (CTO, Magnet), Nilkanth Parab (ex. Team Leader), Ameya Pandit, Ali Asgar, Jayesh Mewada, Nishit Shah, Bhavesh Vala , Suhail Thakur and Kaushik Kawa. These people are associated with me directly or indirectly. I really appreciate all the people for there support, encouragement and guidance.

What I really impressed in Magnet is the work culture of Magnet, Monday Meeting (MM), Monthly review Meeting (MoM) and Hack Festival. Thanks to Mr. Ashok (Ashok Karania, CEO and MD of Magnet Technologies) for putting wonderful idea of MM. On every Monday at 3:00pm, the Monday Meeting (MM) is schedule and which will go around 1 hour, with presentation by magneteer, appreciation notes etc. I am also thankful to HR (Aarti More, Sarita Hegde and Reshma T) for organizing cultural events, Fun Friday, Saturday Blast, Monday blast etc..

I wish all the best to all Magneteer and Magnet Technologies.

Wish you great future ahead!!

Thank you,

Arun Bagul

Difference between TIMESTAMP DEFAULT, CURRENT_TIMESTAMP, ON UPDATE CURRENT_TIMESTAMP in MYSQL

Difference between TIMESTAMP DEFAULT, CURRENT_TIMESTAMP, ON UPDATE CURRENT_TIMESTAMP in MYSQL

TIMESTAMP DEFAULT :- in this when new row get inserted or existing row get updated that time current timestamp get inserted.

CURRENT_TIMESTAMP :- in this when row get inserted that time current timestamp get inserted.no change on update.

ON UPDATE CURRENT_TIMESTAMP :- when row get affected that time current timestamp get inserted.

Regards

Prasanna Shejwal