Windows 2012 server NIC teaming

NIC teaming, or channel bonding or Link Aggregation is a new feature in Windows 2012. Previously NIC teaming could only be accomplished in Windows by using a Vendor specific driver. With this new Windows feature, one can use up to 32 network cards of different vendors in “team”.

nicteaming overview

Some configuration options in NIC teaming explained:

Teaming mode

nicteaming_team_mode

The default option is switch independent. This is the only option that works with more than one switch. Switches are unaware of NIC teams.

Static teaming requires a smarter switch. Teaming ports are configured on the switch and teaming NIC’s are configured on the computer. Disadvantage of Static teaming is when a NIC or cable fails, it will still use that port/NIC.

LACP or Link Aggregation Control Protocol is the successor of static teaming. It uses small packets (LACPDU’s) between the switch and the computer to dynamically check if connections are available. When a port/NIC fails, LACP doesn’t use that port anymore in the NIC team.

Load Balancing Mode

Load Balancing Mode configures the way the traffic flows through the NIC teams. We don’t want one connection (between 1 host and 1 switch) to flow through different ports, because parts of a packet could arrive in the wrong order (and it would require to much time for a switch to reassemble those packets).

nicteaming load balancing mode
To decide which specific port and which specific NIC will be used for a connection, a hash is created, containing (sometimes parts of)  Source/Destination IP/MAC addresses and ports.  This hash is unique per connection.

When using virtual machines (Hyper-V), the MAC address of the Hyper-V-switch port is used as a unique identifier for the connection.

Standby adapter

nicteaming standby adapter

Default Windows will use all NICs from the team in an Active-Active manner.

An administrator can decide to provide a failover and configure a NIC as a Standby NIC. This NIC will only become Active when the other NIC in the team fails.

Remark: A common misunderstanding seems to be that, when doing Active-Active teaming with NICS of 1Gbps, one could double the speed to 2Gbps. This is NOT the case when only one host connects to the switch, since one connection always flows through the same switch port and the same physical NIC port. When two hosts make a connection, each host can get a maximum of 1Gbps.

An instructive video (in Dutch) Windows 2012 NIC teaming NL

More information on this topic in this doc at http://www.microsoft.com/en-us/download/details.aspx?id=30160

Posted in windows | Tagged , , , , , | Leave a comment

bash string manipulation

These quick string manipulations might save you some time. This works with bash starting from version 4.0

Function

Input String s

Bash

Output String

