Linux rebuild software RAID1

Set the bash field separator to newline:

IFS="
"

See what disk and partitions are currently up, then generate commands to re-add the missing disk and partitions, then run them:

for i in `cat /proc/mdstat | grep md | cut -d [ -f1 | sed -e 's/\(md[0-9]\).*\(sd[a-z][0-9]\)/mdadm --add \/dev\/\1 \/dev\/\2/' | sed -e 's/sdb/sda/'`;  do eval $i; done;

TODO: make it determine which disk to add (/dev/sda or /dev/sdb)

change SSH listen address

If you have servers with internal and external interfaces, you may want to disable ssh on the external side. In this case we just get the internal IP address and tell sshd to only listen on that address:

sed -i "s/#ListenAddress 0.0.0.0/ListenAddress `grep address /etc/network/interfaces | grep 10.229 | awk '{print $2}'`/" /etc/ssh/sshd_config

Do it to many hosts:

for i in `seq 313 364`; do ssh ftp$i "sed -i \"s/#ListenAddress 0.0.0.0/ListenAddress \`grep address /etc/network/interfaces | grep 10.229 | awk '{print \$2}'\`/\" /etc/ssh/sshd_config"

And restart SSH:

for i in `seq 313 364`; do ssh ftp$i "service ssh restart"; done;

remove IPv6 address from interface

If you don’t want to have an IPv6 address on an interface, you can quickly remove it:

# ip addr show dev eth0 
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:c2:fc:59 brd ff:ff:ff:ff:ff:ff
    inet 10.210.0.141/16 brd 10.210.255.255 scope global eth0
    inet6 fe80::20c:29ff:fec2:fc59/64 scope link 
       valid_lft forever preferred_lft forever

Only get the address:

# ip addr show dev eth0 | grep inet6 | awk '{print $2}'
fe80::20c:29ff:fec2:fc59/64

Remove it:

# ip -6 addr del `ip addr show dev eth0 | grep inet6 | awk '{print $2}'` dev eth0

Or do it to many hosts:

for i in `seq 201 272`; do ssh www$i "ip -6 addr del \`ip addr show dev eth0 | grep inet6 | awk '{print \$2}'\` dev eth0"; done;

drop messages in mailqueue from single sender

Drop all messages from the sender ‘nagios’:

# for i in `mailq | tail -n +2 | awk  'BEGIN { RS = "" } { if ($7== "nagios") print $1}'`; do postsuper -d $i; done;
postsuper: B60BF9FB69: removed
postsuper: Deleted: 1 message
postsuper: C3B429FB6F: removed
postsuper: Deleted: 1 message
postsuper: 0306C9FB87: removed
postsuper: Deleted: 1 message
postsuper: E3BC79FB7E: removed
postsuper: Deleted: 1 message
postsuper: B32EA9FB65: removed
(many more lines)

Gets the mailqueue, starts the output on line 2, skipping the first header line, if the sender equals ‘nagios’ then print the first field, which is the message id. Then use postsuper to drop the message identified by it’s id.

get vmware esxi cpu info


~ # vmwarevim-cmd hostsvc/hosthardware | grep -A10 cpuPkg | grep description
   cpuPkg = (vim.host.CpuPackage) [
      (vim.host.CpuPackage) {
         dynamicType = , 
         index = 0, 
         vendor = "intel", 
         hz = 2833433579, 
         busHz = 333345127, 
         description = "Intel(R) Xeon(R) CPU           E5440  @ 2.83GHz", 
         threadId = (short) [
            0, 
            1, 

Here I get the info for 8 different machines to see if they all match:

for i in `seq 1 1 8`; do ssh vm30$i "vim-cmd hostsvc/hosthardware | grep -A10 cpuPkg | grep description"; done;

This works on 4.1 and 5.1 versions.

make many directories with Bash sequences

Sometimes it’s necessary to do things with sequences in Bash. If you want to create a bunch of directories that will be mount points for NFS servers you could do this:


# mkdir /mnt/fs42
# mkdir /mnt/fs42/vol0
# mkdir /mnt/fs42/vol1
# mkdir /mnt/fs42/vol2
# mkdir /mnt/fs42/vol3

But, even the best experts at the “up arrow key” wouldn’t want to do this for 50 file servers. A nested for loop would work, but it’s not necessarily the easiest way to go. The command seq makes a sequence of integers bounded by the numbers specified.

ok:

for i in `seq 1 50`; do for j in `seq 0 3`; do mkdir -p /mnt/fs$i/vol$j; done; done;

Using Bash sequences, you can tell the shell to interpret this {1..50} as a list of integers between 1 and 50. This also works with letters like {a..t}.

# echo {a..t}
a b c d e f g h i j k l m n o p q r s t
# echo {0..50}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

way better:


# mkdir -p /mnt/fs{1..50}/vol{0..3}

of course if you have non-sequential lists, you may have to specify each element like this:

{1,3,4,5,6,9}