Thursday, February 26, 2009

Solaris Zone Basics

# uname -a
SunOS nido 5.10 Generic_118844-26 i86pc i386 i86pc

# pwd
/
create a Zone
# zonecfg -z
nidoz3: No such zone configured
Use 'create' to begin configuring a new zone.
zonecfg:nidoz3> create
zonecfg:nidoz3> set autoboot=true
zonecfg:nidoz3> set zonepath=/nidoz3
zonecfg:nidoz3> add net
zonecfg:nidoz3:net> set address=137.72.420.420/24
zonecfg:nidoz3:net> set physical=e1000g0
zonecfg:nidoz3:net> end
zonecfg:nidoz3> verify
zonecfg:nidoz3> commit
zonecfg:nidoz3> exit
#
# zoneadm list –cv
# cat /etc/zones/nidoz3.xml
# mkdir /nidoz3
# chmod 700 /nidoz3
# zoneadm -z nidoz3
# zoneadm -z nidoz3 install
#zoneadm list –cv

#zoneadm -z nidoz3 boot
#zoneadm list -cv
#zlogin -C nidoz3

Add File-System: Mount FileSystem in Global Zone:
zonecfg:nidoz3> add fs
zonecfg:nidoz3:fs> set dir=/opt
zonecfg:nidoz3:fs> set special=/nidoz4_1/opt
zonecfg:nidoz3:fs> set type=lofs
zonecfg:nidoz3:fs> set options=rw,nodevices
zonecfg:nidoz3:fs> end

Remove a FileSystem in Non-Global Zone
global# zonecfg -z zonename
remove fs dir=/usr/local/mysql/data

To get the Non-Global Zone Information:
# cat /etc/zones/nidoz3.xml
Or
zonecfg:nidoz1> info
Or
# zoneadm list –cv
Or
# zonecfg -z nidoz1 export

To Set the IP Address for Non-Global Zone
ifconfig e1000g0:3 plumb 137.72.420.420 netmask 255.255.255.0 zone nidoz3 up

To Delete a Non-Global Zone:
# zoneadm list –cv
# zoneadm -z nidoz3 halt
# zoneadm list –cv
# zoneadm -z nidoz3 uninstall
# zoneadm list –cv
# zonecfg -z nidoz1 delete
# zoneadm list –cv
-----------------------------------------------------------------------------------------
Refer the Following URL for zone create script: (Not tested yet but will soon be )

timepass - Ping Host Table

#!/bin/ksh
clear
echo "Do you want to flush the log data before running ping test (y/n): "
read choice
case $choice in
y)
cat /dev/null > /tmp/pnaidu/err.txt
cat /dev/null > /tmp/pnaidu/ok
cat /dev/null > /tmp/pnaidu/NOTOK

for A in `awk '{print $1}' /script/testpnhost.txt`
do
ping ${A} -n 1 >>/tmp/pnaidu/out.txt 2>>/tmp/pnaidu/err.txt
if [ $? = 0 ]; then
{
echo "${A} is able to connect" >> /tmp/pnaidu/ok
}
else
{
echo "${A} NOT able to connect" >> /tmp/pnaidu/NOTOK
}
fi
done
echo "Hosts which couldn't ping throw them all from the 7th Floor"
echo "Check the log files under /tmp/pnaidu \n "


;;

n)
echo "Hasta la vista"
exit 0
;;
*)
echo "Invalid Choice "
echo "Exiting....... Wake up and get a cup of coffee"
exit 1
esac

expect scripting

Nice way to start expect would be to first visit: http://expect.nist.gov/

Expect is mainly used to automate the task for some applications which require manual interaction such as telnet, ftp, passwd, fsck, rlogin, tip, etc.

Enough of the introduction part for expect.


Example(1): will telnet to a host named as `nido` it'll put login and password credentials and then will surely can run either of the commands ( I know, one command will fail)...

_______________________________________________

#!/usr/local/bin/expect
spawn telnet nido
expect "login"
send "root\r\n"
expect "Password:"
send "whoami\r\n"
expect -re {\#}
send "/usr/contrib/bin/machinfo| mailx parwatraj@yahoo.com/r"
expect -re {\#}
send "ls -l\r"
expect -re {\#}
send "/opt/ignite/bin/print_manifest| mailx parwatraj@yahoo.com/r"
expect -re {\#}
exit
_______________________________________________________
Example(2): will telnet or ssh to a list of hosts which are in a text file `/script/testpnhost.txt` and will do the same task as we've seen on the above example.
Excute testpn4 and see how it works for you.
Modify the contents of expect script as per your need.

---------------------------------------
# cat testpn4
#!/bin/sh
for A in `cat /script/testpnhost.txt`
do
echo ${A}
echo "__________________________"
/script/testpn5.exp ${A}
done
---------------------------------------------------------------

# cat testpn5.exp
#!/usr/local/bin/expect
spawn telnet [lindex $argv 0]
expect "login"
send "root\r\n"
expect "Password:"
send "whoami\r\n"
expect -re {\#}
send "/usr/contrib/bin/machinfo| mailx parwatraj@yahoo.com/r"
expect -re {\#}
send "/opt/ignite/bin/print_manifest| mailx parwatraj@yahoo.com/r"
expect -re {\#}
exit
---------------------------------------------------------------------