Wednesday, May 23, 2012

How to assign a password present in a file to a variable ?

Let's say you have a file 'secretfile' which contains the password 'newpass'. You want to assign the password mentioned in that file to a variable called 'pass1'.  The syntax will go like this:

# cat  secretfile
newpass

Method 1
# pass1=$(cat secretfile)
# echo $pass1
newpass

Method 2
# read pass1 < secretfile
# echo $pass1
newpass

Sunday, May 20, 2012

Setting alias for 'ls -l' command

I wish to enhance the default alias ('ll') for 'ls -l' command into more pleasing one:

Following is the alias options which I set for 'll' :   # alias ll='ls -lhGpt --color=always'
'-h' - Human readable
'G' - No Group information
'p' - Append / notation for folders
't' - Sort by modification time.

SAMPLE OUTPUT:

[ashok@hostxyz etc]$ ll
total 3.5M
-rw-r--r--  1 root 204K May 20 04:05 prelink.cache
-rw-r--r--  1 root  14K May 16 20:36 group
-r--------  1 root 7.6K May 16 20:36 gshadow
-rw-r--r--  1 root  43K May 16 20:36 passwd
-r--------  1 root  27K May 16 20:36 shadow
-rw-r--r--  1 root  14K May 16 20:33 group-
-r--------  1 root 7.6K May 16 20:33 gshadow-
-rw-r--r--  1 root  43K May 16 20:33 passwd-
-r--------  1 root  27K May 16 20:33 shadow-
drwxr-xr-x  2 root 4.0K May  9 05:16 ssh/
drwxr-xr-x  2 root 4.0K May  8 19:00 blkid/
drwxr-xr-x 10 root 4.0K May  8 06:11 sysconfig/
-rw-r--r--  1 root  128 May  7 03:55 resolv.conf
.
.
<Output truncated>

Syntax to check NIC statistics using 'sar' command

This syntax checks the statistics of 'eth1' interface:
[root@hostxyz ~]# sar -n DEV -f /var/log/sa/sa15 -s 18:00:01 -e 23:01:01 | head -3 | tail -1 | sed 's/^.........../           /'; sar -n DEV -f /var/log/sa/sa15 -s 18:00:01 -e 23:01:01 | grep "eth1"
                      IFACE   rxpck/s   txpck/s   rxbyt/s   txbyt/s   rxcmp/s   txcmp/s  rxmcst/s
06:10:01 PM      eth1     20.54      0.02   2056.62      3.12      0.00      0.00      0.00
06:20:01 PM      eth1     26.11      0.01   2650.42      1.98      0.00      0.00      0.00
06:30:01 PM      eth1     17.03      0.01   1711.49      1.98      0.00      0.00      0.00
06:40:01 PM      eth1     10.40      0.01    972.02      1.60      0.00      0.00      0.00
06:50:01 PM      eth1     24.11      0.02   2426.33      2.74      0.00      0.00      0.00
07:00:01 PM      eth1     25.27      0.01   2554.85      1.87      0.00      0.00      0.00
07:10:01 PM      eth1     13.36      0.01   1312.31      1.98      0.00      0.00      0.00
07:20:01 PM      eth1     12.21      0.02   1175.69      3.12      0.00      0.00      0.00
07:30:01 PM      eth1     25.09      0.01   2566.25      1.98      0.00      0.00      0.00
07:40:01 PM      eth1     26.01      0.01   2612.24      1.60      0.00      0.00      0.00
07:50:01 PM      eth1    206.35    635.89  13526.21 947854.38      0.00      0.00      0.00
08:00:01 PM      eth1   1326.80   4410.98  80781.99 6673444.08      0.00      0.00      0.00
08:10:01 PM      eth1    796.86   2608.67  49050.73 3947034.38      0.00      0.00      0.00
08:20:01 PM      eth1    773.95   2411.76  47858.10 3649035.90      0.00      0.00      0.00
08:30:01 PM      eth1    798.65   2814.04  48448.94 4257857.47      0.00      0.00      0.00
08:40:01 PM      eth1 599122.53 1729538.33 3278958.93 585797.40      0.00      0.00      0.00
08:50:01 PM      eth1   1126.42   3821.02  68802.91 5781612.42      0.00      0.00      0.00
09:00:01 PM      eth1   1416.86   4984.36  85843.95 377147.69      0.00      0.00      0.00
09:10:01 PM      eth1    803.29   2777.80  48717.98 4202947.25      0.00      0.00      0.00
09:20:01 PM      eth1    129.55    352.83   8700.49 533737.74      0.00      0.00      0.00
09:30:01 PM      eth1     25.34      0.01   2581.83      1.98      0.00      0.00      0.00
09:40:01 PM      eth1     15.67      0.01   1523.95      1.79      0.00      0.00      0.00
09:50:01 PM      eth1     11.38      0.01   1066.06      1.60      0.00      0.00      0.00
10:00:01 PM      eth1     23.75      0.03   2366.47      3.01      0.00      0.00      0.00
10:10:01 PM      eth1     26.29      0.01   2679.49      1.98      0.00      0.00      0.00
10:20:01 PM      eth1     12.79      0.01   1241.40      1.98      0.00      0.00      0.00
10:30:01 PM      eth1     13.03      0.03   1273.22      3.12      0.00      0.00      0.00
10:40:01 PM      eth1     25.71      0.01   2586.43      1.60      0.00      0.00      0.00
10:50:01 PM      eth1     24.44      0.01   2448.89      1.60      0.00      0.00      0.00
11:00:01 PM      eth1     10.38      0.01    958.51      1.87      0.00      0.00      0.00
Average:         eth1    288.58    929.73  18156.03 212554.92      0.00      0.00      0.00
[root@hostxyz ~]#

