nb_repos() {
[ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos' # for busybox
(
- # Always !
- echo "$NB_ROOT"
+ # Always !
+ echo "$NB_ROOT"
- # Conf file
- local conf="$NB_ROOT/repos.conf"
- [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf"
+ # Conf file
+ local conf="$NB_ROOT/repos.conf"
+ [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf"
- # Realpath
- #ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null
- local IFS="
+ # Realpath
+ #ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null
+ local IFS="
"
- for dir in $(eval ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null); do
+ for dir in $(eval ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null); do
- # Accept directories or files (ex: use to tag a directory: .nb-install)
- [ -f "$dir" ] && dir=$(dirname "$dir")
+ # Accept directories or files (ex: use to tag a directory: .nb-install)
+ [ -f "$dir" ] && dir=$(dirname "$dir")
- dir=$(realpath "$dir")
- echo "$dir"
+ dir=$(realpath "$dir")
+ echo "$dir"
- done
+ done
# no doubles
# strong if awk failed
"
for d in `nb_repos`; do
- for f in "$@"; do
+ for f in "$@"; do
- #nb_debug "====$d/$f"
- for repo in "$d"/$f; do
- [ -e "$repo" ] && echo "$repo"
- done
+ #nb_debug "====$d/$f"
+ for repo in "$d"/$f; do
+ [ -e "$repo" ] && echo "$repo"
+ done
- done
+ done
done
}
timeout() {
if which timeout 1>/dev/null; then
- timeout -- $@
+ timeout -- $@
else
- perl -e 'BEGIN{$|=1}; $SIG{ALRM}=sub{exit 1}; alarm(shift @ARGV); system join(" ",@ARGV)' -- $@
+ perl -e 'BEGIN{$|=1}; $SIG{ALRM}=sub{exit 1}; alarm(shift @ARGV); system join(" ",@ARGV)' -- $@
fi
}
#
local msg=""
if [ "$#" -gt 0 ]; then
- msg="$1"
- shift
+ msg="$1"
+ shift
fi
local i
#while [ $# -gt 0 ]; do
for i in $@; do
- case "$i" in
- -h|-help|--help)
- printf "$msg\n"
+ case "$i" in
+ -h|-help|--help)
+ printf "$msg\n"
# NB 25.02.17 case $OSTYPE in
# NB 25.02.17 darwin*) printf "$msg\n" ;;
# NB 25.02.17 *) echo -e "$msg" ;;
# NB 25.02.17 esac
- return 0
- ;;
- esac
- shift 2>/dev/null || break
+ return 0
+ ;;
+ esac
+ shift 2>/dev/null || break
done
return 1
#
local msg=""
if [ "$#" -gt 0 ]; then
- msg="$1"
- shift
+ msg="$1"
+ shift
fi
local args="$*"
local server; server="$1"; shift
local tmp; tmp="/tmp/$FUNCNAME.$USER"
(
- alias | sed -e 's/^alias //' -e 's/^/alias /'
- eval type $(shell_functions|perl -pe 's/\s+/ /') | grep -v ' function$'
- echo "[ -r /etc/profile ] && source /etc/profile"
+ alias | sed -e 's/^alias //' -e 's/^/alias /'
+ eval type $(shell_functions|perl -pe 's/\s+/ /') | grep -v ' function$'
+ echo "[ -r /etc/profile ] && source /etc/profile"
) | ssh "$server" "cat > $tmp || rm -vf $tmp" || return
local args
if [ -z "$@" ]; then
- [ -z "$ssh_opt" ] && ssh_opt='-t'
+ [ -z "$ssh_opt" ] && ssh_opt='-t'
- case "$ZSH_NAME" in
- *zsh) ssh $ssh_opt "$server" "source $tmp && zsh -f -d; rm -f $tmp" ;;
- *) ssh $ssh_opt "$server" "bash --rcfile $tmp; rm -f $tmp" ;;
- esac
+ case "$ZSH_NAME" in
+ *zsh) ssh $ssh_opt "$server" "source $tmp && zsh -f -d; rm -f $tmp" ;;
+ *) ssh $ssh_opt "$server" "bash --rcfile $tmp; rm -f $tmp" ;;
+ esac
else
# NB 31.08.16 ssh $ssh_opt "$server" "bash && rm -f $tmp" <<< ". $tmp; $@"
- echo ". $tmp; $@" | ssh $ssh_opt "$server" "bash && rm -f $tmp"
+ echo ". $tmp; $@" | ssh $ssh_opt "$server" "bash && rm -f $tmp"
fi
}
[ -z "$env_value" ] || env_value=$(printf %s "$env_value" | awk -v RS=: '{ if (!arr[$0]++) {printf("%s%s",!ln++?"":":",$0)}}')
for p in $@; do
- [ -e "$p" ] || continue
- case "${env_value}" in
- *:$p|*:$p:*|$p:*|$p) continue;;
- esac
- [ -z "$env_value" ] || env_value=":${env_value}"
- env_value="${p}${env_value}"
+ [ -e "$p" ] || continue
+ case "${env_value}" in
+ *:$p|*:$p:*|$p:*|$p) continue;;
+ esac
+ [ -z "$env_value" ] || env_value=":${env_value}"
+ env_value="${p}${env_value}"
done
# Clean up doubles
if ! which realpath >/dev/null; then
realpath() {
if which perl 1>/dev/null; then
- perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1"
+ perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1"
else
- case "$OSTYPE" in
- darwin*) readlink "$1" ;;
- *) readlink -f "$1" ;;
- esac
+ case "$OSTYPE" in
+ darwin*) readlink "$1" ;;
+ *) readlink -f "$1" ;;
+ esac
fi
}
fi
ascii() {
if [ ! -t 0 ]; then
- cat | perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }'
+ cat | perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }'
elif [ -n "$*" ]; then
- perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' $@
+ perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' $@
else
- perl -e 'binmode(STDOUT, ":utf8"); for(32..255){ print "$_:".chr($_)."\n"; }'
+ perl -e 'binmode(STDOUT, ":utf8"); for(32..255){ print "$_:".chr($_)."\n"; }'
fi
}
$in | perl -e '
while (<>) {
for (split("",$_)) {
- print sprintf("\%3d",ord($_)).":$_\n";
+ print sprintf("\%3d",ord($_)).":$_\n";
}
}
'
if [[ ${D} != 0 ]]
then
- printf '%d days %02d:%02d:%02d\n' $D $H $M $S
+ printf '%d days %02d:%02d:%02d\n' $D $H $M $S
else
- printf '%02d:%02d:%02d\n' $H $M $S
+ printf '%02d:%02d:%02d\n' $H $M $S
fi
}
local in args
if [ "$1" = "-regex" ]; then
- args="regex=$2"
- shift
- shift
+ args="regex=$2"
+ shift
+ shift
fi
in="cat"
[ -z "$@" ] || in="echo $@"
use NB::Functions qw/bytes2h/;
$exp = "([\\d\\.]+)";
if (@ARGV and $ARGV[0] =~ /regex=(\S+)/) {
- shift @ARGV;
- $exp = $1;
+ shift @ARGV;
+ $exp = $1;
}
}; s/$exp/bytes2h($1)/ge;' $args
}
exp=$1; shift
if [ -z "$_getexp_pcregrep" ]; then
- _getexp_pcregrep=0
- which pcregrep > /dev/null && _getexp_pcregrep=1
+ _getexp_pcregrep=0
+ which pcregrep > /dev/null && _getexp_pcregrep=1
fi
if [ $_getexp_pcregrep = 1 ]; then
- pcregrep -o${num} "$exp" $@
+ pcregrep -o${num} "$exp" $@
else
- perl -ne 'BEGIN{$e=shift @ARGV; $n=shift(@ARGV) or 0; $e="($e)" and $n=1 if $n==0} (@_=m{$e}) and ($n<=scalar(@_)) and print ($n==0 ? $_[0] : $_[$n-1].chr(10))' "$exp" "$num" $@
+ perl -ne 'BEGIN{$e=shift @ARGV; $n=shift(@ARGV) or 0; $e="($e)" and $n=1 if $n==0} (@_=m{$e}) and ($n<=scalar(@_)) and print ($n==0 ? $_[0] : $_[$n-1].chr(10))' "$exp" "$num" $@
fi
return
}
while [ $# -gt 0 ]; do
- if [ "$1" = "-threshold" ]; then
- threshold=$2; shift 2; continue
- fi
+ if [ "$1" = "-threshold" ]; then
+ threshold=$2; shift 2; continue
+ fi
- #colors=$(convert $1 -format "%[mean]" info:-)
+ #colors=$(convert $1 -format "%[mean]" info:-)
- if ! convert "$1" -threshold ${threshold}% -format %c histogram:info:- | grep -qm1 white; then
- echo "$1"
- ret=0
- fi
- shift
+ if ! convert "$1" -threshold ${threshold}% -format %c histogram:info:- | grep -qm1 white; then
+ echo "$1"
+ ret=0
+ fi
+ shift
done
local count; count=0
while [ $# -gt 0 ]; do
- count=$(($count+1))
+ count=$(($count+1))
- if [ "$count" = "$tot" ]; then
- avi="$1"; shift; break
- fi
- echo "$1" >> "$still"
+ if [ "$count" = "$tot" ]; then
+ avi="$1"; shift; break
+ fi
+ echo "$1" >> "$still"
- shift
+ shift
done
#echo $(cat $still)" -> $avi"
#shell_help "$usage" "$@" && return 1
[ -z "$*" ] && echo "$usage" && return 1
if ! perl -e '' 2>/dev/null; then
- echo "cron_d_install: please install perl" 1>&2
- return 1
+ echo "cron_d_install: please install perl" 1>&2
+ return 1
fi
if [ "$#" -gt 0 ]; then
- case "$1" in -t|--test) test=1; shift ;; esac
+ case "$1" in -t|--test) test=1; shift ;; esac
fi
#echo "$#"; return
if [ $# -gt 3 ]; then
- shell_help_noarg "$usage" "--help" && return 1
+ shell_help_noarg "$usage" "--help" && return 1
#elif [ $# -eq 3 ]; then
elif [ ! -e "$1" ]; then
- fname=$1; shift
- tmp="/tmp/$FUNCNAME.$$"
- trap "rm -f $tmp*" EXIT
- file="$tmp.cron"
+ fname=$1; shift
+ tmp="/tmp/$FUNCNAME.$$"
+ trap "rm -f $tmp*" EXIT
+ file="$tmp.cron"
# NB 31.08.16 cat <<< "$1" > "$file"; shift
- echo "$1" > "$file"; shift
- exp=$1; shift
+ echo "$1" > "$file"; shift
+ exp=$1; shift
else
- file=$1; shift
- exp=$1; shift
- fname=$(basename "$file")
+ file=$1; shift
+ exp=$1; shift
+ fname=$(basename "$file")
fi
#
if [ -d /etc/cron.d ]; then
- if [ "$test" = "1" ]; then
- printf '%s' "$FUNCNAME: /etc/cron.d: $fname"
- [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return
- echo ': Not changed'
- return 1
- fi
+ if [ "$test" = "1" ]; then
+ printf '%s' "$FUNCNAME: /etc/cron.d: $fname"
+ [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return
+ echo ': Not changed'
+ return 1
+ fi
- [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && cp "$file" "/etc/cron.d/$fname"
- return
+ [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && cp "$file" "/etc/cron.d/$fname"
+ return
fi
#
)
if [ "$test" = "1" ]; then
- printf '%s' "$FUNCNAME: crontab: $fname"
- #echo -e "\n<\n$(crontab -l)\n>\n<\n$cron\n>\n"
- [ "$cron" != "$(crontab -l)" ] && echo ': Changed' && return
- echo ": Not changed; return 1"
+ printf '%s' "$FUNCNAME: crontab: $fname"
+ #echo -e "\n<\n$(crontab -l)\n>\n<\n$cron\n>\n"
+ [ "$cron" != "$(crontab -l)" ] && echo ': Changed' && return
+ echo ": Not changed; return 1"
fi
# NB 31.08.16 [ "$cron" != "$(crontab -l)" ] && crontab <<< "$cron"
}
csv2human() {
- shell_help "Usage: $FUNCNAME [PERL_REGEXP --sep|-s (default: tab)] [--noheader|-nh]" "$@" && return;
- local sep noheader;
- sep='\t';
- noheader=0;
- local files=''
-
- while [ $# -gt 0 ]; do
-
- if [ -e "$1" ]; then
- files="$files $1"
- shift
- continue
- fi
-
- case "$1" in
- --sep|-s)
- sep=$2;
- shift
- ;;
- --noheader|-nh)
- noheader=1
- ;;
- *)
- echo "Unknow option: $1 at $0!";
- return 1
- ;;
-
- esac
-
- shift
-
- done
-
- [ -z "$@" ] || sep="$@";
-
- cat $files | perl -MEncode -F"$sep" -ane 'BEGIN {
+ shell_help "Usage: $FUNCNAME [PERL_REGEXP --sep|-s (default: tab)] [--noheader|-nh]" "$@" && return;
+ local sep noheader;
+ sep='\t';
+ noheader=0;
+ local files=''
+
+ while [ $# -gt 0 ]; do
+
+ if [ -e "$1" ]; then
+ files="$files $1"
+ shift
+ continue
+ fi
+
+ case "$1" in
+ --sep|-s)
+ sep=$2;
+ shift
+ ;;
+ --noheader|-nh)
+ noheader=1
+ ;;
+ *)
+ echo "Unknow option: $1 at $0!";
+ return 1
+ ;;
+
+ esac
+
+ shift
+
+ done
+
+ [ -z "$@" ] || sep="$@";
+
+ cat $files | perl -MEncode -F"$sep" -ane 'BEGIN {
$noheader = shift @ARGV;
@len = (); @lines = ();
binmode( STDOUT, "utf8:" );
$sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10);
print $sep_line;
while ($_ = shift @lines) {
- printf $format,@$_;
- print $sep_line if !$noheader and !$i++;
+ printf $format,@$_;
+ print $sep_line if !$noheader and !$i++;
}
print $sep_line;
print "$tot Records\n";
)
echo "$out"
case "$out" in
- *"Relay access denied"*) return 0 ;;
- *) return 1 ;;
+ *"Relay access denied"*) return 0 ;;
+ *) return 1 ;;
esac
}
local ret="$1"
local to_var="$2"
case "$1" in
- #) echo "Escape: $i" ;;
- *[^[:alpha:]./_-]*) ret='"'${1//\"/\\\"}'"' ;;
+ #) echo "Escape: $i" ;;
+ *[^[:alpha:]./_-]*) ret='"'${1//\"/\\\"}'"' ;;
esac
[ -z "$to_var" ] && echo "$ret" && return
eval "$to_var=\"\$$to_var \$ret\"; $to_var=\${$to_var## };"
local length="${#1}"
local i
for i in `seq 0 $(($length-1))`; do
- eval 'local c="${1:i:1}"' # TODO compatible with busybox
- case $c in
- [a-zA-Z0-9.~_-]) printf "$c" ;;
- *) printf '%%%02X' "'$c"
- esac
+ eval 'local c="${1:i:1}"' # TODO compatible with busybox
+ case $c in
+ [a-zA-Z0-9.~_-]) printf "$c" ;;
+ *) printf '%%%02X' "'$c"
+ esac
done
}
local mbr=`dd if=/dev/sda bs=446 count=1 2> /dev/null| strings -a`
case "$mbr" in
- *GRUB*) echo "GRUB" ;;
- *LILO*) echo "LILO" ;;
- *) return 1 ;;
+ *GRUB*) echo "GRUB" ;;
+ *LILO*) echo "LILO" ;;
+ *) return 1 ;;
esac
return 0
is_prompt() {
#[[ $- == *i* ]] && echo "Interactive" || echo "Not interactive"
case "$-" in
- *i*) return 0 ;;
- *) return 1 ;;
+ *i*) return 0 ;;
+ *) return 1 ;;
esac
}
ls_users() {
case "$OSTYPE" in
- darwin*)
- dscacheutil -q user \
- | perl -ne '/^\w+:\s+(.*)?$/; push @_,$1; print join(":",$_[0],"*",@_[2,3,6,4,5])."\n" and @_=() if /^\s*$/' \
- | sort -u \
- | sort -t : -k3,4 -n \
- ;
- ;;
- *)
- if [ -n "$(which getent 2>/dev/null)" ]; then
- getent passwd
- elif [ -e /etc/passwd ]; then
- grep '^[^#:]\+:' /etc/passwd | sort -u | sort -t : -k3,4 -n
- else
- echo "$FUNCNAME: can not find any users on this system: #OSTYPE" 1>&2
- return 1
- fi
- ;;
+ darwin*)
+ dscacheutil -q user \
+ | perl -ne '/^\w+:\s+(.*)?$/; push @_,$1; print join(":",$_[0],"*",@_[2,3,6,4,5])."\n" and @_=() if /^\s*$/' \
+ | sort -u \
+ | sort -t : -k3,4 -n \
+ ;
+ ;;
+ *)
+ if [ -n "$(which getent 2>/dev/null)" ]; then
+ getent passwd
+ elif [ -e /etc/passwd ]; then
+ grep '^[^#:]\+:' /etc/passwd | sort -u | sort -t : -k3,4 -n
+ else
+ echo "$FUNCNAME: can not find any users on this system: #OSTYPE" 1>&2
+ return 1
+ fi
+ ;;
esac
}
find_sort_mtime() {
(
- local find_args="$@"
- [ -z "$find_args" ] && args="."
-
- case "$OSTYPE" in
- darwin*)
- find $find_args -type f -exec stat -f '%m'$'\t''%N' {} \;
- ;;
- *)
- find $@ -type f -printf "%T@\t%p\n" | sed 's/^\([0-9]\+\)\.[0-9]\+/\1/'
- ;;
- esac
+ local find_args="$@"
+ [ -z "$find_args" ] && args="."
+
+ case "$OSTYPE" in
+ darwin*)
+ find $find_args -type f -exec stat -f '%m'$'\t''%N' {} \;
+ ;;
+ *)
+ find $@ -type f -printf "%T@\t%p\n" | sed 's/^\([0-9]\+\)\.[0-9]\+/\1/'
+ ;;
+ esac
) | sort -k1 -n $sort_args | epoch2date
# strftime "%a %b %e %H:%M:%S %Y"
local usage="Usage: shell_replace [-i[.BACKUP_EXTENSION]] [-find PAHT]"
if [ -z "$*" -o "$1" = "--help" -o "$1" = "-h" ]; then
- echo "$usage"
- return
+ echo "$usage"
+ return
fi
if [ "$1" = "-find" ]; then
- shift
- grep -ErlI '^.{1,3}<SHELL_REPLACE' ${@:?$usage}
- return
+ shift
+ grep -ErlI '^.{1,3}<SHELL_REPLACE' ${@:?$usage}
+ return
fi
perl -ne '
local url='';
for url in http://ipinfo.io/ip http://www.ipchicken.com http://icanhazip.com http://ident.me http://nicolas.boisselier.free.f/myip/;
do
- http_get "$url" | perl -ne 'print "$1\n" and exit 64 if /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/';
- [ "$?" == "64" ] && return 0;
+ http_get "$url" | perl -ne 'print "$1\n" and exit 64 if /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/';
+ [ "$?" == "64" ] && return 0;
done;
return 1
}
http_get() {
if which curl 1>/dev/null; then
- curl -sk $*
+ curl -sk $*
elif which wget 1>/dev/null; then
- wget -O - --quiet --no-verbose --no-check-certificate --timeout 5 --tries 1 $*
+ wget -O - --quiet --no-verbose --no-check-certificate --timeout 5 --tries 1 $*
elif which lynx 1>/dev/null; then
- lynx --source -dump $*
+ lynx --source -dump $*
else
- echo "Can't find web client" 1>&2
+ echo "Can't find web client" 1>&2
fi
}
# NB 07.01.17 alias Get=http_get
http_head() {
if which curl 1>/dev/null; then
- curl -sk --head $*
+ curl -sk --head $*
elif which wget 1>/dev/null; then
- http_get --save-headers - $* | perl -ne '/^\s*$/ and last;print'
+ http_get --save-headers - $* | perl -ne '/^\s*$/ and last;print'
elif which lynx 1>/dev/null; then
- lynx --head -dump $*
+ lynx --head -dump $*
else
- echo "Can't find web client" 1>&2
+ echo "Can't find web client" 1>&2
fi
}
# Cache
case "$_which_cache|" in
- *"|$1=1|"*)
- nb_debug "which_cache() $1=1"
- return 0
- ;;
- *"|$1=0|"*)
- nb_debug "which_cache() $1=0"
- return 1
- ;;
+ *"|$1=1|"*)
+ nb_debug "which_cache() $1=1"
+ return 0
+ ;;
+ *"|$1=0|"*)
+ nb_debug "which_cache() $1=0"
+ return 1
+ ;;
esac
#nb_debug "which_cache() $1=?"
for p in $PATH; do
- test -x "$p/$1" || continue
+ test -x "$p/$1" || continue
- _which_cache="$_which_cache|$1=1"
- echo "$1"
- return 0
+ _which_cache="$_which_cache|$1=1"
+ echo "$1"
+ return 0
done
_which_cache="$_which_cache|$1=0"
local color=16
while [ $color -lt 245 ]; do
- echo -ne "$color: \\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m"
- printf '\t%s\n' "\\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m"
- color=$(($color+1))
+ echo -ne "$color: \\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m"
+ printf '\t%s\n' "\\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m"
+ color=$(($color+1))
done
}
local size=2
[ "$1" = "-size" ] && shift && size=$1 && shift
local space=$(eval "printf ' %.0s' {1..$size}")
- perl -pe "1 while s/^\t/$space/" $@
+# NB 08.12.17 perl -pe "1 while s/^\t/$space/" $@
+ perl -pe "s/\G[\t]/$space/g" $@
}
space2tab() {
local size=2
[ "$1" = "-size" ] && shift && size=$1 && shift
local space=$(eval "printf ' %.0s' {1..$size}")
- #perl -pe "1 while s/^(\t*)?$space/$1\t/" $@
- #perl -pe "1 while s/^$space/\t/; 1 while s/(\t)+$space/$1\t/" $@
- perl -pe "1 while s/^$space/\t/" $@
+# NB 08.12.17 perl -pe "1 while s/^$space/\t/" $@
+ perl -pe "s/^\t/$space/; s/\G[ ]{$size}/\t/g" $@
}
which slapcat > /dev/null || return
ldap_new_id() {
- local attr=${1:-uidNumber}
- local max=${2:-65000}
- max=$(
- slapcat -o ldif-wrap=999 -a "(&($attr=*))" | grep "^$attr:" | sed 's/^[^:]\+: //' | awk '$1 < '$max' {print $0}' | sort -n | tail -1
- )
- [ -z "$max" ] && max=0
- echo $(($max+1))
+ local attr=${1:-uidNumber}
+ local max=${2:-65000}
+ max=$(
+ slapcat -o ldif-wrap=999 -a "(&($attr=*))" | grep "^$attr:" | sed 's/^[^:]\+: //' | awk '$1 < '$max' {print $0}' | sort -n | tail -1
+ )
+ [ -z "$max" ] && max=0
+ echo $(($max+1))
}
ldap_user_password() {
- local pass=$(for i in 0 1 3 4;do slappasswd -gn; done; echo)
- cat <<EOF
+ local pass=$(for i in 0 1 3 4;do slappasswd -gn; done; echo)
+ cat <<EOF
# objectClass: simpleSecurityObject
# userPassword: {CLEARTEXT}$pass
userPassword: $(slappasswd -s "$pass")
#unset -f lapd_config2file 2>&1 | true
ldap_config2file() {
- [ ! -r slapd.d ] && echo slapd.d si missing 1>&2 && return 1
- slapcat -n 0 -F slapd.d -l config.ldif
- echo config.ldif created
+ [ ! -r slapd.d ] && echo slapd.d si missing 1>&2 && return 1
+ slapcat -n 0 -F slapd.d -l config.ldif
+ echo config.ldif created
}
ldap_config2dir() {
- [ ! -r config.ldif ] && echo config.ldif si missing 1>&2 && return 1
- slapadd -n 0 -F slapd.d -l config.ldif
- echo slapd.d created
+ [ ! -r config.ldif ] && echo config.ldif si missing 1>&2 && return 1
+ slapadd -n 0 -F slapd.d -l config.ldif
+ echo slapd.d created
}
ldap_config() {
# NB 03.11.17 ldapsearch -Q -Y EXTERNAL -H ldapi:/// -o ldif-wrap=999 -b cn=config '(!(objectClass=olcSchemaConfig))'
- slapcat -o ldif-wrap=no -b cn=config -a '(!(objectClass=olcSchemaConfig))'
+ slapcat -o ldif-wrap=no -b cn=config -a '(!(objectClass=olcSchemaConfig))'
}
ldap_databases() {
- slapcat -o ldif-wrap=no -b cn=config | grep "^dn: olcDatabase="
+ slapcat -o ldif-wrap=no -b cn=config | grep "^dn: olcDatabase="
}
ldap_schema_config() {
# NB 03.11.17 ldapsearch -x -s base -b "cn=subschema" objectclasses
- slapcat -o ldif-wrap=no -b cn=config -a '(&(objectClass=olcSchemaConfig))'
+ slapcat -o ldif-wrap=no -b cn=config -a '(&(objectClass=olcSchemaConfig))'
}
ldap_cn() {
- #ldapsearch -x '(cn=*)' dn | grep ^dn | sed 's/dn: //'
- slapcat -a 'cn=*' $@ | grep ^dn | sed 's/dn: //'
+ #ldapsearch -x '(cn=*)' dn | grep ^dn | sed 's/dn: //'
+ slapcat -a 'cn=*' $@ | grep ^dn | sed 's/dn: //'
}
slapcat_csv() {
- local aawk human=no human_opt='--noheader'
- local aldap="$1"; shift
- while [ $# -gt 0 ]; do
- case "$1" in
- -head) aawk+=" $1"; human_opt="" ;;
- -human) human=yes; human_opt=""; aawk+=" -head" ;;
- -nohead) human_opt="--noheader"; aawk=${aawk// -head/} ;;
- *) aawk+=" $1" ;;
- esac
- shift
- done
- [ $human = yes ] && slapcat_csv "$aldap" $aawk | csv2human $human_opt && return
- slapcat -a "$aldap" -o ldif-wrap=no | grep -v '^entryCSN' | awk -f $NB_ROOT/lib/awk/ldap2csv.awk -- $aawk
+ local aawk human=no human_opt='--noheader'
+ local aldap="$1"; shift
+ while [ $# -gt 0 ]; do
+ case "$1" in
+ -head) aawk+=" $1"; human_opt="" ;;
+ -human) human=yes; human_opt=""; aawk+=" -head" ;;
+ -nohead) human_opt="--noheader"; aawk=${aawk// -head/} ;;
+ *) aawk+=" $1" ;;
+ esac
+ shift
+ done
+ [ $human = yes ] && slapcat_csv "$aldap" $aawk | csv2human $human_opt && return
+ slapcat -a "$aldap" -o ldif-wrap=no | grep -v '^entryCSN' | awk -f $NB_ROOT/lib/awk/ldap2csv.awk -- $aawk
}
ldapsearch_csv() {
- local aldap aawk human=no human_opt='--noheader'
-
- while [ $# -gt 0 ]; do
- case "$1" in
- -head) aawk+=" $1"; human_opt="" ;;
- -human) human=yes; human_opt=""; aawk+=" -head" ;;
- -nohead) human_opt="--noheader"; aawk=${aawk// -head/} ;;
- *) aldap+=" $1" ;;
- esac
- shift
- done
-
- aldap=${aldap/ /}; aawk=${aawk/ /}
- [ $human = yes ] && ldapsearch_csv $aldap $aawk | csv2human $human_opt && return
- ldapsearch -LLLx -o ldif-wrap=no $aldap | awk -f $NB_ROOT/lib/awk/ldap2csv.awk -- $aawk $aldap
+ local aldap aawk human=no human_opt='--noheader'
+
+ while [ $# -gt 0 ]; do
+ case "$1" in
+ -head) aawk+=" $1"; human_opt="" ;;
+ -human) human=yes; human_opt=""; aawk+=" -head" ;;
+ -nohead) human_opt="--noheader"; aawk=${aawk// -head/} ;;
+ *) aldap+=" $1" ;;
+ esac
+ shift
+ done
+
+ aldap=${aldap/ /}; aawk=${aawk/ /}
+ [ $human = yes ] && ldapsearch_csv $aldap $aawk | csv2human $human_opt && return
+ ldapsearch -LLLx -o ldif-wrap=no $aldap | awk -f $NB_ROOT/lib/awk/ldap2csv.awk -- $aawk $aldap
}
ldap_gup_dir() {
- local dir="$1" && shift
- cd "$dir" || return
- local perm=$(ls -dlaF --color=auto . 2>/dev/null | awk '{print $3"."$4}')
- local tmp=`mktemp -d`
- eval '
- ldap_gup $@ | while read -r -a rec; do
- file=${rec[0]}
- rec=("${rec[@]:1}") # shift
- echo "${rec[@]}" >> "$tmp/$file"
- done
- '
- [ -n "$perm" ] && chown "$perm" "$tmp"/*
- chmod 640 "$tmp"/*
- mv "$tmp"/* ./
- rm -rf "$tmp"
+ local dir="$1" && shift
+ cd "$dir" || return
+ local perm=$(ls -dlaF --color=auto . 2>/dev/null | awk '{print $3"."$4}')
+ local tmp=`mktemp -d`
+ eval '
+ ldap_gup $@ | while IFS=: read -r -a rec; do
+ IFS=:
+ file=${rec[0]}
+ rec=("${rec[@]:1}") # shift
+ #echo "$file ${rec[*]}" && continue
+ echo "${rec[*]}" >> "$tmp/$file"
+ done
+ '
+ (
+ [ -n "$perm" ] && chown "$perm" "$tmp"/*
+ chmod 640 "$tmp"/*
+ mv "$tmp"/* ./
+ ) 2>&1 | true
+ rm -rf "$tmp"
}
ldap_gup() {
- local usage="Usage: ldap_gup [filter attrs ...]"
- case "$*" in
- -h|-help) echo $usage; return;;
- esac
-
- eval '
- local args
- slapcat_csv "(&(objectClass=posixGroup)(memberUid=*))" cn memberUid | while read -r -a rec; do
-
- group=${rec[0]}
- rec=("${rec[@]:1}") # shift
-
- for uid in ${rec[@]}; do
- #echo slapcat_csv "(&(uid=$uid)(userPassword=*))" userPassword
- rec=( $(slapcat_csv "(&(uid=$uid)(userPassword=*))" userPassword uid mail) )
- pass=${rec[0]}
- rec=("${rec[@]:1}") # shift
- for field in ${rec[@]}; do
- echo $group $field:$pass
- done
- done
-
- done
- '
- return
+ local usage="Usage: ldap_gup [filter attrs ...]"
+ case "$*" in
+ -h|-help) echo $usage; return;;
+ esac
+ local filer=$1; shift; [ -n "$filter" ] && filter="($filter)"
+ local attrs=${*:-"cn"}; shift
+
+ eval '
+ local args
+ slapcat_csv "(&(objectClass=posixGroup)(memberUid=*)$filter)" cn memberUid | while read -r -a rec; do
+
+ group=${rec[0]}
+ rec=("${rec[@]:1}") # shift
+
+ for uid in ${rec[@]}; do
+ rec=( $(slapcat_csv "(&(uid=$uid)(userPassword=*))" userPassword $attrs) )
+ pass=${rec[0]}
+ rec=("${rec[@]:1}") # shift
+ for field in ${rec[@]}; do
+ echo $group:$field:$pass
+ done
+ done
+
+ done
+ '
+ return
}