Linux Shell script to Add user with password to the system

Linux Shell script to Add user with password to the system

We often need to create unix users and need to assign password.This is quite Simple but if we have to create multiple users and need to assign the password to them,then its quite difficult. Below script will help in the desired task.

Hence first create a file user password which consists of user in the first column and password in the second column.

root@s:~# cat userpasswd
user1:password1
user2:password2
user3:password3
user4:password4
user5:password5

Run the below script to add users.

root@pri:~# for i in `cat user`; do echo $i; useradd $i ;done
user1
user2
user3
user4
user5
root@pri:~# cat userpasswd | chpasswd

Removing pkg on multiple Solaris zones

Script to remove the pkg on mutiple solaris zones

# for z in `zoneadm list |grep -v global`; do
echo $z
zlogin $z pkgrm uptimeagent >> /tmp/unixplanetary.txt
done

Script to list the pkg on mutiple solaris zones .

#for z in `zoneadm list |grep -v global`; do
echo $z
zlogin $z pkginfo -l uptimeagent >> /tmp/unixplanetary.txt
done

Remove and list files Older than X no of days

1. How to remove old file older than x no of days and which is not .gz file

create a old dated file using below commands.

touch -d 20150101 syslog.1

[root@unixplanetary tmp]# ls -ltr

-rw-r–r–. 1 root root 0 Jan  1  2015 syslog.1.gz

-rw-r–r–. 1 root root 0 Jan  1  2015 syslog.1

[root@unixplanetary tmp]= find /tmp -type f -mtime +20 |grep -v *.gz | xargs rm -rf;

[root@unixplanetary tmp]# ls -ltr

-rw-r–r–. 1 root root 0 Jan  1  2012 syslog.1.gz

[root@unixplanetary tmp]#

2. Remove more than 30 Days file.

find /tmp -mtime +30 -exec rm -rf {} \;

find /tmp -xdev -type f -mtime +30 -exec rm -rf {} \;

3. How to List the files older than 100 days

Unixplanetary:# find /var -mtime +100 |wc -l
56587

4. How to display the files older than 100 days .

#find /tmp -mtime +100 -print

5. List all file ending with .bz2 .

find / -type f -name “*.bz2”

6.  limit the find to the first level you can do:

# find / -maxdepth 1 -name ‘*.bz2’
/file1.bz2

7. List file Larger than 1 GB

find /tmp -xdev -size +1000000000c -exec ls -ltrh {} \;

8.Remove file larger than 1 GB from temp file system
find /tmp -xdev -size +1000000000c -exec rm -rf {} \;

Bash script to modify sudoers file in remote hosts

Bash script to modify the sudoers file in remote hosts , in this case we have only one  server which is unixplanetary.mylabserver.com  and we are going to add  the line  “test        ALL=(ALL)       NOPASSWD: ALL ” to /etc/sudoers file .

#!/bin/bash

for i in  unixplanetary.mylabserver.com

do  echo ssh $i ” test      ALL=(ALL)       NOPASSWD: ALL ” >> /etc/sudoers

done

Script to unlock user

Use below bash script to unlock all or any user on system So please check user lock info .

1.root@node1:~# passwd -S user1
user1 L 10/09/2017 0 99999 7 -1                (L in ubuntu os)
root@node1:~#

2.[root@node2]# passwd -S user1
user1 LK 2017-12-28 0 99999 7 -1              (LK in centos os)
[root@node2]#

So before starting make sure that weather user has L or LK ( user lock info given in line 1 and 2 ).
Hence put same in  script as well .

#! /bin/bash

for username in `cat /etc/passwd |awk -F: ‘{print $1}’`
do
user=`passwd -S $username | awk ‘{print $2}’`
if [ “$user” = “LK” ] then
echo ” user locked”
passwd -u $username
else
echo “user is not locked”
fi
done

Variable assignment in shell scripting

Variable assignment in shell scripting

[test@lab ~]$ a=`df -kh /tmp |awk ‘{print $4}’`
[test@lab ~]$ echo $a
Avail 14G
[test@bab ~]$

 

test@lab ~]$ a=`df -kh /tmp |awk ‘{print $4}’`
[test@lab ~]$ echo ” The free space in temp is $a”
The free space in temp is Avail
14G
[test@lab ~]$

Script to check if server is up or down

[root@labserver]# cat  ping.sh

a=`ping -c 1 192.168.1.35|head -n 2|tail -n 1|awk {‘print $1}’`
if [ “$a” -eq “64” ] then
echo $a
echo “server is up”
else
echo “server is down”
fi
[root@labserver]#

Script to check rootdisk under SVM/VXVM/ZFS !

Script to check rootdisk under SVM/VXVM/ZFS !

root@Labserver# more Script.sh
#!/bin/bash
## to check rootdisk under SVM/VXVM/ZFS
vol=`df -k | head -2| tail -1 | awk '{print $1 }' | cut -c6-10`
if [ $vol = "/ROOT" ]
then
# ZFS
## to check the disk
echo "Server is in ZFS"
zfsdisk=`zpool status rpool | grep s0 | head -1 | awk '{ print $1}' | cut -c1-21`
echo "rootdisk is $zfsdisk"
hard=`iostat -en | grep -i $zfsdisk | awk '{print $2}'`
echo "hardware error : $hard"
elif [ $vol = "md/ds" ]
then
## SVM
echo "Server is in SVM"
svmdisk=`metadb -i | grep -i m | grep -i dsk | awk '{print $7}' | cut -c10- | cut -c1-6`
echo "rootdisk is $svmdisk"
hard=`iostat -en | grep -i $svmdisk | awk '{print $2}'`
echo "hardware error : $hard"
elif [ $vol = "vx/ds" ]
then
## VXVM
echo "Server is in VXVM"
dgname=`vxdg bootdg`
vxvmdisk=`vxdisk -e list | grep -i $dgname | grep -i rootdisk | awk '{print $6}' | cut -c1-6`
echo "rootdisk is $vxvmdisk"
hard=`iostat -en | grep -i $vxvmdisk | awk '{print $2}'`
echo "hardware error : $hard"
else
## Single disk
echo "Server is up on single disk"
disk=`df -k | head -2| tail -1 | awk '{print $1 }' | cut -c10- | cut -c1-6`
echo "rootdisk is $disk"
hard=`iostat -en | grep -i $disk | awk '{print $2}'`
echo "hardware error : $hard"
fi
root@Labserver#

Script to login to remote server via script

Write a script to login to remote server and store the password .

An Expect script is a interaction script written for two Channels: a sender and a receiver. One of the more popular activities to automate is an SSH session between two hosts, in which one host is the sender (local host) and the other is the receiver (remote host).

Step1. sudo yum install expect

localsystem#
 !/usr/bin/expect  
 spawn ssh user1@remotesystem.com
 expect "password"
 send "redhat\r"
 expect "$"

 !/usr/bin/expect --->
To make an Expect script executable as a standalone program, you must do two things: Make the script executable, and supply the path to the script for expect . The path on my system is: /usr/bin/expect
spawn ssh user1@remotesystem.com  -----> spawn runs an external command from the expect script,```` In this case, ssh to a remote host (remotesystem ):
expect "password"  -----------> When you SSH to a remote system, you’re prompted for a password. This password prompt is what you “expect” from the remote system; therefore, you enter that expected response
 send "redhat\r"  ------------->From the local side, you have to enter your password at the password prompt. To send anything to the remote system it must include a hard return (\r ). Change PASSWORD to your password:
expect "$" ------------->you have to enter the expected response from the remote system, which in this case is a user prompt ($ ).