UnixTips.net

* use my tips at your own risk !!

* use my tips at your own risk !!

Categories

  • Other (3)
  • Scripting (10)
  • Unix (70)
  • Wordpress (2)

Powered by Genesis

Rename Logical Volumes Redhat 6 7

I had to rename a bunch of logical volumes and filesystems.

This script is based on the lvrename command.

It will go line by line of the fs file formated

cat fs
oldfs01a:newfs01b
oldfs02a:newfs02b
oldfs03a:newfs03b

In my case the lvname and fs were were similar except for the last three characters.

It will umount the fs , rename the lv , rename the mount, rename the /etc/fstab and mount the directory.

Use with caution. Use my tips at your own risk !!

#!/usr/bin/sh

DATE=`date '+%Y.%m.%d_%H.%M.%S'`

cp /etc/hosts /etc/hosts.${DATE}

cat fs | while read line
do

oldfs=`echo ${line} | awk -F: '{print $1}'`
newfs=`echo ${line} | awk -F: '{print $2}'`

mapperinfo=`df -h /var/cache/${oldfs} | grep mapper`

oldlv=`lvs ${mapperinfo} | grep -v LV | awk '{print $1}'`
vgname=`lvs ${mapperinfo} | grep -v LV | awk '{print $2}'`

newlv=`echo ${oldlv} | sed -e "s/${oldfs}/${newfs}/g"`

echo "rename ${oldfs} -> ${newfs}"
sleep 7
#######
df -h /var/cache/${oldfs}

umount /var/cache/${oldfs}

lvrename ${vgname} ${oldlv} ${newlv}

sed -ie "s/${oldfs}/${newfs}/g" /etc/fstab

mv /var/cache/${oldfs} /var/cache/${newfs}

mount /var/cache/${newfs}

df -h /var/cache/${newfs}
echo ""
echo ""
done

repeat scp x number of times

Create an empty 500m file ( linux )

# truncate –s 500m 500mb.file

Copy a file 10 times in a row via a loop.

for X in {1..10}
do
scp -p -r 500m.file myhost:/tmp
done

ssh script hanging

I had an issue where an ssh loop script was exiting after 1 host.

The fix use the -n flag.

Ensure ssh isn’t reading from standard input by using the -n option:

ZFSHEALTH=`/usr/local/bin/ssh -n $MACHINE "/sbin/zpool get health rpool" |
grep ONLINE | wc -l`

The nc command

To test a connection use nc

# nc -vz myhost 22 -w 15 > /dev/null 2>&1

-w 15 is the timeout

Will return 0 if the host connects on ssh port 22
or 1 if it does not connect.

Updated: 03/29/2016

The new update to the nc command takes this option away.

Instead the following will work

timeout 60s /bin/bash -c 2>/dev/null >/dev/tcp/myhost/22

Test port connection
*from the man pages

On one console, start nc listening on a specific port for a connection.

# nc -l 1234

nc is now listening on port 1234 for a connection. On a second console (or a second machine), connect to the machine and port being listened on:

# nc 127.0.0.1 1234

There should now be a connection between the ports. Anything typed at the second console will be concatenated to the first, and vice-versa. The connection may be terminated using an EOF.

Add basic options to script

#!/bin/bash

if [ $# -ne 1 ]
then
echo "Usage: $0 "
exit 0
fi

host=$1

If no hostname is specified output will be:

./1.myscript.bash 
Usage: ./1.myscript.bash  
  • 1
  • 2
  • Next Page »