Wednesday, August 25, 2010

Getopts in Bash shell Scripts

### Example of Getopts ###

Getopts Multi Option Parsing ...
01    #!/bin/bash    // Run time parameters
02   
03    function help {
04        echo "Usage: test -p"
05    }
06   
07    if test $# -eq 0; then
08        help
09        exit 0
10    fi
11   
12    exit 0

If you don't give any parameters, it prints the help message. But... what about checking the parameter itself? Thats where getopts comes into play! First of all, look at the following script, which adds the getopts to the above script...

01    #!/bin/bash
02   
03    function help {
04        echo "Usage: test -p"
05    }
06   
07    if test $# -eq 0; then
08        help
09        exit 0
10    fi
11   
12    while getopts "p" option; do
13        case $option in
14            p) echo "this is a test";;
15            *) help;;
16        esac
17    done
18   
19    exit 0

#### How it works

Each time the while loop is executed, getopts puts the next parameter into the variable option. The parameters desired, must be defined as a string, containing them one by one.

If you want to accept the parameters a, b, and c, the loop should be:

while getopts  "abc" var; do
2        case $var in
3            a) echo "parameter a given";;
4            b ) echo "parameter b given";;
5            c) echo "parameter c given";;
6            *) echo "Usage: script -abc";;
7        esac
8    done

### Accepting Arguments   
Getopts gives you a way to accept arguments for a parameter too! Just put a  :  after the parameter's name. Like this:

1    while getopts "a:bc"  var; do
2        case $var in
3            a) echo "parameter a given, it's argument is $OPTARG";;
4            b ) echo "parameter b given";;
5            c) echo "parameter c given";;
6            *) echo "Usage: script -a message -bc";;
7        esac
8    done

As understood from above script, the corresponding argument for a parameter is inside the variable OPTARG. So, you can easily manage it
  

Sunday, August 15, 2010

Sun-Solaris

############################ ZFS ######################################
Zettabyte File System (ZFS)
CLI,GUI,Mirroring,Raid-z,snapshots,clones
Features:
supports Very large Storage Space  --
It can address 256 Quadrilion Zettabytes
Quadrilion - 1 million

1  256 Quadrillion Zettabytes (Terrbytes - Perabytes - Exabytes - Zettabytes ) ( 1024 Exabytes  - 1 Zettabyte)
2  File system for the Feature
3. RAID -0/1 Mirroring,striping  & RAID-Z ( RAID-5 with enhancements ) ( 2-required virtual  devices )
4. Snapshots - Great Features - Read-only copies of file systems or volumes.. Be able to take a snapshot of current filesytem ...can revert back to the previous file system
5. Uses Storage Pools to manage storage - aggregates virtual devices .. Since the filesystems are attached to pools... They can dynamically grow
6. File Systems attached to pools grow dynamically as storage is added
7. We can attach the filesystem with out interupting any transaction
8. File systems may span multiple physical disks
9. ZFS is transactional (less likely to corrupt data)
  Eg; Traditional file system 100 MB 80% is written 20% failed due to some reason which leads to data corruption...
         But in zfs if it writes total 100 or nothing written ... After writing 100 MB it will do commit... so there is less channe of data  corruption. Important feature of mission critical information .. 100 Mb will be written or nothing will be written
10. Pools & file systems are auto-mouted. NO need to maintain /etc/vfstab ... (Virtual file system tab ) Pools should have unique names ... Pools name must be unique... Within pool filesystem should also be unique
11. Supports file system hierarchies: /pool1/{home (5GB) ,var (10 GB) ,etc (15 gb)}
12. Supports reservation of storage: 36  /pool1/{home,var} .. We put reservation .. ensuring the home always get 10Gb ...
      Inshort a specific filesystem will have it's specified size always...
13. Provides a secure web-based management tool - https://localhost:6789/zfs

Note: Compiling Reasons... Above about Zfs file system

################# ZFS CLI ###########################
Command Line Interface
which zpool
zpool list - lists known pools
zpool create pool_name(alphaanumeric,_,-,:,.)
Pool Name Constraints: Reserved name (Do Not Use These Names For your Pool Names):
1. mirror
2. raidz