Monday, May 14, 2012

Perl subroutine to check Database Status

sub CheckDBStatus($)
{
my $host = $_[0];
my $dbStatus=`ssh $host -C "ps -ef | grep ora_pmon | grep -v grep -c"`;
if ($dbStatus == 1)
{
Log("Database is up on $host",0,LOGFILE);
return("0");
}
else
{
Log("Database is down on $host",0,LOGFILE);
return("1");
}
}

Note: You can use appropriate string to capture Oracle process with grep command.

Perl subroutine to un-mount a file-system


sub UnMountVolume($)
{
  my $mountPoint = $_[0];
  print "Un-mounting $mountPoint\n";
  # Checking if the mount point exists
  if ( grep m{$mountPoint}, qx{/bin/mount} )
  {
    #Mount point exists, attempt to unmount it without force option
    system("/bin/umount $mountPoint");
  }
  else
  {
    print "$mountPoint is not mounted, please check it out\n";
  }
}

Zip files using 'find' command

Let's say we want to Zip files which are more than a week time under a folder /var/log/nmon :

# for i in `find /var/log/nmon -mtime +7 -type f -print`; do gzip $i; done

Friday, May 11, 2012

Command-line syntax to reset the password

Let’s say we have to reset the Root password of a Linux machine to “XYZabc123”.  We can use the below command set to do it. This can be executed on remote machines if we have a centralized administration server.

# echo ‘XYZabc123’ > /opt/pass; passwd --stdin root < /opt/pass; rm -f  /opt/pass

Thursday, May 10, 2012

Perl script to list all Failed Login attempts in Linux

#!/usr/bin/perl -w
# Description             :  Upon execution, this script will list all the Failed Login attempts on a Linux Server
# Mode of Execution  :  As 'root' user from shell prompt
# Developed by          :  Ashok Raj
# Version                   :  1.2
# -------------------------------------------------------------------------------------------------------------------------

$osnam=`uname -s`;
chomp($osnam);
$cnt=0;

if($osnam eq 'Linux')
{
chdir "/var/log" or die "$!";
@arylog = `cat ./secure*|grep 'Failed password'`;
print "\n";
print "REPORT ON FAILED ATTEMPT LOGINS\n\n";
print " DATE/TIME       USERNAME\tHOSTNAME/IPADDRESS\n";
print "---------------------------------------------\n";
foreach(@arylog)
 {
 if($_ =~ m/(\w+\s+\d+\s\d\d:\d\d:\d\d)\s\S+\ssshd\[\d+\]:\s+Failed\s.*?\sfor.*?\s(\w+)\sfrom\s+(\S+)/)
            {
              printf "%-16s   %-16s%-16s\n",$1,$2,$3;
              $cnt++;
            }
}
}
print "\nTotal Number of Failed Attempts: $cnt \n\n";

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).