Tuesday, August 19, 2014

Modify all LDAP users in one shot

Once I need to reset login shell for all users. Here is easy solution, one simple loop

SAVEIFS=$IFS;
IFS=$(echo -en "\n\b");
for i in $(ldapsearch -h localhost -p 389 -D "cn=Manager,dc=<yourdomain>,dc=com" -w "<Manager's passwd>" -s sub -b  "ou=Users,dc=<yourdomain>,dc=com" | grep -v root | grep "dn:")
do
LDIF_FILE=/tmp/loginShell.ldif;
echo "${i}" > $LDIF_FILE;
echo "changetype: modify" >> $LDIF_FILE;
echo "replace: loginShell" >> $LDIF_FILE;
echo "loginShell: /usr/sbin/user_shell" >> $LDIF_FILE;
/usr/bin/ldapmodify -h localhost -p 389 -D "cn=Manager,dc=<yourdomain>,dc=com" -w "<Manager's passwd>" -f $LDIF_FILE;
done;
IFS=$SAVEIFS;


Of course, you can put instead of  loginShell any other parameters. Don't forget, to change multiply parameters put  '-' separator:

*****
        echo "replace: userPassword" >> $LDIF_FILE
        echo "userPassword: $PASSWD_SSHA" >> $LDIF_FILE
        echo "-" >> $LDIF_FILE
        echo "replace: sambaNTPassword" >> $LDIF_FILE
        echo "sambaNTPassword: $sambaNTPassword" >> $LDIF_FILE
  

Friday, August 15, 2014

Asterisk(Bash): watching agents on call/waiting in color

#!/bin/bash
clear
while true
do
buffer=$(
clear
/usr/sbin/asterisk -rx "show queue <My_Queue>" \
| grep Agent \
| grep -v \(Unavailable\) \
| sort -t"(" -k 2 \
| GREP_COLOR='01;31' egrep -i --color=always '^.*[0-9] \(Not in use.*$|$' \
| GREP_COLOR='01;32' egrep -i --color=always '^.*[0-9] \(Busy.*$|$' \
| GREP_COLOR='01;34' egrep -i --color=always '^.*\(paused.*$|$' )

echo "$buffer"
sleep 2
done

exit 0

Thursday, August 14, 2014

ZFS::chmod: changing permissions of Operation not permitted

Problem: 

I can't change a folder permissions running on NFS client:

chmod: changing permissions of <somedir>: Operation not permitted


But from ZFS/NAS server the command is working


Solutiom: 


You need to add no_root_squash to your /etc/exports

But if you are running ZFS, you'll need to set

#sharemgr set -P nfs -p anon=0 zfs/data

Where zfs/data is name of your pool
Then you can check:

#sharemgr show -vp

Output:
default nfs=()

zfs

    zfs/rpool nfs=()
          /rpool
    zfs/data nfs=(anon="0")
          /data

read more:
http://prefetch.net/blog/index.php/2009/05/05/using-sharemgr-to-manage-nfs-file-systems-on-opensolaris-hosts/

Monday, August 11, 2014

GREP: colored output

http://stackoverflow.com/questions/17236005/grep-output-with-multiple-colors

EXAMPLE:  Colored output from asterisk CLI

 asterisk -rvvvvvvv | GREP_COLOR='01;36' egrep --color=always '^.*answered SIP.*$|$' | GREP_COLOR='01;31' egrep -i --color=always '^.*Got SIP response.*$|$' | grep -E '(answered SIP|Got SIP response)'

Thursday, August 7, 2014

BASH Shell: For Loop File Names With Spaces

http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html

BASH for loop works nicely under UNIX / Linux / Windows and OS X while working on set of files. However, if you try to process a for loop on file name with spaces in them you are going to have some problem. For loop uses $IFS variable to determine what the field separators are. By default $IFS is set to the space character. There are multiple solutions to this problem.

Set $IFS variable

Try it as follows:
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in *
do
  echo "$f"
done
IFS=$SAVEIFS
OR
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# set me
FILES=/data/*
for f in $FILES
do
  echo "$f"
done
# restore $IFS
IFS=$SAVEIFS

More examples using $IFS and while loop

Now you know that if the field delimiters are not whitespace, you can set IFS. For example, while loop can be used to get all fields from /etc/passwd file:
....
while IFS=: read userName passWord userID groupID geCos homeDir userShell
do
      echo "$userName -> $homeDir"
done < /etc/passwd

Using old good find command to process file names

To process the output of find with a command, try as follows:
find . -print0 | while read -d $'\0' file
do
  echo -v "$file"
done
Try to copy files to /tmp with spaces in a filename using find command and shell pipes:
find . -print0 | while read -d $'\0' file; do cp -v "$file" /tmp; done

Processing filenames using an array

Sometimes you need read a file into an array as one array element per line. Following script will read file names into an array and you can process each file using for loop. This is useful for complex tasks:
#!/bin/bash
DIR="$1"
 
# failsafe - fall back to current directory
[ "$DIR" == "" ] && DIR="."
 
# save and change IFS 
OLDIFS=$IFS
IFS=$'\n'
 
# read all file name into an array
fileArray=($(find $DIR -type f))
 
# restore it 
IFS=$OLDIFS
 
# get length of an array
tLen=${#fileArray[@]}
 
# use for loop read all filenames
for (( i=0; i<${tLen}; i++ ));
do
  echo "${fileArray[$i]}"
done

Playing mp3s with spaces in file names

Place following code in your ~/.bashrc file:
mp3(){
 local o=$IFS
 IFS=$(echo -en "\n\b")
 /usr/bin/beep-media-player "$(cat  $@)" &
 IFS=$o
}
Keep list of all mp3s in a text file such as follows (~/eng.mp3.txt):
/nas/english/Adriano Celentano - Susanna.mp3
/nas/english/Nick Cave & Kylie Minogue - Where The Wild Roses Grow.mp3
/nas/english/Roberta Flack - Kiling Me Softly With This Song.mp3
/nas/english/The Beatles - Girl.mp3
/nas/english/John Lennon - Stand By Me.mp3
/nas/english/The Seatbelts, Cowboy Bebop - 01-Tank.mp3
To play just type:
$ mp3 eng.mp3.txt

Saturday, August 2, 2014

bash:: tolowercase,to uppercase

http://stackoverflow.com/questions/2264428/converting-string-to-lower-case-in-bash-shell-scripting

To lowercase
$ string="A FEW WORDS"
$ echo ${string,}
a FEW WORDS
$ echo ${string,,}
a few words
$ echo ${string,,[AEIUO]}
a FeW WoRDS

$ string="A Few Words"
$ declare -l string
$ string=$string; echo $string
a few words
To uppercase
$ string="a few words"
$ echo ${string^}
A few words
$ echo ${string^^}
A FEW WORDS
$ echo ${string^^[aeiou]}
A fEw wOrds

$ string="A Few Words"
$ declare -u string
$ string=$string; echo $string
A FEW WORDS
Toggle (undocumented)
$ string="A Few Words"
$ echo ${string~~}
a fEW wORDS
$ string="A FEW WORDS"
$ echo ${string~}
a fEW wORDS
$ string="a few words"
$ echo ${string~}
A Few Words
Capitalize (undocumented)
$ string="a few words"
$ declare -c string
$ string=$string
$ echo $string
A few words
Title case:
$ string="a few words"
$ string=($string)
$ string=${string[@]^}
$ echo $string
A Few Words

$ declare -c string
$ string=(a few words)
$ echo ${string[@]}
A Few Words