Wednesday, October 2, 2013

A sample PowerCLI script for iterating multiple vCenters

Add-PsSnapin VMware.vimAutomation.core
$vcs=@()
$vcs +="vCenter1"
$vcs +="vCenter2"
$vcs +="vCenter3"
foreach ($vc in $vcs)
{
"Connecting to $vc"
Connect-VIServer -server $vc -user xxx -password xxx
"Connected to $vc"
}
...
Disconnect-VIServer *

Wednesday, January 23, 2013

Some SED tips for my records

To delete trailing whitespace from end of each line:
# sed 's/[ \t]*$//' filename > filename_notrailingspace

To remove all blank lines:
# sed '/^$/d' filename > filename_noblankspace

To remove all leading and trailing whitespace from end of each line:
$ cat filename | sed 's/^[ \t]*//;s/[ \t]*$//' > filename_nospace

Sunday, September 2, 2012

Perl script to check Kernel parameter differences between 2 Linux machines

This Perl script can be used for checking the Kernel Parameters difference between 2 Linux machines.
All you got to do is, take the ‘sysctl –a’ output into a file from each Linux machine that you wish to compare and give it as an input for this script like shown below:
Upon execution, you need to choose option 1 or 2.  ‘1’ is for listing out all the kernel parameter values of both servers in tabular form and ‘2’ for listing out only the parameters that are either differing or non-exist on other server.
 # perl  <scriptname>.pl  <first_server_kernel.txt>  <second_server_kernel.txt>



#!/usr/bin/perl
# Description   :       This script can be used to list out of the differences in the Kernel Settings between 2 Linux Servers.        
#                       It takes 2 files (consists of sysctl -a output) as input, upon execution it asks for User's choice either      
#                       to List out all the Kernel parameter of 2 Servers or to list out only the Differences.                        
# Version       :       1.1                                                                                                            
# Execution method :    Manual (by any normal user)                                                                                    

if( @ARGV != 2 )
{
   print "\nUsage: $0 <First-file> <Second-file>\n\n";
   print "First-file should contain 'sysctl -a' output taken from first Linux Machine\n\n";
   print "Second-file should contain 'sysctl -a' output taken from Second Linux Machine\n\n";
   exit 1;
}

open(File1, "$ARGV[0]" );
open(File2, "$ARGV[1]" );

my @first_file = <File1>;
my @second_file = <File2>;
my %output1, %output2, %kerparm;
my ($cnt1,$cnt2) = (0,0);

print "\nEnter '1' for Listing all Kernel parameters or '2' to list only the Differences : ";
my $choice=<STDIN>;
chomp($choice);

system $^O eq 'MSWin32' ? 'cls' : 'clear';
printf("%-50s %-25s %-50s\n\n", "KERNEL PARAMETER", "FIRST SERVER", "SECOND SERVER");

foreach $i(@first_file) {
        $i =~ s/\s+$//;
        @array1 = split(" = ",$i);
        $output1{$array1[0]} = $array1[1];
        $cnt1++;
        $kerparm{$array1[0]} = $cnt1;
}

foreach $j(@second_file) {
        $j =~ s/\s+$//;
        @array2 = split(" = ",$j);
        $output2{$array2[0]} = $array2[1];
        $cnt2++;
        $kerparm{$array2[0]} = $cnt2;
}

foreach $key (sort keys %kerparm)
{
  $output1{$key} = "" unless defined $output1{$key};
  $output2{$key} = "" unless defined $output2{$key};
  if($choice eq '1') {
          printf("%-50s %-25s %-50s\n", $key,$output1{$key},$output2{$key}); }
  elsif($choice eq '2') {
          printf("%-50s %-25s %-50s\n", $key,$output1{$key},$output2{$key}) if ($output1{$key} ne $output2{$key});
  }
}

Friday, August 24, 2012

PowerCLI script to get the status of CPU/Memory Hot-Plug in vSphere5

