Thursday, February 11, 2016

Linux - Schedule one time job using at

Schedule a job to execute at a specific time
$ at time date
$ at 12 pm july 4

2. Schedule an at job using a time + specified time
$ at now + COUNT Unit
$ at now + 15 min
$ at now + 1 hour
$ at now + 1 day
$ at now + 1 week
$ at now + 5 years
$ at midnight


3. List scheduled jobs
$ atq or at -l -->> notice the job number on first field

4. Check the content of a scheduled job
$ at -c <job id#>

6. Remove schedule jobs
$ atrm <job#>

7. Send an email once the job is complated
$ at 12:49 -m

Run a script at 12.30
$ at 12:32 -m -f /data/onetime.jobs

Tuesday, February 9, 2016

Script - check pool size

Checking size of zfs pool. Please note, if the size is over TB, this script does not work.

#!/bin/bash
# Tue Aug 4 09:14:09 EDT 2015
# File
# Copy authorized key to remove host
# Sam Bhusalsystem check on remote server
# OS: Solaris 10
#

LOGFILE="logs/POOL_SIZE_`date "+%m%d%y_time.%H-%M-%S.log`"

# using for to loop through, you may find better option
for i in `cat ../etc/hosts.ip | grep -v "#" | awk '{print $1}'`
do
echo "Checking $i"
echo "Checking $i" >> $LOGFILE
#ssh -q $i /usr/sbin/zpool list | grep -vi size | awk '{print $2}' >>$LOGFILE
# ssh -q $i echo "Checking `hostname`:$i"; echo; zpool list | awk '{print $1 "\t" $2}' >>$LOGFILE
ssh -q $i /usr/sbin/zpool list | grep -vi size | awk '{print $2}'| sed -e 's/G//g' | paste -sd+ - | bc >> $LOGFILE
echo "" >> $LOGFILE
echo "-------------------------------------" >> $LOGFILE

done
more $LOGFILE
# EOF

--------------------------------
$ cd etc
$ cat hosts.ip

192.168.10.200 dnsserv1
192.168.10.130 appserv1
# 192.168.10.20 dnsserv1
192.168.10.210 dnsserv2
192.168.10.140 appserv2

Script - Checking filesystem info of remote server

Checking filesystem info of remote server

#!/bin/bash
# Tue Aug 4 09:14:09 EDT 2015
# File
# Copy authorized key to remove host
# Sam Bhusalsystem check on remote server
# OS: Solaris 10
#

LOGFILE="logs/DISK_SPACE_`date "+%m%d%y_time.%H-%M-%S.log`"

# using for to loop through, you may find better option
for i in `cat ../etc/hosts.ip | grep -v "#" | awk '{print $1}'`
do
echo "Checking $i"
echo "Checking $i" >> $LOGFILE
#ssh -q $ uname -a >> $LOGFILE
ssh -q $i w >> $LOGFILE
ssh -q $i df -h | egrep -v "objfs|sharefs|ctfs|proc|platform|fs|mnttab" >> $LOGFILE
echo "" >> $LOGFILE
echo "-------------------------------------" >> $LOGFILE

done
more $LOGFILE
# EOF

--------------------------------
$ cd etc
$ cat hosts.ip

192.168.10.200 dnsserv1
192.168.10.130 appserv1
# 192.168.10.20 dnsserv1
192.168.10.210 dnsserv2
192.168.10.140 appserv2


script - copy public key to remote server


#!/bin/bash
# Tue Aug 4 09:14:09 EDT 2015
# generate your key using ssh-keygen -t rsa -N "" -b 2048
# Copy authorized key to remove host
# Sam Bhusal
# OS: Solaris 10, should also work on Linux env
# copy_pkey.sh

# using for to loop through, you may find better option
for i in `cat ../etc/hosts.ip | grep -v "#" | awk '{print $1}'`
do
echo "Checking $i"
ssh-keyscan $i >> ~/.ssh/known_hosts
ssh -q $i mkdir .ssh
scp -qp authorized_keys $i:/export/home/sam/.ssh
#ssh -q $i /usr/local/bin/sudo cat /etc/shadow | egrep "$USERS"
# ssh -q $i cat /etc/passwd | egrep "$USERS"

done
# EOF