Show ABC123abc123 ${s} ABC123abc123
Show UPPERCASE ABC123abc123 ${s^^} ABC123ABC123
Show lowercase ABC123abc123 ${s,,} abc123abc123
Show length ABC123abc123 ${#s} 12
Show last 4 characters ABC123abc123 ${s: -4} c123
Show first 4 characters
ABC123abc123 ${s:0:4} ABC1
Show characters 4 to 10 ABC123abc123 ${s:3:6} 123abc
Remove first 7 characters
ABC123abc123 ${s:7} bc123
Remove last 4 characters
ABC123abc123 ${s::-4} ABC123ab
Remove first 2 and final 3 characters ABC123abc123 ${s:2:-3} C123abc
Remove substring A*12 in front ungreedy ABC123abc123 ${s#A*12} 3abc123
Remove substring A*12 in front greedy ABC123abc123 ${s##A*12} 3
Remove substring 123* at end ungreedy ABC123abc123 ${s%123*} ABC123abc
Remove substring 123* at the end greedy ABC123abc123 ${s%%123*} ABC
Remove 1 time substring 123 ABC123abc123 ${s/123} ABCabc123
Remove all substrings 123 ABC123abc123 ${s//123} ABCabc
Remove all letters ABC123abc123 ${s//[a-z]} 123123
Replace 1 time 123 by abc ABC123abc123 ${s/123/abc} ABCabcabc123
Replace everywhere 123 by abc ABC123abc123 ${s//123/abc} ABCabcabcabc
Replace beginning ABC by 123 ABC123abc123 ${s/#ABC/123} 123123abc123
Replace ending 123 by abc ABC123abc123 ${s/%123/abc} ABC123abcabc
Posted in bash, linux, macosx | Tagged , , , , | Leave a comment

Mass SMTP mailer from shell

I configured my local mail system to use the gmail SMTP server.
Install package ssmtp and configure /etc/ssmtp/ssmtp.conf with following parameters:

 root=postmaster
 AuthUser=kdguntu@gmail.com
 AuthPass=NoWAYiWILLtellYOUmypassWORD
 FromLineOverride=YES
 mailhub=smtp.gmail.com:587
 UseSTARTTLS=YES
 hostname=defaults.be

This scripts reads an input file (listusers.txt) with ; seperated values for example containing lines like this:

mathilde.dudekem@hotmail.com;Mathilde;team1
filip.frombelgium@yahoo.com;Filip;team2

Since it might be dangerous running this spam machine with any kind of inputfile, this script does not send mails, but creates a mailscript.

INPUT="listusers.txt"
M="mailscript.sh"
echo '#!/bin/bash' > $M
SUBJECT="Your teamserver is ready"
 
OLDIFS=$IFS
IFS=";"
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read EMAIL NAME TEAM
do
echo "mail -s \"$SUBJECT\" \"$EMAIL\" <<EOM ">>$M
echo 'Dear $NAME,'>>$M
 
echo 'Your server is available at:  '>>$M
echo "https://defaults.be/$TEAM ">>$M
echo ' ' >>$M
echo 'Kind Regards'>>$M
echo ' ' >>$M
echo 'Kdguntu'>>$M
echo 'EOM'>>$M
echo " echo $EMAIL sent " >> $M
echo "sleep 1" >> $M
done < $INPUT
IFS=$OLDIFS
chmod +x $M
Posted in bash, linux, mail, server | Tagged , , | Leave a comment

Generate easy passwords

This script generates combinations of words to form a password. These passwords are easier to remember than true random garbage combinations, which can’t be remembered by anyone. Moreover the passwords are not dictionary words and too difficult to be guessed by a brute force attack.

#!/bin/sh
# Author:       kdguntu@gmail.com
# Copyright:    (c) 2012 GNU Copyright v3
# Function:     Generate a password 
# Requires:     dictionary with words (here with dutch words from package hunspell)
#               perl
 
DICT="/usr/share/hunspell/nl_BE.dic"
NUMBEROFWORDS=4
MINWORDLENGTH=5
FIXWORDLENGTH=true # set to false to disable
 
####################
MINWLMIN1=`expr $MINWORDLENGTH - 1 `
TELLER=0
PASS=""
while [ $TELLER -lt $NUMBEROFWORDS ]             # 4 random words
do 
 RAND=`perl -e "print int rand(99999)"`
 WORD=`sed $RAND"q;d" $DICT | cut -d"/" -f1` 
 WORD=`echo ${WORD} | tr -cd '[[:lower:]]'`      # replace special chars with (a-z)
 CHARS=`echo $WORD|wc -c`
 if [ ${#WORD} -ge $MINWORDLENGTH ]; then        # wordlength at least 5
   if $FIXWORDLENGTH 
    then
       WORD=${WORD:0:$MINWORDLENGTH}             # cut at 5th char
       C5=`echo ${WORD:$MINWLMIN:1} | tr -cd 'aeiou'|wc -c`  
         if [ ${C5} -gt 0 ]                      # 5th char is vowel
            then  
              TELLER=`expr $TELLER + 1`
              PASS="$PASS${WORD^}"
         fi
    else
       TELLER=`expr $TELLER + 1`
       PASS="$PASS${WORD^}"
 
   fi
 fi
done
echo $PASS

I check if the 5th character is a vowel, because the combined password sounds (remembers) better that way. You can easily adapt the script with extra (or less) restrictions.
An example of the password it generates is “GemoePolieVergeTenni”

Posted in bash, linux, macosx, regex, ubuntu | Tagged , , , , , | Leave a comment

Watermark all images

Recently I had to watermark around 500 images for an existing site.
Images are replaced with watermarked images!
I had problems with temporary/hidden images in the directory so the find statement contains a ! -name “.*” which negates hidden files on linux/macosx.

First script to find all jpg and JPG:

find . -iname "*.jpg" ! -name ".*" -type f -exec ./watermark.sh {} \;

This is the original image:

The script watermark.sh processes each image. Every image is watermarked with a text at the bottom and a almost invisible mask. The mask is an image with a white background and a light-gray logo (it doesn’t have to be transparant). This is the mask:

#!/bin/sh
# Name:         watermark.sh
# Author: 	kdguntu@gmail.com
# Copyright: 	(c) 2011 GNU Copyright v3
# Function:     place watermark on input file
# Requires:	imagemagick (convert, composite)
# Arguments: 	$1 inputfile
 
FILEIN="$1"
FILEOUT="$FILEIN"
FILETMP="$FILEIN.$$"
FILETILE="watermask.jpg"
COPYRIGHT="kdguntu@gmail.com"
 
# Place copyright text at the bottom
convert -size 120x15 xc:none -gravity center \
        -redoke black -redokewidth 2 -annotate 0 $COPYRIGHT \
        -background none -shadow 120x5+0+0 +repage \
        -redoke none -fill white  -annotate 0 $COPYRIGHT \
        $FILEIN  +swap -gravity south -geometry +0+5 \
        -composite $FILETMP
 
# Place mask image over picture (tiled)
composite -compose bumpmap  -tile $FILETILE \
          $FILETMP  $FILEOUT
 
rm -f $FILETMP
echo "$FILEIN is watermarked"

This is the watermarked image:

Posted in bash, find, linux, macosx, ubuntu | Tagged , , | Leave a comment

Get filename and or extension

This script uses the filename and the extension of a file using shell scripts. This is useful when you have to rename a file or its extension.

 
#!/bin/sh
# Name:      filenameextension.sh
# Author:    kdguntu@gmail.com
# Function:  Show filename and extension
# Copyright: 2010 kdguntu@gmail.com GNU GPL v3
#            http://www.gnu.org/licenses/gpl.html
 
FILE="./test.txt"
FILE_NOPATH="`basename $FILE`"
EXTENSION1="${FILE_NOPATH##*.}"
NAME1="${FILE_NOPATH%.*}"
EXTENSION2=`echo "$FILE_NOPATH" | cut -d'.' -f2`
NAME2=`echo "$FILE_NOPATH" | cut -d'.' -f1`
NEWEXTENSION="$NAME1.csv"
 
echo "$FILE without PATH is $FILE_NOPATH"
echo "Name:$NAME1 Extension:$EXTENSION1" # best solution
echo "Name:$NAME2 Extension:$EXTENSION2"

Only the “best solution” works also for files named a.b.c or .c

Posted in bash, cut, linux, macosx, ubuntu | Tagged , , , , , | Leave a comment

Create thumbnails with a mask

First we create a mask like this

with this script:

 
#!/bin/bash
# Author:     kdguntu@gmail.com
# Copyright:  (c) 2011 GNU Copyright v3
# Name:       create_mask.sh
# Function:   create thumbnail mask
# Requires:   imagemagick (convert)
 
THUMBRECT="thumbnail_rectangle.png"
THUMBMASK="thumbnail_mask.png"
SIZE=96                                   # thumbnail output size
CSIZE=$((SIZE/10))                        # corner size (10%)
SIZEOK=$((SIZE-1))                        # minus 1 pixel
SIZEX="$SIZEOK"; SIZEY="$SIZEOK"          # shadow will use an extra pixel
 
# Create a gray rectangle with rounded corners
RECT="roundrectangle $CSIZE,$CSIZE $SIZEX,$SIZEY $CSIZE,$CSIZE"
convert -size ${SIZEX}x${SIZEY} xc:none -fill gray -draw "${RECT}" $THUMBRECT
 
# Create a mask with nice lights
convert $THUMBRECT -bordercolor None -border 1x1 \
          -alpha Extract -blur 0x5  -shade 80x8 -alpha On \
          -background gray50 -alpha background -auto-level \
          -function polynomial  3.5,-4.05,2.05,0.3 \
          \( +clone -alpha extract  -blur 0x3 \) \
          -channel RGB -compose multiply -composite \
          +channel +compose -chop 1x1 \
          $THUMBMASK

The next script requires the previous mask image (thumbnail_mask.png) and generates a mask for an existing image (the image is the first argument of the script).
This picture:

Will be combined with the mask and become this image:

#!/bin/bash
# Author:    kdguntu@gmail.com
# Copyright: (c) 2011 GNU Copyright v3
# Function:  create thumbnail
# Requires:  imagemagick (convert)
#            thumbnail mask (see previous script to generate a mask)
# Arguments: First argument: inputfile
#            Second argument (optional): outputfile
 
THUMBMASK="thumbnail_mask.png"
SIZE="96x96"
THUMBOUT="$2"
THUMBPREFIX="thumb_"
THUMBSUFFIX=".png"
 
if ! [ -f $1 ]; then
  echo 1&gt;&amp;2 "Usage: $0 inputfile [outputfile]"; exit 1
fi
 
BIGIN="$1"; BIGINFILE="`basename $1`"
 
if [ -z "$2" ]; then
 THUMBOUT="$THUMBPREFIX$BIGINFILE$THUMBSUFFIX"
fi
 
convert   -define jpeg:size=200x200 $BIGIN  -thumbnail $SIZE^ \
          -gravity center -extent $SIZE $BIGIN.temp.png
 
convert $BIGIN.temp.png $THUMBMASK \
          \( -clone 0,1 -compose Hardlight -composite \) \
          -delete 0 -compose In -composite \
          $THUMBOUT
 
rm -f $BIGIN.temp.png

Both scripts can be combined and used with find to process a lot of images:

./create_mask.sh
find . -iname "*.jpg" -exec ./create_thumb_with_mask.sh {} \;
Posted in Uncategorized | Leave a comment

Check if running chrooted

This script checks if we are running in a chroot environment

#!/bin/sh
# Name: checkroot.sh
# Author: kdguntu@gmail.com
# Function: check if the script is running within a chroot environment
# Parameter: none
# Copyright: 2011 kdguntu@gmail.com GNU GPL v3
# http://www.gnu.org/licenses/gpl.html
 
if [ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ];
  then echo "Don’t run this script within the chroot environment"
  exit 1
fi
Posted in bash, chroot, linux, stat, ubuntu | Leave a comment

Use a shell function, check if a file exists, true or false

This script uses a shell function to check if a file exists (-f). It returns 0 (zero) if the file exists and 1 if it does not exist. You can write your own check functions for checking if a directory (-d) exists, if a file is writable (-w). See “man test” for all check you could do within the if clause.

#!/bin/sh
# Name: isfile.sh
# Author: kdguntu@gmail.com
# Function: Check if a file exists
# Parameter: none
# Copyright: 2011 kdguntu@gmail.com GNU GPL v3
# http://www.gnu.org/licenses/gpl.html 
FILE="myfile.txt"
isfile() {
if [ -f "$1" ];
  then
    return 0
  else
 echo "File $1 not found"  return 1
fi
}
 
if ! isfile "$FILE";then echo "Please provide $FILE"; exit 1; fi

There might be a confusion between the exit code and the return code.

An exit code stops the program with an (invisible but interceptable) code.

exit 0 means the program exits without errors
exit 1-255 means the program exits with an error code.
You could use your own exit code numbers here, depending on the type of error.
For example exit 2 means file not found, exit 3 means configuration error, etc…
One can “see” this exit code by doing a echo $? when the program finishes

A return code of a function, like the function isfile() from above returns true (0) or false (1)

Posted in bash, cut, function, linux, regex, sed, Uncategorized, windows | Tagged , , | Leave a comment