Get-VM | Get-View | Select Name, `
@{N="CpuHotAddEnabled";E={$_.Config.CpuHotAddEnabled}}, `
@{N="CpuHotRemoveEnabled";E={$_.Config.CpuHotRemoveEnabled}}, `
@{N="MemoryHotAddEnabled";E={$_.Config.MemoryHotAddEnabled}} | Export-Csv C:\<path>\file.csv

Wednesday, August 22, 2012

Bash script to stop and disable list of services in Linux

# Defining List of unwanted services
SERVICES="rhnsd sendmail cups netfs autofs nfslock mdmonitor isdn cpuspeed rpcidmapd rpcgssd iptables xfs pcmcia smartd"

# Finding each service status
for service in $SERVICES; do
  if [ -f /etc/init.d/$service ]; then
     # Turning off the service
      chkconfig $service off
     if_running=`service $service status | egrep -i running`
      # Stopping the service if it is running
      [ ! -z "$if_running" ] && service $service stop
  fi
done

Tuesday, August 21, 2012

Sample script to change JDK version

#!/bin/bash
# Purpose      :  For changing JDK version to 1.6.0_14 non-interactively. This script could be useful for
#                      changing the JDK version automatically on multiple servers. I have used this script in a
#                      production change where we got to update JDK version simultaneously on 60+ servers.  
# Assumption :  The Binary version of JDK version 1.6.0_14 already installed under /usr/java/jdk1.6.0_14"

/usr/sbin/alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_14/bin/java 1
/usr/sbin/alternatives --set java /usr/java/jdk1.6.0_14/bin/java

/usr/sbin/alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_14/bin/javac 1
/usr/sbin/alternatives --set javac /usr/java/jdk1.6.0_14/bin/javac

unlink /usr/bin/java
ln -s /usr/java/jdk1.6.0_14/bin/java /usr/bin/java

sed -i.bak '/export/ i JAVA_HOME=/usr/java/jdk1.6.0_14\nPATH=$PATH:$JAVA_HOME/bin\nJAVAPTH=$JAVA_HOME/bin\n' /etc/profile

sed -i.bak 's/export.*$/export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC JAVA_HOME JAVAPTH/' /etc/profile

java -version 2&> java_ver

java_ver=`cat java_ver | grep java | cut -c 15-22`

if [ "$java_ver" != "1.6.0_14" ]; then
echo -e "Updating JDK version to 1.6.0_14 Failed. Please check manually.\n"
exit 1
else
rm -f ./java_ver
echo -e "\n\n\nJDK version has been successfully changed to 1.6.0_14.\n\n"
fi

Friday, July 27, 2012

How to debug a Bash shell script ?

When executing a shell script use 'bash' command with below options to debug a shell script in effective way. 
 -x if you set this option, it will show you each line before it executes it. Comments will not be reported.
sh -x scriptname.sh
-v Echo’s each line as it is read. It’s a kind of verbose and even echo back commented lines too.
sh -v scriptname.sh
Note: A small difference between –x and –v is that –v echo’s the line as it is read (So it will even display comments too.), whereas –x flag causes each command to be echoed as it is executed.
sh -xv scriptname.sh
-u –At times you use a variable without setting some value to it. If you use this flag it will give you the error saying so and so variable is not set before executing the script.
-e –Exit the shell script if any error occurs. This option will stop the script to run further once the script encounters an error. Use full for debugging the first error itself when running big scripts…
sh -e scriptname.sh

Saturday, July 14, 2012

Bash syntax to calculate sum list of numeric values in a file

Lets say you have a file which consists list of Numeric values and wish to add the total sum of all the numeric values. The bash syntax would be:
sum=0; for i in `cat filename.txt`; do sum=$(( $sum + $i )); done; echo $sum
(or)
declare -i sum=0; for i in `cat filename.txt`; do sum+=$i; done; echo $sum

Popular Posts

About Me

My photo
The intent of this blog is to share my work experience and spread some smart solutions on Linux to System Administrators. I'm hoping the solutions shared in this Blog would be helpful and come as a handy for Viewers. Brief about me: I have 18+ years work experience in System and Cloud Administration domain, primarily works on VMware Cloud Products (vSphere, vCloud Director, vRealize Automation, NSX Adv. Load Balancer, vROps).