Monday 4 November 2019

Log Rotation

Delete files older then n days
n=1  ---- Number of days
filePattern="abc.log.day*"   --- Pattern of file
find . -mtime +$n -name "$filePattern" -exec rm {} \;  --- Command delete files older then n days


Move files older then n days to directory
dir=




Zip files older then 1 day
find . -mtime +1 -name "fileName*" -exec gzip {} \;

Check Statistics of a file

stat fileName

Monday 30 September 2019

if else

#!/bin/bash

a=0
b=7
if ! [ $a -ne 0 ] && ! [ $b -ne 7 ];
then
echo "success"
fi

$success

Friday 27 September 2019

How to make pass-wordless connectivity between 2 hosts

Create keys
Run below command if .ssh folder is not present at home
ssh-keygen -t rsa

Friday 6 September 2019

Check for SSH connectivity to multiple hosts

Checking SSH connectivity from one server to multiple hosts
Condition: All the hosts/IP's should have same username and password

Step 1: Create a file with name "ipFile" and insert all the hostnames/IP's line by line
Step 2: Create a file checkSSH.sh with below code
#/bin/bash
export user="username"

export pass="password"
export SSHPASS=$pass
>output
for i in `cat ipFile`
do
sshpass -e ssh $user@$i -q "echo $i is Accessible" >>output
done
Step 3: Edit "username" and "password" with your target IP's user and password
Step 4: Run above file using command : sh checkSSH.sh
Step 5 : A new file with name "output" will be created with the details


Below steps are useful if you don't have expect and sshpass utility
Condition : All IP's have same username

Step 1: Create a file with name "ipFile" and insert all the IP's line by line
Step 2: Create a file checkSSH.sh with below code
#/bin/bash
user="username"
>output
for i in `cat ipFile`
do
ssh -o "StrictHostKeyChecking no" $user@$i -q "echo $i is Accessible" >>output
done
Step 3: Edit user="username" with your linux user
Step 4: Run above file using command : sh checkSSH.sh
Step 5: Give password one by one
Step 6 : A new file with name "output" will be created with the details