# Note:
# ssh-copy-id is not available on Solaris servers.
# ssh-copy-id -i ~/.ssh/id_rsa.pub remote-server
# ssh-copy-id appends the public key on to the remote-server’s .ssh/authorized_key under the users home dir
# Once you done, try logging from your machine toremove "ssh remote server.
# should not prompt you for password.

--------------------------------
$ cd etc
$ cat hosts.ip

192.168.10.200 dnsserv1
192.168.10.130 appserv1
# 192.168.10.20 dnsserv1
192.168.10.210 dnsserv2
192.168.10.140 appserv2


$ sh copy_pkey.sh

script - Query user on remote server.

Query user

1. Prepare your script
$ mkdir etc bin
$ cd bin
#!/bin/bash
# Tue Aug 4 09:14:09 EDT 2015
# Query Specified user
# Sam Bhusal
# usage sh -xv finduser.sh "john|mary|arya"
# How to use it?
# create two directories bin and etc on your home dir
# on your etc directory create a hosts.ip file with ip and host
# 192.168.10.20 dnsserv1
# 192.168.10.130 appserv1
# Create this file on bin dir
# to use this script
# As a command line parameter, specify the user with | for multiple user
# Usage: yourscript "user1|user2|user3"
#
USERS="$1"
[ $# -eq 0] && { echo ""; echo "Error:- Usage: $0 user_name"; echo ""; exit 1; }


# using for to loop through, you may find better option

for i in `cat ../etc/hosts.ip | grep -v "#" | awk '{print $1}'`
do
echo "Checking $i"
#ssh -q $i /usr/local/bin/sudo cat /etc/shadow | egrep "$USERS"
ssh -q $i cat /etc/passwd | egrep "$USERS"

done
# EOF

--------------------------------
2. Prepare your config file

$ cd etc
$ cat hosts.ip

192.168.10.200 dnsserv1
192.168.10.130 appserv1
# 192.168.10.20 dnsserv1
192.168.10.210 dnsserv2
192.168.10.140 appserv2

3. Now, run your script
$ sh finduser.sh john
or
$ sh finduser.sh "john|mary|sam"

Saturday, February 6, 2016

RHEL - Input/Output redirection


Input/Output redirection

1. Redirect output to a file
$ ls -l life is beautiful cool >/var/tmp/ls1.out
ls: cannot access cool: No such file or directory

2. Read the output content
$ cat /var/tmp/ls1.out
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 beautiful
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 is
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 life

3. Redirect the error message to a file
$ ls -l life is beautiful cool 2>/var/tmp/error.out
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 beautiful
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 is
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 life

4. Read the error message
$ cat /var/tmp/error.out
ls: cannot access cool: No such file or directory

5. Redirect output and error message to a same file
$ ls -l life is beautiful cool >/var/tmp/all1.out 2>&1

6. Read the output/error message
$ cat /var/tmp/all1.out
ls: cannot access cool: No such file or directory
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 beautiful
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 is
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 life

7. List user in a file users
$ cat users
ram
sam
sita
gita

8. Input redirect to the cat command
$ cat < users
ram
sam
sita
gita

9. Using pipe
$ cat /etc/passwd -b | grep ravi
    39  ravi:x:1000:1000:ravi:/home/ravi:/bin/bash

10. Here document
$ cat <<COOL
This is test
I am testing
and enjoying
COOL

11. Pipes takes output from one command and seens to second command as an input
$ command | command2
$ cat /etc/passwd | grep sam

Friday, February 5, 2016

Solaris10: patching

I just wrote and haven't tested it. When I have time, I will test when I get a chance. Please feel free to use it test it and see if it works. I manually installed, tested and verified the Solaris 10 1st quartet (Q1) patch successfully. Based on that procedure, I prepare this note.

# cat patch_automate.sh
#!/usr/bin/bash
# Kamal Bhusal
# Last updated - Wed Jan 27 10:28:18 EST 2016
# Automate patching procedure for Solaris 10 zfs root
#

# Patch location
mkdir /var/tmp/patchlog
LOG_DIR="/var/tmp/patchlog"
cp -p /etc/hosts /etc/hosts.01252016
cp -p /etc/nsswitch.conf /etc/nsswitch.conf.01252016
cp -p /etc/mail/sendmail.cf /etc/mail/sendmail.cf.01252016
df -h >>$LOG_DIR/df.out
zfs list >>$LOG_DIR/myzfs.out
zpool list>> $LOG_DIR/zpool.list
zpool status >>$LOG_DIR/zpool.status
cat /etc/lu/ICF.1>>$LOG_DIR/ICF1.bk
cat /etc/lu/ICF.2>>$LOG_DIR/ICF2.bk

cd /var/adm; rm pacct.* auditlog.0 auditlog.1
cd /var/tmp; rm -fr DCE* SPX* TCP* US* RAW* NMP* net* ISPX* VI* BEQ* DEC* ITCP*
cd /var/audit; /usr/local/bin/purge_audit.sh; cd /

# Current Book Environment
C_BE=`df -h / | grep -v Filesystem | awk '{print $1}' | awk -F/ '{print $3}'`
# OLD_Boot_Env
O_BE=`/usr/sbin/lustatus | tail -2 | grep -v "$C_BE" | awk '{print $1}'`
# New Book env
N_BE="aBE01252016"

/usr/sbin/lustatus >> $LOG_DIR/lustatus.log
/usr/sbin/ludelete $O_BE
echo "" >> $LOG_DIR/lustatus.log
echo "---- After deleting old BE ----" >> $LOG_DIR/lustatus.log
echo ""
/usr/sbin/lustatus >> $LOG_DIR/lustatus.log

/usr/sbin/lucreate -n $N_BE
echo "---- After Creating a new BE ----" >> $LOG_DIR/lustatus.log
echo "" >> $LOG_DIR/lustatus.log
/usr/sbin/lustatus >> $LOG_DIR/lustatus.log

# Upload your patch to /data or choice of your directory location
# Now, extract the patch, change your patch dir
mkdir /var/tmp/patch; cd /var/tmp/patch
/usr/bin/unzip -qq /data/Sol_Patch/10_Recommended_06102015.zip
cd 10_Recommended_CPU_2016-01

# Mount your new BE
# mkdir /alt
/usr/sbin/lumount $N_BE /alt

# Apply OS patches to /alt.  Apply Pre-Req first and Patch cluster
echo "Please pay attention here to see if you get any error"
# ./installcluster --apply-prereq --s10patchset >/alt/opt/Patches/10_recommended.pre.out 2>&1 &
# ./installcluster --apply-prereq --s10patchset >/alt/opt/Patches/10_recommended.pre.out
./installcluster --apply-prereq --s10patchset

# Now, apply the Kernel patch.
echo " Patching on progress ..."
nohup ./installcluster -R /alt --s10patchset --disable-space-check > /alt/opt/Patches/10_Recommended.out 2>&1 &

# note: do not use --disable-space-check option if you don't have enough space.
# Verify currently installed patch
showrev -p -R /alt | grep 150400-30 >> $LOG_DIR/new_Patch.log

# Update the boot-archive
bootadm update-archive -R /alt

/usr/sbin/luumount $N_BE

/usr/sbin/luactivate $N_BE

sync;sync;sync
# findally reboot the server
# init 6

# For Jan, 2016 patch, following patches were applied

# cat /alt/opt/Patches/10_Recommended.out | grep -i success | awk '{print "#" $i}'
#Applying 119059-71 ( 33 of 397) ... success
#Applying 119213-32 ( 39 of 397) ... success
#Applying 119783-33 ( 73 of 397) ... success
#Applying 122911-35 (124 of 397) ... success
#Applying 125719-56 (165 of 397) ... success
#Applying 143506-11 (244 of 397) ... success
#Applying 145080-15 (274 of 397) ... success
#Applying 146288-03 (285 of 397) ... success
#Applying 147692-95 (300 of 397) ... success
#Applying 147693-95 (301 of 397) ... success
#Applying 148031-05 (310 of 397) ... success
#Applying 148071-19 (312 of 397) ... success
#Applying 148104-21 (313 of 397) ... success
#Applying 148241-05 (317 of 397) ... success
#Applying 148342-09 (320 of 397) ... success
#Applying 148559-16 (326 of 397) ... success
#Applying 150400-30 (347 of 397) ... success
#Applying 151721-01 (389 of 397) ... success
#Applying 151912-02 (391 of 397) ... success
#Applying 152076-11 (392 of 397) ... success
#Applying 152077-11 (393 of 397) ... success
#Applying 152248-01 (395 of 397) ... success
#Applying 152260-01 (397 of 397) ... success
#