zpool create pool_name devices_name1, device_name2, device_name3, etc
Eg;
zpool create pool1 c0t1d0 | /dev/dsk/c0td10
Note: format (searches for disks )
It will scan for the connected disks.

Eg:
- zpool create pool1 c0t1d0
- echo $?
- mount
- ls -l /pool1/
- zpool list

ZFS Pool Statuses:
1. ONLINE - available
2. DEGRADED - failed or mirror failed
3. FAULTED - In Accessible, Before removing a HD make it offline a remove it
4. OFFLINE
5. UNAVAILABLE

zfs list - returns ZFS dataset info
zfs mount - returns pools and mount points
zpool status - returns virtual devices - The most important command to run ... probably after creation of pools
zpool status -v pool_name - To get verbose information about the pool

Note: ZFS requires a minimum of 128 MB virtual device to create a pool

zpool destroy pool1 - Destroys pool and associated file systems

############## Create file systems within pool1 ##############
zfs create pool1/home - creates file system named 'home' in pool1
home is subset of pool1
Note: Default action of 'zfs create pool1/home' assigns all storage available to 'pool1`, to 'pool1/home'

############# Set Quota on existing file System ########
- zfs set quota=10G pool1/home
- zfs list

############## Create user-based file system beneath pool1/home ###########
- zfs create pool1/home/vxadmin ( We can specify the size as well)
- zfs list
Note: ZFS inherits properties from immediate ancestor
- zfs set quota=2G pool1/home/gyani
- zfs get -r quota pool1
- zfs get -r compression pool1 - returns compression property for file systems associated with 'pool1'
   Note: Be default the compression will be in off state to the filesystems

############# Rename File System ####################
zfs rename pool1/home/unixcbt pool1/home/unixcbt2

########## Extending dynamically, Pool #####################
- format - Search out the available disks
- zpool add pool1 c0t2d0 (device_name) [ Able to address dynamically added storage ]
- zfs list
- zpool status

################## ZFS WEB GUI #############################3

ls -ltr /usr/sbin/smcwebserver
netstat -anP -tcp | grep 6789
Note: By Default Nfs doesn't share, due to security reason
legacy filesystem - means /etc/vfstab

############## ZFS Redundancy/Replication ################
1. Mirroring - RAID-1
2. RAID-5 - RAID-Z

Virtual Devices:
#Mirroring
- zpool create poolmirror1 mirror c0t1d0 c0t2d0
- zfs create poolmirror1/home
- zfs set quota=10G poolmirror1/home

# RaidZ
2 - minimum number of disks required
format -> 1 -> partition -> print
#Create the pool
/usr/sbin/zpool create -f poolraidz1 raidz c0t1d0 c0t2d0
- zfs list
# Change a mount point
/usr/sbin/zfs set mountpoint=/poolraidz2 poolraidz1
zfs et quota=10G poolraidz1/home
# Change a mount point back to inherited
/usr/sbin/zfs inherit mountpoint poolraidz1

########### ZFS Snapshots/Clones #################

Snapshots allows as to create a readonly copy of file systems or volume
Commerical products like NetApps,SAN,EMC's similar capabilities
Features:
1. Read-only copies of volumes or file systems
2. Use no additional space, initially

- zfs list -t snapshot - returns available snapshots
#snap shot syntax
- zfs snapshot poolraidz1/home@homesnap1
- zfs list -t snapshot
- snapshots are stored inside the hidden directory
/poolrraidz1/home/.zfs/snapshot/homesnap1
#Destroy the snapshot
- zfs destroy poolraidz1/home@homesnap1
# Rename Snapshot
- zfs rename poolraidz1/home@homesnap3 poolraidz/home@homesnap20060703
- zfs list -t snapshot
# Snapshots Roleback... It has to unmount and mount
- zfs rollback -f poolraidz1/home@homesnap20060703
### Clones
Clones are writeable copies.
Features:
1. Writable file systems or volumes
2. Linked to a snapshot... We cannot create a clone withoaut snapshot
3. Clone can be stored anywhere in ZFS hierarchy

###ZFS Clone
- zfs clone poolraidz1/home@homesnap20060703 poolraidz1/homeclone1
Note: Clones will inherit the attributes whereas snapshots won't inherits anything.
Note: clone is writeable whereas snapshot is not
Note: It we delete the snapshot... It will delete the clone as well.. Directly propotional

