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

Friday, June 22, 2012

Useful Run Commands For Windows 7

List of commands that you can run off from the Run Command prompt in Windows 7:

To get the command prompt, press Windows logo key + R 

Administrative Tools
Administrative Tools = control admintools
Authorization Manager = azman.msc
Component Services = dcomcnfg
Certificate Manager = certmgr.msc
Direct X Troubleshooter = dxdiag 
Display Languages = lpksetup
ODBC Data Source Administrator = odbcad32
File Signature Verification Tool = sigverif
Group Policy Editor = gpedit.msc
Add Hardware Wizard = hdwwiz.cpl
iSCSI Initiator = iscsicpl
Iexpress Wizard = iexpress
Local Security Settings = secpol.msc
Microsoft Support Diagnostic Tool = msdt
Microsoft Management Console = mmc
Print management = printmanagement.msc
Printer User Interface = printui
Problems Steps Recorder = psr
People Near Me = p2phost 
Registry Editor = regedit or regedt32
Resoure Monitor = resmon
System Configuration Utility = msconfig
Resultant Set of Policy = rsop.msc
SQL Server Client Configuration = cliconfg
Task Manager = taskmgr
Trusted Platform Module = tpm.msc
TPM Security Hardware = TpmInit 
Windows Remote Assistance = msra
Windows Share Folder Creation Wizard = shrpubw
Windows Standalong Update Manager = wusa
Windows System Security Tool = syskey
Windows Script Host Settings = wscript
Windows Version = winver
Windows Firewall with Advanced Security = wf.msc
Windows Memory Diagnostic = MdSched
Windows Malicious Removal Tool = mrt 

Computer Management
Computer Management = compmgmt.msc or CompMgmtLauncher
Task Scheduler = control schedtasks
Event Viewer = eventvwr.msc
Shared Folders/MMC = fsmgmt.msc
Local Users and Groups = lusrmgr.msc
Performance Monitor = perfmon.msc
Device Manager = devmgmt.msc
Disk Management = diskmgmt.msc
Services = services.msc
Windows Management Infrastructure = wmimgmt.msc

Conrtol Panel
Control Panel = control
Action Center= wscui.cpl 
Autoplay = control.exe /name Microsoft.autoplay
Backup and Restore = sdclt
Create a System Repair disc = recdisc
BDE Administrator = bdeadmin.cpl
Color Management = colorcpl 
Credential Manager = control.exe /name Microsoft.CredentialManager
Credential Manager Stored User Names and Passwords = credwiz
Date and Time Properties = timedate.cpl
Default Programs = control.exe /name Microsoft.DefaultPrograms
Set Program Access and Computer Defaults = control appwiz.cpl,,3 or ComputerDefaults
Devices and Printers = control printers
Devices and Printers Add a Device = DevicePairingWizard
Display = dpiscaling 
Screen Resolution = desk.cpl
Display Color Calibration = dccw 
Cleartype Text Tuner = cttune 
Folders Options = control folders
Fonts = control fonts
Getting Started = GettingStarted
HomeGroup = control.exe /name Microsoft.HomeGroup
Indexing Options = control.exe /name Microsoft.IndexingOptions
Internet Properties = inetcpl.cpl
Keyboard = control keyboard
Location and Other Sensors = control.exe /name Microsoft.LocationandOtherSensors 
Location Notifications = LocationNotifications
Mouse = control mouse or main.cpl
Network and Sharing Center = control.exe /name Microsoft.NetworkandSharingCenter
Network Connections = control netconnections or ncpa.cpl
Notification Area Icons = control.exe /name Microsoft.NotificationAreaIcons
Parental Controls = control.exe /name Microsoft.ParentalControls
Performance Information = control.exe /name Microsoft.PerformanceInformationandTools
Personalization = control desktop
Windows Color and Appearance = control color
Phone and Modem Options = telephon.cpl
Power Configuration = powercfg.cpl
Programs and Features = appwiz.cpl or control appwiz.cpl
Optional Features Manager = optionalfeatures or control appwiz.cpl,,2
Recovery = control.exe /name Microsoft.Recovery
Regional and Language = intl.cpl
RemoteApp = control.exe /name Microsoft.RemoteAppandDesktopConnections
Sound = mmsys.cpl
Volume Mixer = sndvol
System Properties = sysdm.cpl or Windows logo key + Pause/Break
SP ComputerName Tab = SystemPropertiesComputerName
SP Hardware Tab = SystemPropertiesHardware
SP Advanced Tab = SystemPropertiesAdvanced
SP Performance = SystemPropertiesPerformance
SP Data Execution Prevention = SystemPropertiesDataExecutionPrevention
SP Protection Tab = SystemPropertiesProtection
SP Remote Tab = SystemPropertiesRemote
Windows Activation = slui
Windows Activation Phone Numbers = slui 4
Taskbar and Start Menu = control.exe /name Microsoft.TaskbarandStartMenu
Troubleshooting = control.exe /name Microsoft.Troubleshooting
User Accounts = control.exe /name Microsoft.UserAccounts
User Account Control Settings = UserAccountControlSettings
User Accounts Windows 2000/domain version = netplwiz or control userpasswords2
Encryption File System = rekeywiz
Windows Anytime Upgrade = WindowsAnytimeUpgradeui
Windows Anytime Upgrade Results = WindowsAnytimeUpgradeResults
Windows CardSpace = control.exe /name Microsoft.cardspace
Windows Firewall = firewall.cpl
WindowsSideshow = control.exe /name Microsoft.WindowsSideshow
Windows Update App Manager = wuapp

Accessories
Calculator = calc
Command Prompt = cmd
Connect to a Network Projector = NetProj
Presentation Settings = PresentationSettings
Connect to a Projector = displayswitch or Windows logo key + P
Notepad = notepad
Microsoft Paint = mspaint.exe
Remote Desktop Connection = mstsc
Run = Windows logo key + R
Snipping Tool = snippingtool 
Sound Recorder = soundrecorder 
Sticky Note = StikyNot 
Sync Center = mobsync
Windows Mobility Center (Only on Laptops) = mblctr or Windows logo key + X
Windows Explorer = explorer or Windows logo key + E 
Wordpad = write
Ease of Access Center = utilman or Windows logo key + U
Magnifier = magnify
Narrator = Narrator
On Screen Keyboard = osk
Private Character Editor = eudcedit
Character Map = charmap
Ditilizer Calibration Tool = tabcal
Disk Cleanup Utility = cleanmgr
Defragment User Interface = dfrgui
Internet Explorer = iexplore
Rating System = ticrf
Internet Explorer (No Add-ons) = iexplore -extoff
Internet Explorer (No Home) = iexplore about:blank
Phone Dialer = dialer
Printer Migration = PrintBrmUi
System Information = msinfo32
System Restore = rstrui
Windows Easy Transfer = migwiz
Windows Media Player = wmplayer
Windows Media Player DVD Player = dvdplay
Windows Fax and Scan Cover Page Editor = fxscover
Windows Fax and Scan = wfs
Windows Image Acquisition = wiaacmgr
Windows PowerShell ISE = powershell_ise
Windows PowerShell = powershell
XPS Viewer = xpsrchvw

Open Documents folder = documents
Open Pictures folder = pictures
Open Music folder = music
Open Favorites folder = favorites
Open Downloads folder = downloads
Logs out of Windows = logoff
Shuts Down Windows = shutdown

Popular Posts

About Me

My Photo
I have started this blog to share my work experience and spread some smart solutions on Linux to Internet community. I'm hoping more people will get benefited from this blog.