2 # my weird function file
3 # lots of this coming from the internets
6 # test if given argument
9 local default
="/var/log/nginx/access.log"
10 tail -f $default |
awk '$9 == 500 { print $0 }'
13 tail -f $log |
awk '$9 == 500 { print $0 }'
19 # command -v ssh-agent >/dev/null || return
20 # [ "$SSH_CONNECTION" ] && return
21 # local info=$HOME/.cache/ssh-agent-info
22 # [ -f $info ] && . $info >/dev/null
23 # [ "$SSH_AGENT_PID" ] && kill -0 $SSH_AGENT_PID 2>/dev/null || {
24 # mkdir -p $(dirname $info)
34 [ "$SSH_CONNECTION" ] && return
35 ssh-add
-l |
grep $key >/dev
/null || ssh-add
$key
42 "vm1") _ssh_add
$HOME/.ssh
/id_rsa_kratos
;;
43 "phobos") _ssh_add
$HOME/.ssh
/id_rsa_phobos
;;
44 *) #echo "valid key not found" && exit 1 ;;
54 # "vm1:") _ssh_add $HOME/.ssh/id_rsa_kratos ;;
55 # "phobos:") _ssh_add $HOME/.ssh/id_rsa_phobos ;;
64 # push|pull|fetch) _ssh_add ;;
76 echo "nameserver 80.67.169.12" | sudo
tee /etc
/resolv.conf
81 curl
-4 "http://wttr.in/"$town
86 local foldertobezipped
=$2
88 #tar -cvf - file1 file2 dir3 | gzip > archive.tar.gz
93 local foldertobezipped
=$2
98 # zip some files using python
102 # call the zip module from python3
103 python3
-m zipfile
-c $2 $1
106 # to unban an ip blocked because of fail ssh connection
107 my_fail2ban_unban_ip
(){
109 sudo fail2ban-client
set ssh unbanip
$ip
113 my_bak
() { cp "$1" "$1_`date +%Y-%m-%d_%H-%M-%S`" ; }
116 # return the service on port
121 my_mount_set_of_partition
(){
122 # Mount set of partitions for recovery.
124 mkdir
/media
/sdd
$i && mount
-o ro
/{dev
,media
}/sdd
$i ||
rmdir /media
/sdd
$i ;
128 my_copy_file_to_clipboard
() {
130 [ -f $file ] && xclip
-sel clip
< $filecontent
136 usage: $PROGNAME options
138 Program deletes files from filesystems to release space.
139 It gets config file that define fileystem paths to work on, and whitelist rules to
143 -c --config configuration file containing the rules. use --help-config to see the syntax.
144 -n --pretend do not really delete, just how what you are going to do.
145 -t --test run unit test to check the program
146 -v --verbose Verbose. You can specify more then one -v to have more verbose
148 -h --help show this help
149 --help-config configuration help
157 $PROGNAME --test test_string.sh
160 $PROGNAME --config /path/to/config/$PROGNAME.conf
162 Just show what you are going to do:
163 $PROGNAME -vn -c /path/to/config/$PROGNAME.conf
168 my_change_owner_of_files
() {
170 local group
=$1; shift
176 chown
$user:$group $i
181 my_template_function
() {
183 local PROGNAME
=$
(basename $0)
184 local PROGDIR
=$
(readlink
-m $
(dirname $0))
189 #readonly local second_arg=$2;
191 # if no args, print usage
192 is_empty_string
$ARGS && my_usage
203 local env
=$HOME"/envs/"$dir"/source/"
204 #if source dir not exists, create & cd
205 [ -d $env ] && cd $env || mkdircd
-p $env
206 # activate virtualenv
207 source $HOME"/envs/"$dir"/bin/activate"
210 # Taken from http://aaroncrane.co.uk/2009/03/git_branch_prompt/
213 until [ "$dir" -ef / ]; do
214 if [ -f "$dir/.git/HEAD" ]; then
215 GIT_REPO
=`readlink -e $dir`/
225 head=$
(< "$1/.git/HEAD")
226 if [[ $head == ref
:\ refs
/heads
/* ]]; then
227 GIT_BRANCH
=${head#*/*/}
228 elif [[ $head != '' ]]; then
229 GIT_BRANCH
='(detached)'
231 GIT_BRANCH
='(unknown)'
235 # Taken from https://github.com/jimeh/git-aware-prompt
237 local status
=$
(git status
--porcelain 2> /dev
/null
)
238 if [[ "$status" != "" ]]; then
246 if [ -e "$1/.git/refs/stash" ]; then
253 cdls
() { cd "$@" && clear && ls; }
270 mkdir
-p "$@" && eval cd "\"\$$#\"";
274 local serverdir
="${TMPDIR:-/tmp}/emacs${UID}"
276 for file in ${serverdir}/*; do
277 if [[ -S ${file} ]]; then
278 servers
+=("${file##*/}")
302 printf '%s ' "$result";
309 echo "[$(date + '%Y-%m-%dT%H:%M:%S%z')]: $@ >&2"
318 my_err
"unable to my_test"
319 exit "${E_DID_NOTHING}"
323 my_pretty_print_json
() {
327 my_not_start_service_on_boot
(){
328 # service to not start on boot
330 update-rc.d
$service disable
;
333 my_install_new_font
(){
340 sudo ip link
set eth0 up
;
341 # ask for an ip via dhcp
345 #function to show the current branch
346 my_show_current_branch
(){
347 git rev-parse
--abbrev-ref HEAD
353 echo $
(pdftk
$filename dump_data \
354 |
grep -i numberofpages \
360 echo $
(pdftk
$filename burst
);
364 for i
in $
(ls |
grep .pdf$
); do
369 # function to show one line of file
371 local line_number
=$1;
375 echo $
(sed -n $line_number$p $filename);
378 # function to extract in one command
382 if is_file
$filename ; then
384 *.
tar.xz
) tar xvfJ
$filename ;;
385 *.xz
) tar xf
$filename ;;
386 *.
tar.bz2
) tar xvjf
$filename ;;
387 *.
tar.gz
) tar xvzf
$filename ;;
388 *.bz2
) bunzip2
$filename ;;
389 *.rar
) unrar x
$filename ;;
390 *.gz
) gunzip
$filename ;;
391 *.
tar) tar xvf
$filename ;;
392 *.tbz2
) tar xvjf
$filename ;;
393 *.tgz
) tar xvzf
$filename ;;
394 *.
zip) unzip $filename ;;
395 *.Z
) uncompress $filename ;;
396 *.7z
) 7z x
$filename ;;
397 *) echo "'$filename' cannot be extracted via extract" ;;
400 echo "'$filename' is not a valid file"
404 # function to list the content of an archive
405 my_list_archive_content
() {
408 if is_file
$filename ; then
410 *.
tar.xz
) tar tvfJ
$filename ;;
411 *.
tar.bz2
) tar tvjf
$filename ;;
412 *.
tar.gz
) tar tzvf
$filename ;;
413 *.bz2
) bunzip2
$filename ;;
414 *.gz
) gunzip
-l $filename ;;
415 *.
tar) tar tvf
$filename ;;
416 *.tbz2
) tar tvjf
$filename ;;
417 *.tgz
) tar tvzf
$filename ;;
418 *.
zip) unzip -l $filename ;;
419 *.Z
) uncompress -l $filename ;;
420 *.7z
) 7z l
$filename ;;
421 *) echo "content of '$filename' cannot be listed :(" ;;
424 echo "'$filename' is not a valid file"
428 # dump a mysql database
432 echo "dumping database '$database' ..."
433 mysqldump
-u patrick
-ppatrick $database > $databse_dump_$
(date +%m
%d
%Y
).sql
437 my_dump_mysql_table
() {
441 echo "dumping table '$table' from database '$database' ..."
442 mysqldump
-u patrick
-ppatrick $database $table > $table_dump_$
(date +%m
%d
%Y
).sql
451 echo "upload of '$filename' to '$ftp_url' by '$user' ..."
452 #curl -T $filename ftp://dev.africafilms.tv/movies/ --user dev.africafilms.tv:dev/aftv/DAK2602
453 curl
-T $filename $ftp_url --user $user:$password
457 local string_to_find
=$1;
460 echo "recursive looking for the string '$string' inside '$directory' directory"
461 grep -l -R -i -r $string_to_find $directory;
465 local string_to_find
=$1;
468 echo "recursive looking for the string '$string' inside '$directory' directory"
469 find $directory |
xargs grep $string -sl;
472 # extract md5 hash from a string
476 echo "md5 hash of '$string' is: "
477 echo -n "$string" |
md5sum;
480 # Creates an archive (*.tar.gz) from given directory.
484 tar cvzf
"${directory%%/}.tar.gz" "${directory%%/}/";
487 # Create a ZIP archive of a file or folder.
491 zip -r "${file%%/}.zip" "$file";
494 # Make your directories and files access rights sane.
495 my_sanitize
() { chmod -R u
=rwX
,g
=rX
,o
= "$@" ;}
497 # Get IP adress on ethernet.
499 local my_ip
=$
( /sbin
/ifconfig eth0 \
500 |
awk '/inet/ { print $2 } ' \
502 local my_gtw
="gtw: "$
(netstat
-nr |
awk '{print $2}' |
sed -n 3p
);
503 echo ${my_ip:-"Not connected"}
504 echo ${my_gtw:-"Not connected"}
507 # Get current host related info.
509 echo -e "\nYou are logged on ${BRed}$HOST"
510 echo -e "\n${BRed}Local IP Address :$NC" ; my_ip
511 echo -e "\n${BRed}Current date :$NC " ; date; calendar |
head -1
512 echo -e "\n${BRed}Additionnal information:$NC " ; uname
-a
513 echo -e "\n${BRed}Users logged on:$NC " ; \
518 echo -e "\n${BRed}Machine stats :$NC " ; uptime
519 echo -e "\n${BRed}Memory stats :$NC " ; free
-h
520 echo -e "\n${BRed}Diskspace :$NC " ; mydf
/ $HOME
521 echo -e "\n${BRed}Open connections :$NC "; netstat
-pan --inet;
525 # Pretty-print of 'df' output. Inspired by 'dfc' utility.
533 echo -e $fs" :No such file or directory" ; continue
536 local info
=( $
(command df
-P $fs |
awk 'END{ print $2,$3,$5 }') )
537 local free
=( $
(command df
-Pkh $fs |
awk 'END{ print $4 }') )
538 local nbstars
=$
(( 20 * ${info[1]} / ${info[0]} ))
540 for ((j
=0;j
<20;j
++)); do
541 if [ ${j} -lt ${nbstars} ]; then
547 out
=${info[2]}" "$out"] ("$free" free on "$fs")"
553 # debootstrap --verbose \
554 # --variant=minbase \
555 # --arch=i386 --include ifupdown,locales,libui-dialog-perl,dialog,dhcp3-client,netbase,net-tools,iproute,openssh-server \
556 # sid /var/lib/lxc/sid http://ftp.debian.org/debian
562 local rememberfile
="$HOME/.remember"
563 # if the file not exists, touch it
564 [[ ! -f $rememberfile ]] && touch $rememberfile
565 if [ $# -eq 0 ] ; then
566 echo "Type what you want to remember then, hit ^D: "
567 cat - >> $rememberfile;
569 #echo $(date +%m%d%Y)" -- $@" >> $rememberfile
570 echo $
(date +%F
)" -- $@" >> $rememberfile
575 local rememberfile
="$HOME/.remember"
577 #is_not_file $remberfile && echo 'nothing to do' ||
578 #is_empty $rememberfile && echo 'nothing to do' ||
580 is_not_file
$rememberfile ||
[[ $
(cat $rememberfile |
wc -l) = 0 ]] && echo "nothing to do" || my_display_todo
"$@";
585 local rememberfile
="$HOME/.remember"
587 #[[ $# -eq 0 ]] && more $rememberfile || grep -i "$@" $rememberfile | more;
588 [[ $# -eq 0 ]] && is_file
$rememberfile && is_not_empty_file
$rememberfile && cat $rememberfile ||
grep -i "$@" $rememberfile;
593 #lsbytesum - sum the number of bytes in a directory listing
595 for Bytes
in $
(ls -l |
grep "^-" |
awk '{ print $5 }')
597 let TotalBytes
=$TotalBytes+$Bytes
599 TotalMeg
=$
(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" |
bc)
606 declare -a $listname;
611 sensonet_create_event
() {
612 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"
615 sensonet_update_event
() {
616 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"
620 [[ $# -eq 0 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/events
/ || \
621 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/events
/"$1"
624 sensonet_del_event
() {
625 curl
-i -X "DELETE" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/events
/"$1"
628 sensonet_create_sensor
() {
629 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"
632 sensonet_update_sensor
() {
633 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"
636 sensonet_del_sensor
() {
637 curl
-i -X "DELETE" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/sensors
/"$1"
641 [[ $# -eq 0 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/sensors
/ || \
642 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/sensors
/"$1"
645 sensonet_create_network
(){
646 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"
649 sensonet_update_network
(){
650 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"
653 sensonet_del_network
(){
654 curl
-i -X "DELETE" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$1"
658 [[ $# -eq 0 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/ || \
659 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$1"
662 sensonet_network_probes
() {
663 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$1"/probes
666 sensonet_create_probe
() {
667 curl
-i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" \
668 -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}"
671 sensonet_update_probe
() {
672 curl
-i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" \
673 -d "name=${1}&latitude=48.0&longitude=-1.0&monitorMe=1&sleepingTime=0" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${2}"
677 [[ $# -eq 0 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/ || \
678 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"
681 sensonet_del_probe
() {
682 curl
-i -X "DELETE" -k -H -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${2}"
685 sensonet_probe_neighborhood
(){
686 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"/neighborhood
689 sensonet_probe_set_sleeping_time
(){
690 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
693 sensonet_probe_sensors
(){
694 [[ $# -eq 1 ]] && curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"/sensors || \
695 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"/sensors
/"${2}"
698 sensonet_probe_sensor_values
(){
699 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/probes
/%2B221
"${1}"/sensors
/"${2}"/values
703 # Show top 10 history command on screen
705 history |
awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' |
sort -rn |
head
708 # Wrapper for host and ping command
709 # Accept http:// or https:// or ftps:// names for domain and hostnames
710 _getdomainnameonly
(){
713 # remove protocol part of hostname
720 # remove username and/or username:password part of hostname
723 # remove all /foo/xyz.html*
725 # show domain name only
731 local array
=( $@
) # get all args in an array
732 local len
=${#array[@]} # find the length of an array
733 local host=${array[$len-1]} # get the last arg
734 local args
=${array[@]:0:$len-1} # get all args before the last arg in $@ in an array
735 local _ping
="/bin/ping"
736 local c
=$
(_getdomainnameonly
"$host")
737 [ "$t" != "$c" ] && echo "Sending ICMP ECHO_REQUEST to \"$c\"..."
744 local len
=${#array[@]}
745 local host=${array[$len-1]}
746 local args
=${array[@]:0:$len-1}
747 local _host
="/usr/bin/host"
748 local c
=$
(_getdomainnameonly
"$host")
749 [ "$t" != "$c" ] && echo "Performing DNS lookups for \"$c\"..."