### Zfs Share
ZFS specific Settings :
zfs sharenfs=on vxpool
zfs set sharenfs=rw=@10.10.20.0/24,root=@10.10.20.0/24 vxpool

sharemgr show -pv
sharemgr show -pvx

Ninja/ssh tunneling Tips

########### SSH Port Forwarding #################
Facilitates Local & Remote Port forwarding
1. Local - means to forward a port on the local system to a remote system
2. Remote - means to forward a remote port to our local host

Local Port : -L next to localport, and Remote IP
e.g. ssh -L 9000:10.10.20.290:5900 root@IP
Remote Port: -R next to RemotePort, and Localhost
e.g. ssh -R 4444:localhost:22 root@ip

Reverse SSH Tunnel

A reverse tunnel is just like the tunnel we set up with -L, except it allows the destination machine to connect to the client machine.

In the example from this article, you can create a reverse tunnel from Server to Netbook allowing Netbook to reconnect to Server. The following command, when run from Server, connects to Netbook and creates a tunnel from port 4444 on Netbook to the SSH dæmon on Server. Any shell on Netbook then can connect to Server via port 4444:

Private Server No ssh is allowed Directly
ssh -R 4444:localhost:22 load_runner
Note: -R next to port is the Remote Port (load_runner), Make sure 4444 port is not being used on load_runner.

The following command, when run from load_runner host machine, would connect to Server via the reverse tunnel:

ssh -p 4444 localhost (load_runner)
Note: You will jump into the Private Server from where the port it mapped

Forward SSH Tunnel

Many applications use protocols where passwords and data are sent as clear text. These protocols include POP3, IMAP, SMTP and NNTP. SSH can encrypt these connections transparently. Say your e-mail program normally connects to the POP3 port (110) on mail.example.net. Also, say you can't SSH directly to mail.example.net, but you have a shell login at shell.example.net. You can instruct SSH to encrypt traffic from port 9110 (chosen arbitrarily) on your local computer and send it to port 110 on mail.example.net, using the SSH server at shell.example.net:

ssh -L 9110:mail.example.net:110 shell.example.net
Note: -L next to port is the localhost port

That is, send local port 9110 to mail.example.net port 110, over an SSH connection to shell.example.net.

Then, simply tell your e-mail program to connect to port 9110 on localhost. From there, data is encrypted, transmitted to shell.example.net over the SSH port, then decrypted and passed to mail.example.net over port 110. As a neat side effect, as far as the POP3 dæmon on mail.example.net knows, it is accepting traffic from shell.example.net.
Tunneled Connections

SSH can act as a bridge through a firewall whether the firewall is protecting your computer, a remote server or both. All you need is an SSH server exposed to the other side of the firewall. For example, many DSL and cable-modem companies forbid sending e-mail from your own machine over port 25 (SMTP).

Our next example is sending mail to your company's SMTP server through your cable-modem connection. In this example, we use a shell account on the SMTP server, which is named mail.example.net. The SSH command is:

ssh -L 9025:mail.example.net:25 mail.example.net
Note: -L next to is the localhost port, make sure the port it open

Then, tell your mail transport agent to connect to port 9025 on localhost to send mail. This exercise should look quite similar to the last example; we are tunneling from local port 9025 to mail.example.net port 25 over mail.example.net. As far as the firewall sees, it is passing normal SSH data on the normal SSH port, 22, between you and mail.example.net.





LOCAL:
Flow: Client -> Port(2323) -> SSH- Tunnel Remote Host (2323)
Syntax:
ssh -L 2323:DestinationHost:2323 SSHD_Router_Server
Note: Port Forwarding in Solaris 10 supports ONLY TCP traffic
Note: -L next to is the localhost port 

ssh -L 2323:linuxcbtmedial:80
Note: -L next to is the localhost port, which is being mapped to port 80

Note: Ensure that local port is free, and destination port is listening
Note: Default port forwarding provides connectivity ONLY to localhost
Cross-Check : telnet localhost 2323  Use ^] to print the web-page
netstat -anP tcp | grep 2323
rcapache2 start
rcsshd restart
svcs -l apache2 (services list) - maintenace mode
svcadm clear apache2 (service adm)
svcs -l apache2 - online

