2 # my weird function file
3 # lots of this coming from the internets
6 # command -v ssh-agent >/dev/null || return
7 # [ "$SSH_CONNECTION" ] && return
8 # local info=$HOME/.cache/ssh-agent-info
9 # [ -f $info ] && . $info >/dev/null
10 # [ "$SSH_AGENT_PID" ] && kill -0 $SSH_AGENT_PID 2>/dev/null || {
11 # mkdir -p $(dirname $info)
17 # http://uggedal.com/journal/lazy-ssh-add/
21 [ "$SSH_CONNECTION" ] && return
22 ssh-add
-l >/dev
/null || ssh-add
$key
29 "vm1") _ssh_add
$HOME/.ssh
/id_rsa_kratos
;;
30 "phobos") _ssh_add
$HOME/.ssh
/id_rsa_phobos
;;
31 *) echo "valid key not found" && exit 1 ;;
42 "vm1:") _ssh_add
$HOME/.ssh
/id_rsa_kratos
;;
43 "phobos:") _ssh_add
$HOME/.ssh
/id_rsa_phobos
;;
52 # push|pull|fetch) _ssh_add ;;
59 echo "nameserver 80.67.169.12" | sudo
tee /etc
/resolv.conf
64 local foldertobezipped
=$2
66 #tar -cvf - file1 file2 dir3 | gzip > archive.tar.gz
71 local foldertobezipped
=$2
76 # zip some files using python
80 # call the zip module from python3
81 python3
-m zipfile
-c $2 $1
84 # to unban an ip blocked because of fail ssh connection
85 my_fail2ban_unban_ip
(){
87 sudo fail2ban-client
set ssh unbanip
$ip
91 my_bak
() { cp "$1" "$1_`date +%Y-%m-%d_%H-%M-%S`" ; }
94 # return the service on port
99 my_mount_set_of_partition
(){
100 # Mount set of partitions for recovery.
102 mkdir
/media
/sdd
$i && mount
-o ro
/{dev
,media
}/sdd
$i ||
rmdir /media
/sdd
$i ;
106 my_copy_file_to_clipboard
() {
108 [ -f $file ] && xclip
-sel clip
< $filecontent
114 usage: $PROGNAME options
116 Program deletes files from filesystems to release space.
117 It gets config file that define fileystem paths to work on, and whitelist rules to
121 -c --config configuration file containing the rules. use --help-config to see the syntax.
122 -n --pretend do not really delete, just how what you are going to do.
123 -t --test run unit test to check the program
124 -v --verbose Verbose. You can specify more then one -v to have more verbose
126 -h --help show this help
127 --help-config configuration help
135 $PROGNAME --test test_string.sh
138 $PROGNAME --config /path/to/config/$PROGNAME.conf
140 Just show what you are going to do:
141 $PROGNAME -vn -c /path/to/config/$PROGNAME.conf
146 my_change_owner_of_files
() {
148 local group
=$1; shift
154 chown
$user:$group $i
159 my_template_function
() {
161 local PROGNAME
=$
(basename $0)
162 local PROGDIR
=$
(readlink
-m $
(dirname $0))
167 #readonly local second_arg=$2;
169 # if no args, print usage
170 is_empty_string
$ARGS && my_usage
181 local env
=$HOME"/envs/"$dir"/source/"
182 #if source dir not exists, create & cd
183 [ -d $env ] && cd $env || mkdircd
-p $env
184 # activate virtualenv
185 source $HOME"/envs/"$dir"/bin/activate"
188 # Taken from http://aaroncrane.co.uk/2009/03/git_branch_prompt/
191 until [ "$dir" -ef / ]; do
192 if [ -f "$dir/.git/HEAD" ]; then
193 GIT_REPO
=`readlink -e $dir`/
203 head=$
(< "$1/.git/HEAD")
204 if [[ $head == ref
:\ refs
/heads
/* ]]; then
205 GIT_BRANCH
=${head#*/*/}
206 elif [[ $head != '' ]]; then
207 GIT_BRANCH
='(detached)'
209 GIT_BRANCH
='(unknown)'
213 # Taken from https://github.com/jimeh/git-aware-prompt
215 local status
=$
(git status
--porcelain 2> /dev
/null
)
216 if [[ "$status" != "" ]]; then
224 if [ -e "$1/.git/refs/stash" ]; then
231 cdls
() { cd "$@" && clear && ls; }
248 mkdir
-p "$@" && eval cd "\"\$$#\"";
252 local serverdir
="${TMPDIR:-/tmp}/emacs${UID}"
254 for file in ${serverdir}/*; do
255 if [[ -S ${file} ]]; then
256 servers
+=("${file##*/}")
280 printf '%s ' "$result";
287 echo "[$(date + '%Y-%m-%dT%H:%M:%S%z')]: $@ >&2"
296 my_err
"unable to my_test"
297 exit "${E_DID_NOTHING}"
301 my_pretty_print_json
() {
305 my_not_start_service_on_boot
(){
306 # service to not start on boot
308 update-rc.d
$service disable
;
311 my_install_new_font
(){
318 sudo ip link
set eth0 up
;
319 # ask for an ip via dhcp
323 #function to show the current branch
324 my_show_current_branch
(){
325 git rev-parse
--abbrev-ref HEAD
331 echo $
(pdftk
$filename dump_data \
332 |
grep -i numberofpages \
338 echo $
(pdftk
$filename burst
);
342 for i
in $
(ls |
grep .pdf$
); do
347 # function to show one line of file
349 local line_number
=$1;
353 echo $
(sed -n $line_number$p $filename);
356 # function to extract in one command
360 if is_file
$filename ; then
362 *.
tar.xz
) tar xvfJ
$filename ;;
363 *.xz
) tar xf
$filename ;;
364 *.
tar.bz2
) tar xvjf
$filename ;;
365 *.
tar.gz
) tar xvzf
$filename ;;
366 *.bz2
) bunzip2
$filename ;;
367 *.rar
) unrar x
$filename ;;
368 *.gz
) gunzip
$filename ;;
369 *.
tar) tar xvf
$filename ;;
370 *.tbz2
) tar xvjf
$filename ;;
371 *.tgz
) tar xvzf
$filename ;;
372 *.
zip) unzip $filename ;;
373 *.Z
) uncompress $filename ;;
374 *.7z
) 7z x
$filename ;;
375 *) echo "'$filename' cannot be extracted via extract" ;;
378 echo "'$filename' is not a valid file"
382 # function to list the content of an archive
383 my_list_archive_content
() {
386 if is_file
$filename ; then
388 *.
tar.xz
) tar tvfJ
$filename ;;
389 *.
tar.bz2
) tar tvjf
$filename ;;
390 *.
tar.gz
) tar tzvf
$filename ;;
391 *.bz2
) bunzip2
$filename ;;
392 *.gz
) gunzip
-l $filename ;;
393 *.
tar) tar tvf
$filename ;;
394 *.tbz2
) tar tvjf
$filename ;;
395 *.tgz
) tar tvzf
$filename ;;
396 *.
zip) unzip -l $filename ;;
397 *.Z
) uncompress -l $filename ;;
398 *.7z
) 7z l
$filename ;;
399 *) echo "content of '$filename' cannot be listed :(" ;;
402 echo "'$filename' is not a valid file"
406 # dump a mysql database
410 echo "dumping database '$database' ..."
411 mysqldump
-u patrick
-ppatrick $database > $databse_dump_$
(date +%m
%d
%Y
).sql
415 my_dump_mysql_table
() {
419 echo "dumping table '$table' from database '$database' ..."
420 mysqldump
-u patrick
-ppatrick $database $table > $table_dump_$
(date +%m
%d
%Y
).sql
429 echo "upload of '$filename' to '$ftp_url' by '$user' ..."
430 #curl -T $filename ftp://dev.africafilms.tv/movies/ --user dev.africafilms.tv:dev/aftv/DAK2602
431 curl
-T $filename $ftp_url --user $user:$password
435 local string_to_find
=$1;
438 echo "recursive looking for the string '$string' inside '$directory' directory"
439 grep -l -R -i -r $string_to_find $directory;
443 local string_to_find
=$1;
446 echo "recursive looking for the string '$string' inside '$directory' directory"
447 find $directory |
xargs grep $string -sl;
450 # extract md5 hash from a string
454 echo "md5 hash of '$string' is: "
455 echo -n "$string" |
md5sum;
458 # Creates an archive (*.tar.gz) from given directory.
462 tar cvzf
"${directory%%/}.tar.gz" "${directory%%/}/";
465 # Create a ZIP archive of a file or folder.
469 zip -r "${file%%/}.zip" "$file";
472 # Make your directories and files access rights sane.
473 my_sanitize
() { chmod -R u
=rwX
,g
=rX
,o
= "$@" ;}
475 # Get IP adress on ethernet.
477 local my_ip
=$
( /sbin
/ifconfig eth0 \
478 |
awk '/inet/ { print $2 } ' \
480 local my_gtw
="gtw: "$
(netstat
-nr |
awk '{print $2}' |
sed -n 3p
);
481 echo ${my_ip:-"Not connected"}
482 echo ${my_gtw:-"Not connected"}
485 # Get current host related info.
487 echo -e "\nYou are logged on ${BRed}$HOST"
488 echo -e "\n${BRed}Local IP Address :$NC" ; my_ip
489 echo -e "\n${BRed}Current date :$NC " ; date; calendar |
head -1
490 echo -e "\n${BRed}Additionnal information:$NC " ; uname
-a
491 echo -e "\n${BRed}Users logged on:$NC " ; \
496 echo -e "\n${BRed}Machine stats :$NC " ; uptime
497 echo -e "\n${BRed}Memory stats :$NC " ; free
-h
498 echo -e "\n${BRed}Diskspace :$NC " ; mydf
/ $HOME
499 echo -e "\n${BRed}Open connections :$NC "; netstat
-pan --inet;
503 # Pretty-print of 'df' output. Inspired by 'dfc' utility.
511 echo -e $fs" :No such file or directory" ; continue
514 local info
=( $
(command df
-P $fs |
awk 'END{ print $2,$3,$5 }') )
515 local free
=( $
(command df
-Pkh $fs |
awk 'END{ print $4 }') )
516 local nbstars
=$
(( 20 * ${info[1]} / ${info[0]} ))
518 for ((j
=0;j
<20;j
++)); do
519 if [ ${j} -lt ${nbstars} ]; then
525 out
=${info[2]}" "$out"] ("$free" free on "$fs")"
531 # debootstrap --verbose \
532 # --variant=minbase \
533 # --arch=i386 --include ifupdown,locales,libui-dialog-perl,dialog,dhcp3-client,netbase,net-tools,iproute,openssh-server \
534 # sid /var/lib/lxc/sid http://ftp.debian.org/debian
540 local rememberfile
="$HOME/.remember"
541 # if the file not exists, touch it
542 [[ ! -f $rememberfile ]] && touch $rememberfile
543 if [ $# -eq 0 ] ; then
544 echo "Type what you want to remember then, hit ^D: "
545 cat - >> $rememberfile;
547 #echo $(date +%m%d%Y)" -- $@" >> $rememberfile
548 echo $
(date +%F
)" -- $@" >> $rememberfile
553 local rememberfile
="$HOME/.remember"
555 #is_not_file $remberfile && echo 'nothing to do' ||
556 #is_empty $rememberfile && echo 'nothing to do' ||
558 is_not_file
$rememberfile ||
[[ $
(cat $rememberfile |
wc -l) = 0 ]] && echo "nothing to do" || my_display_todo
"$@";
563 local rememberfile
="$HOME/.remember"
565 #[[ $# -eq 0 ]] && more $rememberfile || grep -i "$@" $rememberfile | more;
566 [[ $# -eq 0 ]] && is_file
$rememberfile && is_not_empty_file
$rememberfile && cat $rememberfile ||
grep -i "$@" $rememberfile;
571 #lsbytesum - sum the number of bytes in a directory listing
573 for Bytes
in $
(ls -l |
grep "^-" |
awk '{ print $5 }')
575 let TotalBytes
=$TotalBytes+$Bytes
577 TotalMeg
=$
(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" |
bc)
584 declare -a $listname;
589 sensonet_create_event
() {
590 curl
-i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" -d "name=$1" http
://projects.emerginov.orange.sn
/sensonet
/resources
/events
/"$2"
593 sensonet_update_event
() {
594 curl
-i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" -d "name=$1" http
://projects.emerginov.orange.sn
/sensonet
/resources
/events
/"$2"
598 [[ $# -eq 0 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/events
/ || \
599 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/events
/"$1"
602 sensonet_del_event
() {
603 curl
-i -X "DELETE" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/events
/"$1"
606 sensonet_create_sensor
() {
607 curl
-i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" -d "description=$1" http
://projects.emerginov.orange.sn
/sensonet
/resources
/sensors
/"$2"
610 sensonet_update_sensor
() {
611 curl
-i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" -d "description=$1" http
://projects.emerginov.orange.sn
/sensonet
/resources
/sensors
/"$2"
614 sensonet_del_sensor
() {
615 curl
-i -X "DELETE" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/sensors
/"$1"
619 [[ $# -eq 0 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/sensors
/ || \
620 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/sensors
/"$1"
623 sensonet_create_network
(){
624 curl
-i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "name=$1" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$2"
627 sensonet_update_network
(){
628 curl
-i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "name=$1" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$2"
631 sensonet_del_network
(){
632 curl
-i -X "DELETE" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$1"
636 [[ $# -eq 0 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/ || \
637 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$1"
640 sensonet_network_probes
() {
641 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$1"/probes
644 sensonet_create_probe
() {
645 curl
-i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" \
646 -d "name=${1}&latitude=0.0&longitude=0.0&monitorMe=1&sleepingTime=0&networkId=univlab" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${2}"
649 sensonet_update_probe
() {
650 curl
-i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" \
651 -d "name=${1}&latitude=48.0&longitude=-1.0&monitorMe=1&sleepingTime=0" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${2}"
655 [[ $# -eq 0 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/ || \
656 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"
659 sensonet_del_probe
() {
660 curl
-i -X "DELETE" -k -H -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${2}"
663 sensonet_probe_neighborhood
(){
664 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"/neighborhood
667 sensonet_probe_set_sleeping_time
(){
668 curl
-i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "duration=${2}" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"/sleepingtime
671 sensonet_probe_sensors
(){
672 [[ $# -eq 1 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"/sensors || \
673 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"/sensors
/"${2}"
676 sensonet_probe_sensor_values
(){
677 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"/sensors
/"${2}"/values
681 # Show top 10 history command on screen
683 history |
awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' |
sort -rn |
head
686 # Wrapper for host and ping command
687 # Accept http:// or https:// or ftps:// names for domain and hostnames
688 _getdomainnameonly
(){
691 # remove protocol part of hostname
698 # remove username and/or username:password part of hostname
701 # remove all /foo/xyz.html*
703 # show domain name only
709 local array
=( $@
) # get all args in an array
710 local len
=${#array[@]} # find the length of an array
711 local host=${array[$len-1]} # get the last arg
712 local args
=${array[@]:0:$len-1} # get all args before the last arg in $@ in an array
713 local _ping
="/bin/ping"
714 local c
=$
(_getdomainnameonly
"$host")
715 [ "$t" != "$c" ] && echo "Sending ICMP ECHO_REQUEST to \"$c\"..."
722 local len
=${#array[@]}
723 local host=${array[$len-1]}
724 local args
=${array[@]:0:$len-1}
725 local _host
="/usr/bin/host"
726 local c
=$
(_getdomainnameonly
"$host")
727 [ "$t" != "$c" ] && echo "Performing DNS lookups for \"$c\"..."