Sunday 11 December 2011

shell script - awk tips

Print column1, column5 and column7 of a data file or output of any columns list
$awk ‘{print $1, $5, $7}’ data_file
$cat file_name |awk ‘{print $1 $5 $7}’
$ls –al |awk ‘{print $1, $5, $7}’ -- Prints file_permissions,size and date

Syntax of running an awk program
Awk ‘program’ input file(s)

List all files names whose file size greater than zero.
$ls –al |awk ‘$5 > 0 {print $9}’

List all files whose file size equal to 512bytes.
$ls –al |awk ‘$5 == 0 {print $9}’

print all lines
$awk ‘{print }’ file_name
$awk ‘{print 0}’ file_name

Number of lines in a file
$awk ‘ END {print NR}’ file_name

Number of columns in each row of a file
$awk ‘ {print NF’} file_name

Sort the output of file and eliminate duplicate rows
$awk ‘{print $1, $5, $7}’ |sort –u

List all file names whose file size is greater than 512bytes and owner is "oracle"
$ls –al |awk ‘$3 == "oracle" && $5 > 512 {print $9}’

List all file names whose owner could be either "oracle" or "root"
$ls –al |awk ‘$3 == "oracle" || $3 == "root" {print $9}’

list all the files whose owner is not "oracle
$ls –al |awk ‘$3 != "oracle" {print $9}’

List all lines which has atlease one or more characters
$awk ‘NF > 0 {print }’ file_name

List all lines longer that 50 characters
$awk ‘length($0) > 50 ‘{print }’ file_name

List first two columns
$awk ‘{print $1, $2}’ file_name

Swap first two columns of a file and print
$awk ‘{temp = $1; $1 = $2; $2 = temp; print }’ file_name

Replace first column as "ORACLE" in a data file
$awk ‘{$1 = "ORACLE"; print }’ data_file

Remove first column values in a data file
$awk ‘{$1 =""; print }’ data_file

Calculate total size of a directory in Mb
$ls –al |awk ‘{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}’

Calculate total size of a directory including sub directories in Mb
$ls –lR |awk ‘{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}’

Find largest file in a directory including sub directories
$ls –lR |awk ‘{print $5 "\t" $9}’ |sort –n |tail -1

Power path installation on hp-ux for load balancing

To install power path :
#swinstall -x autoreboot=true -x mount_all_filesystems=false -s /tmp/EMCPower.HP.4.1.0.tar EMCpower

#emcpreg -install (#Enter registration key)

#powermt config

#powermt set policy=so dev=all

# powermt display

Run powervadm list to confirm unique volume group naming and storage elements do not belong to any other PPVG volume group on the host.

#powervadm list

To create VG & lvol :
#powervg create -vg oracle10g -se c22t0d0 c22t0d1 c22t0d2 –continue

#powervol create -vg oracle10g -vol ora100gbr5v08 -ua -se c24t1d0

To view volume group information

#powervadm [list|show]

#powervadm show -tab

To view information about a specific volume group

#powervg [list|show] –vg volumename –vol

#powervg [list|show] –vg volumename –se

The -vol and the -se arguments are mutually exclusive.

Backup metadata

#powervmeta backup -vg oracle10g -f /tmp/oracle10g_metadata.81390

Destory Volumes and Volume Groups run the following commands

#powervol destroy –vg oracle10g –vol ora22gbr1v01

#powervg destroy –vg oracle10g

Disable root SSH login : How to ??

Disable Root SSH Login on HP UX

# cd /opt/ssh/etc

# vi sshd_config

Change the line:

PermitRootLogin yes

change into like below

PermitRootLogin no

save and exit

Then stop and restart SSH.

# /sbin/init.d/secsh stop

# /sbin/init.d/secsh start

You will then NOT be allowed to ssh as root.

==============================================


Disable Root SSH Login on Linux


#vi /etc/ssh/sshd_config

Find this section in the file, containing the line with “PermitRootLogin” in it.

#LoginGraceTime 2m
#PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6

Make the line look like this to disable logging in through ssh as root.

PermitRootLogin no

Then restart the sshd service:

# /etc/init.d/sshd restart

Wednesday 30 November 2011

How to get cpu / processor / core info in Linux?

To get processor info we need to run below commands in Linux

1. #cat /proc/cpuinfo

or

2. #dmidecode

To get the core info in linux use the below commands. If the processor is dual core or quad core or single core processor means we will get the "core id"value in the below commands. If no core (single core) processor means we wont get the core id . so from the word "core id" we can get the processor is core processor or physical processor.

For examble here i took 2 servers ..

server1 - has 4 physical - single cored processors

server 2 - has 2 physical - dual core processors

4 Physical processor with no core (single core) means we will get o/p like below