#### Remote-Desktop
rdesktop -f -a 16 ip

###### Remote Port Forwarding ###########
Note: Remote port forwarding instructs remote server's SSHD to bind to a port that becomes available to the remote system's users
ssh -R 2424:LocalHost:80 linuxcbtmedia1
ssh -R 2424:localhost:80 linuxcbtmedia1
Note: -R next to is the Remote port of the machine

### Share locally and remotely forwarded ports ###
ssh -g -L 2323:linuxgoogle:80 linuxgooglel (Makes available in the entire subnet)
ssh -g -R 2424:localhost:80 linuxgoogle

##Remote forwarded port  #### Reverse Tunnel
ssh  -R2245:127.0.0.1:22 load_runner


## Jump into Real Machine
ssh load_runner@127.0.0.1 -p 2245


#################
/bin/ps -ef | grep dhclient | sed -n '1p' | awk {'print $2'} | xargs kill -9

#Exec
The exec() family of functions will initiate a program from within a program. They are also various front-end functions to execve().
The functions return an integer error code. (0=Ok/-1=Fail).
An exec command redirects stdin to a file

#fd
File descriptors 0, 1 and 2 are reserved for stdin, stdout and stderr respectively. However, bash shell allows you to assign a file descriptor to an input file or output file. This is done to improve file reading and writing performance. This is known as user defined file descriptors.
exec fd> output.txt
    * where, fd >= 3
Eg:
exec 3> /tmp/output.txt
echo "This is a test" >&3
date >&3
exec 3<&-

############# Iptables #################

route add -net 10.10.20.0/24 gw 192.168.155.138 ( Making gateway as the vx64 box )
ping 10.10.20.81 ( pinging the compute node ) (If not pinging add iptables rules to vx64 box)

iptables -t nat -A POSTROUTING -o eth+ -j MASQUERADE
cat /proc/sys/net/ipv4/ip_forward
echo "1"> /proc/sys/net/ipv4/ip_forward

 ## Parameters Passing in Bash Shell scripts ##

This is a simple alternative to using getopts to parse parameters in a BASH shell script which makes use of the powerful parameter substitution functions in BASH. It should be sufficient for most scripts:

until [[ ! "$*" ]]; do
  if [[ ${1:0:2} = '--' ]]; then
    PAIR=${1:2}
    PARAMETER=`echo ${PAIR%=*} | tr [:lower:] [:upper:]`
    eval P_$PARAMETER=${PAIR##*=}
  fi
  shift
done
The script processes parameters in the format --name=value or --flag

Example output
./test.sh --number=123 --show
+ [[ ! -n --number=123 --show ]]
+ [[ -- = \-\- ]]
+ PAIR=number=123
+ echo number=123
number=123
++ echo number
++ tr '[:lower:]' '[:upper:]'
+ PARAMETER=NUMBER
+ echo NUMBER
NUMBER
+ eval P_NUMBER=123
++ P_NUMBER=123
+ shift
+ [[ ! -n --show ]]
+ [[ -- = \-\- ]]
+ PAIR=show
+ echo show
show
++ echo show
++ tr '[:lower:]' '[:upper:]'
+ PARAMETER=SHOW
+ echo SHOW
SHOW
+ eval P_SHOW=show
++ P_SHOW=show
+ shift
+ [[ ! -n '' ]]
+ set +x


Thursday, August 5, 2010

how to Crimp cat5/cat6 cables

''' To obtain a straight cable, the tips must be crimped typically the same way at each end by respecting the twisted pair size
'''


1) orange-white
2) orange
3) green-white
4) Blue
5) blue-white
6) Green
7) brown-white
8) brown



'''To obtain a crossover cable, reverse the parts 1 / 2 and 3 / 6, so on the one hand the same cable as above, and the other as follows:'''
Note: Replace 1st position with 3rd position color and 2nd position with 6th position Then it become "Cross-over Cable"


1) green-white
2) green
3) orange and white
4) Blue
5) blue-white
6) orange
7) brown-white
8) brown