root:server1# egrep "^processor|^cpu cores|^core id" /proc/cpuinfo
processor : 0
processor : 1
processor : 2
processor : 3
root:server1#

To get core info (whether core is there or not)

root:server1# grep core\ id /proc/cpuinfo |uniq -d |wc -l
0

To get total no of physical processor

root:server1# grep core\ id /proc/cpuinfo | grep -c \ 0$ | grep ^0$ >> /dev/null && grep -c processor /proc/cpuinfo || grep core\ id /proc/cpuinfo | grep -c \ 0$
4


Two dual core processor means (2- physical processor) we will get o/p like below


root:server2# egrep "^processor|^cpu cores|^core id" /proc/cpuinfo
processor : 0
core id : 0 ----------------- processor 1
cpu cores : 2
processor : 1
core id : 0 ----------------- processor 1
cpu cores : 2

processor : 2
core id : 1 ----------------- processor 2
cpu cores : 2
processor : 3
core id : 1 ----------------- processor 2
cpu cores : 2

root:server2#

To get core info run the below command

root:server2# grep core\ id /proc/cpuinfo |uniq -d |wc -l -------> this will give how many cores are there / if no core means it will give out put as 0.
2


To get how many physical processor is there on server ,then run below command

root:server2# grep core\ id /proc/cpuinfo | grep -c \ 0$ | grep ^0$ >> /dev/null && grep -c processor /proc/cpuinfo || grep core\ id /proc/cpuinfo | grep -c \ 0$
2


====================================

Friday 25 November 2011

Mount DVD in unix ?

Use the following procedure to mount a DVD-ROM on HP-UX 11i v2 (B.11.23):

1. Log in as root
2. Insert the DVD into the DVD-ROM drive and wait for the drive's busy light to stop blinking.
3. Find the DVD-ROM device file name using the following command:

ioscan -fn -C disk | more
/dev/dsk/c1t2d0 is a typical device name.

4. Create the mount point under root(/):

mkdir /dvdrom

5. Mount the DVD onto the new directory as a file system. For example, the following command mounts the /dev/dsk/c1t2d0 device as the /dvdrom directory:

mount /dev/dsk/c1t2d0 /dvdrom

Route add in HP Unix

Let us assume your router address is 192.168.1.254 and network ID is 192.168.1.0/24, then you can type route command as follows:
# route add net 192.168.1.0 netmask 255.255.255.0 192.168.1.254
OR
To add a default route:
# route add default 192.168.1.254
Verify that (display) routing table is updated (display routing table):
# netstat -nr
Test it i.e. try to ping or send nslookup request:
# ping mycorp.com
To flush all routing entries use command [quite handy to clean your gordian knot ;)] :
# route -f
However if I reboot HPUX box then above routing entries gets removed. To pick up your setting upon each reboot your need to configure Routes in HPUX networking configuration file - /etc/rc.config.d/netconf. To add default router/gateway 192.168.1.254:

# vi /etc/rc.config.d/netconf
Add or modify following entries
ROUTE_DESTINATION[0]="default"
ROUTE_MASK[0]=""
ROUTE_GATEWAY[0]="192.168.1.254"
ROUTE_COUNT[0]="1"
ROUTE_ARGS[0]=""
Reboot HP-UX system/server to take effect

# shutdown -ry 0

Bundle All Patches in One Big Bundle - HP Unix

Creating one big bundle with all the extra patches you need to install will save your time during the upgrade/patching process and also prevent multiple reboots of your servers.

Create folder '/var/patches' with 'depot' and 'add' subfolders and copy the Quality Pack into '/var/patches/depot' and additional patches into '/var/patches/add' folder. Next you need to unpack the Golden Pack:

# swcopy -s /var/patches/depot/GOLDQPK11i_B.11.11.0612.459.depot \* @ /var/patches/depot/QPK

With swcopy copy all additional patches into this unpacked depot.


# for patch in /var/depot/app/*; do
echo ${patch}
swcopy -x enforce_dependencies=FALSE \
-x mount_all_filesystems=FALSE \
-x reinstall=TRUE -s ${patch} \* @ \
/var/patches/depot/QPK >> swcopy_patchs.log 2>&1 Verify that all the packages in the new bundle we create are OK.

# swverify -d -x mount_all_filesystems=FALSE \
-x enforce_dependencies=FALSE \* @ /var/patches/depot/QPK

Now, we have to revert the bundle into the original format for easier distribution.

# swpackage -x media_type=tape -s /var/patches/depot/QPK @ \
/var/patches/depot/GOLDQPK11i_B.11.11.0612.459.NEW.depot

You can use this file as any other regular depot file.

# swinstall -i -x patch_match_target=true -x autoreboot=true -s \
/var/patches/depot/GOLDQPK11i_B.11.11.0612.459.NEW.depot