Update fzf location
[dotfiles] / .bash_function
1 #!/bin/sh
2 # my weird function file
3 # lots of this coming from the internets
4
5 my_test_gpg_passphrase(){
6 local keyid=$1
7 echo "1234" | gpg --no-use-agent -o /dev/null --local-user $keyid -as - && echo "The correct passphrase was entered for this key"
8 }
9
10 my_tmw() {
11 tmux split-window -dh "$*";
12 }
13
14 my_sec2dhms() {
15 declare -i SS="$1"
16 D=$(( SS / 86400 ))
17 H=$(( SS % 86400 / 3600 ))
18 M=$(( SS % 3600 / 60 ))
19 S=$(( SS % 60 ))
20 [ "$D" -gt 0 ] && echo -n "${D}:"
21 [ "$H" -gt 0 ] && printf "%02g:" "$H"
22 printf "%02g:%02g\n" "$M" "$S"
23 }
24
25 my_nth_line(){
26 local nth=$1
27 local p='p'
28 #sed -n $nth$p
29 awk 'NR=='$nth' {print;exit}'
30 }
31
32 my_backup_current_folder(){
33 zip -r ${PWD##*/}-backup-$(date +%Y-%m-%d) $(pwd)
34 }
35
36 my_hyphenize_filenames(){
37 find -name "* *" -type f | rename 's/ /-/g'
38 }
39
40 my_edit_modified_files(){
41 $EDITOR $( (git ls-files -m -o --exclude-standard; git diff --cached --name-only) | sort | uniq)
42 }
43 my_edit_files_with_conflicts(){
44 $EDITOR $(git diff --name-only --diff-filter=U)
45 }
46 my_edit_recently_committed(){
47 $EDITOR $(git show --name-only --oneline | egrep -v "^[a-z0-9]+ ")
48 }
49 # alias em=edit_modified_files
50 # alias ec=edit_files_with_conflicts
51 # alias er=edit_recently_committed
52
53 my_create_htaccess_pwd(){
54 local password=$1
55 openssl passwd -crypt $password
56 }
57
58 my_find_file_size(){
59 #echo "$(wc -c file.ext | awk '{print $1}') / 1024" | bc
60 local file=$1
61 wc -c $1
62 stat -c%s $1
63 }
64
65 # apt install minicom
66 # apt install setserial
67 # my_find_serial_ports1(){
68 # sudo setserial -g /dev/ttyS[0123]
69 # }
70
71 my_find_serial_ports(){
72 sudo dmesg | grep tty
73 }
74
75 # my_tail_500(){
76 # # test if given argument
77 # if [ $# -ne 1 ]; then
78 # # TODO: print usage?
79 # local default="/var/log/nginx/access.log"
80 # tail -f $default | awk '$9 == 500 { print $0 }'
81 # else
82 # local log=$1
83 # tail -f $log | awk '$9 == 500 { print $0 }'
84 # fi
85 # }
86
87
88 # _ssh_agent() {
89 # command -v ssh-agent >/dev/null || return
90 # [ "$SSH_CONNECTION" ] && return
91 # local info=$HOME/.cache/ssh-agent-info
92 # [ -f $info ] && . $info >/dev/null
93 # [ "$SSH_AGENT_PID" ] && kill -0 $SSH_AGENT_PID 2>/dev/null || {
94 # mkdir -p $(dirname $info)
95 # ssh-agent >$info
96 # . $info >/dev/null
97 # }
98 # } _ssh_agent
99
100
101 _ssh_add() {
102 local key=$1
103
104 [ "$SSH_CONNECTION" ] && return
105 ssh-add -l | grep $key >/dev/null || ssh-add $key
106 }
107
108 ssh() {
109 local remote=$1
110
111 case $remote in
112 "vm1") _ssh_add $HOME/.ssh/id_rsa_kratos ;;
113 "phobos") _ssh_add $HOME/.ssh/id_rsa_phobos ;;
114 "ansible") _ssh_add $HOME/.ssh/id_rsa_ansible ;;
115 *) #echo "valid key not found" && exit 1 ;;
116 esac
117 command ssh "$@"
118 }
119
120 # scp() {
121 # local file=$1
122 # local remote=$2
123
124 # case $remote in
125 # "vm1:") _ssh_add $HOME/.ssh/id_rsa_kratos ;;
126 # "phobos:") _ssh_add $HOME/.ssh/id_rsa_phobos ;;
127 # esac
128
129 # command scp "$@"
130 # }
131
132
133 # git() {
134 # case $1 in
135 # push|pull|fetch) _ssh_add ;;
136 # esac
137 # command git "$@"
138 # }
139
140 my_reboot_br0(){
141 sudo ifdown br0
142 my_up
143 }
144
145 my_up(){
146 sudo ifup br0 && sudo resolvconf -u
147 # ifup br0 &&
148 # echo "nameserver 31.14.133.188" | sudo tee -a /etc/resolv.conf # (ns5.it) -- 99.30% uptime
149 # echo "nameserver 87.98.242.252" | sudo tee -a /etc/resolv.conf # (ns11.fr) -- 99.58% uptime
150 # echo "nameserver 87.98.175.85" | sudo tee -a /etc/resolv.conf # (ns10.fr) -- 99.61% uptime
151 # echo "nameserver 193.183.98.154" | sudo tee -a /etc/resolv.conf #(ns1.it) -- 99.84% uptime
152 # echo "nameserver 80.67.169.12" | sudo tee -a /etc/resolv.conf
153 }
154
155 my_weather(){
156 local town=$1
157 curl -4 "http://wttr.in/"$town
158 }
159
160 my_dotargz(){
161 local tarfile=$1
162 local foldertobezipped=$2
163 tar -zcvf $1 $2
164 #tar -cvf - file1 file2 dir3 | gzip > archive.tar.gz
165 }
166
167 my_dotarbz2(){
168 local tarfile=$1
169 local foldertobezipped=$2
170 tar -jcvf $1 $2
171
172 }
173
174 # zip some files using python
175 my_zipit(){
176 local zipfile=$2
177 local tobezipped=$1
178 # call the zip module from python3
179 python3 -m zipfile -c $2 $1
180 }
181
182 # to unban an ip blocked because of fail ssh connection
183 my_fail2ban_unban_ip(){
184 local ip=$1
185 sudo fail2ban-client set ssh unbanip $ip
186
187 }
188
189 my_bak() { cp "$1" "$1_`date +%Y-%m-%d_%H-%M-%S`" ; }
190
191 my_whois_onport(){
192 # return the service on port
193 local port=$1
194 sudo lsof -i :$port
195 }
196
197 my_mount_set_of_partition(){
198 # Mount set of partitions for recovery.
199 for i in {1..12}; do
200 mkdir /media/sdd$i && mount -o ro /{dev,media}/sdd$i || rmdir /media/sdd$i ;
201 done
202 }
203
204 my_copy_file_to_clipboard() {
205 local filecontent=$1
206 [ -f $file ] && xclip -sel clip < $filecontent
207 }
208
209 my_usage() {
210
211 cat <<-EOF
212 usage: $PROGNAME options
213
214 Program deletes files from filesystems to release space.
215 It gets config file that define fileystem paths to work on, and whitelist rules to
216 keep certain files.
217
218 OPTIONS:
219 -c --config configuration file containing the rules. use --help-config to see the syntax.
220 -n --pretend do not really delete, just how what you are going to do.
221 -t --test run unit test to check the program
222 -v --verbose Verbose. You can specify more then one -v to have more verbose
223 -x --debug debug
224 -h --help show this help
225 --help-config configuration help
226
227
228 Examples:
229 Run all tests:
230 $PROGNAME --test all
231
232 Run specific test:
233 $PROGNAME --test test_string.sh
234
235 Run:
236 $PROGNAME --config /path/to/config/$PROGNAME.conf
237
238 Just show what you are going to do:
239 $PROGNAME -vn -c /path/to/config/$PROGNAME.conf
240
241 EOF
242 }
243
244 my_change_owner_of_files() {
245 local user=$1; shift
246 local group=$1; shift
247 local files=$@
248 local i
249
250 for i in $files
251 do
252 chown $user:$group $i
253 done
254 }
255
256
257 my_template_function() {
258 # local vars
259 local PROGNAME=$(basename $0)
260 local PROGDIR=$(readlink -m $(dirname $0))
261 local ARGS="$@"
262 local first_arg=$1
263
264 # readonly var
265 #readonly local second_arg=$2;
266
267 # if no args, print usage
268 is_empty_string $ARGS && my_usage
269
270
271 }
272
273 my_list_venv(){
274 ls $HOME"/envs"
275 }
276
277 my_goto_venv() {
278 local dir=$1
279 local env=$HOME"/envs/"$dir"/source/"
280 #if source dir not exists, create & cd
281 [ -d $env ] && cd $env || mkdircd -p $env
282 # activate virtualenv
283 source $HOME"/envs/"$dir"/bin/activate"
284 }
285
286 # Taken from http://aaroncrane.co.uk/2009/03/git_branch_prompt/
287 find_git_repo() {
288 local dir=.
289 until [ "$dir" -ef / ]; do
290 if [ -f "$dir/.git/HEAD" ]; then
291 GIT_REPO=`readlink -e $dir`/
292 return
293 fi
294 dir="../$dir"
295 done
296 GIT_REPO=''
297 return
298 }
299
300 find_git_branch() {
301 head=$(< "$1/.git/HEAD")
302 if [[ $head == ref:\ refs/heads/* ]]; then
303 GIT_BRANCH=${head#*/*/}
304 elif [[ $head != '' ]]; then
305 GIT_BRANCH='(detached)'
306 else
307 GIT_BRANCH='(unknown)'
308 fi
309 }
310
311 # Taken from https://github.com/jimeh/git-aware-prompt
312 find_git_dirty() {
313 local status=$(git status --porcelain 2> /dev/null)
314 if [[ "$status" != "" ]]; then
315 GIT_DIRTY='*'
316 else
317 GIT_DIRTY=''
318 fi
319 }
320
321 find_git_stash() {
322 if [ -e "$1/.git/refs/stash" ]; then
323 GIT_STASH='stash'
324 else
325 GIT_STASH=''
326 fi
327 }
328
329 cdls() { cd "$@" && clear && ls; }
330
331
332 cl(){
333 if [ -d "$1" ]; then
334 cd "$1"
335 clear
336 ls -lh
337 else
338 cd
339 clear
340 ls
341 fi
342 }
343
344
345 mkdircd(){
346 mkdir -p "$@" && eval cd "\"\$$#\"";
347 }
348
349 my_emacs_servers(){
350 local serverdir="${TMPDIR:-/tmp}/emacs${UID}"
351 local -a servers
352 for file in ${serverdir}/*; do
353 if [[ -S ${file} ]]; then
354 servers+=("${file##*/}")
355 fi
356 done
357
358 echo "${servers[@]}"
359 }
360
361 my_fib(){
362 x=${1:-1}
363 y=${2:-1}
364 echo $(($x + $y))
365 fib $y $(($x + $y))
366 }
367
368
369 my_recursiveFibo() {
370 local start=0;
371 n="$1";
372 inc="$2";
373 while ((n < $inc));
374 do
375 result=$((start+n));
376 start=$n;
377 n="$result";
378 printf '%s ' "$result";
379 done;
380 }
381
382 #recursivFibo 1 2048
383
384 my_err() {
385 echo "[$(date + '%Y-%m-%dT%H:%M:%S%z')]: $@ >&2"
386 }
387
388 my_test() {
389 echo 'test';
390 }
391
392 my_test2(){
393 if my_test; then
394 my_err "unable to my_test"
395 exit "${E_DID_NOTHING}"
396 fi
397 }
398
399 my_pretty_print_json() {
400 python -m json.tool | pygmentize -l json
401 }
402
403 my_not_start_service_on_boot(){
404 # service to not start on boot
405 local service=$1;
406 update-rc.d $service disable;
407 }
408
409 my_install_new_font(){
410 #sudo fc-cache -v -f
411 fc-cache -v -f;
412 }
413
414 my_launch_eth0(){
415 # set eth0 iface up
416 sudo ip link set eth0 up;
417 # ask for an ip via dhcp
418 sudo dhclient eth0;
419 }
420
421 #function to show the current branch
422 my_show_current_branch(){
423 git rev-parse --abbrev-ref HEAD
424 }
425
426 my_pdf_nbpages(){
427 local filename=$1;
428
429 echo $(pdftk $filename dump_data \
430 | grep -i numberofpages \
431 | awk '{print $2}');
432 }
433
434 my_pdf_explosion() {
435 local filename=$1;
436 echo $(pdftk $filename burst);
437 }
438
439 my_pdfs_to_xml() {
440 for i in $(ls | grep .pdf$); do
441 pdftohtml -xml $i;
442 done;
443 }
444
445 # function to show one line of file
446 my_xline_of_file(){
447 local line_number=$1;
448 local filename=$2;
449 local p='p';
450
451 echo $(sed -n $line_number$p $filename);
452 }
453
454 # function to extract in one command
455 my_xtract() {
456 local filename=$1;
457
458 if is_file $filename ; then
459 case $filename in
460 *.tar.xz) tar xvfJ $filename ;;
461 *.xz) tar xf $filename ;;
462 *.tar.bz2) tar xvjf $filename ;;
463 *.tar.gz) tar xvzf $filename ;;
464 *.bz2) bunzip2 $filename ;;
465 *.rar) unrar x $filename ;;
466 *.gz) gunzip $filename ;;
467 *.tar) tar xvf $filename ;;
468 *.tbz2) tar xvjf $filename ;;
469 *.tgz) tar xvzf $filename ;;
470 *.zip) unzip $filename ;;
471 *.Z) uncompress $filename ;;
472 *.7z) 7z x $filename ;;
473 *) echo "'$filename' cannot be extracted via extract" ;;
474 esac
475 else
476 echo "'$filename' is not a valid file"
477 fi
478 }
479
480 # function to list the content of an archive
481 my_list_archive_content() {
482 local filename=$1;
483
484 if is_file $filename ; then
485 case $filename in
486 *.tar.xz) tar tvfJ $filename ;;
487 *.tar.bz2) tar tvjf $filename ;;
488 *.tar.gz) tar tzvf $filename ;;
489 *.bz2) bunzip2 $filename ;;
490 *.gz) gunzip -l $filename ;;
491 *.tar) tar tvf $filename ;;
492 *.tbz2) tar tvjf $filename ;;
493 *.tgz) tar tvzf $filename ;;
494 *.zip) unzip -l $filename ;;
495 *.Z) uncompress -l $filename ;;
496 *.7z) 7z l $filename ;;
497 *) echo "content of '$filename' cannot be listed :(" ;;
498 esac
499 else
500 echo "'$filename' is not a valid file"
501 fi
502 }
503
504 # dump a mysql database
505 my_dump_mysql_db() {
506 local database=$1;
507
508 echo "dumping database '$database' ..."
509 mysqldump -u patrick -ppatrick $database > $databse_dump_$(date +%m%d%Y).sql
510
511 }
512
513 my_dump_mysql_table() {
514 local database=$1;
515 local table=$2;
516
517 echo "dumping table '$table' from database '$database' ..."
518 mysqldump -u patrick -ppatrick $database $table > $table_dump_$(date +%m%d%Y).sql
519 }
520
521 my_uploadfile() {
522 local filename=$1
523 local ftp_url=$2;
524 local user=$3;
525 local password=$4;
526
527 echo "upload of '$filename' to '$ftp_url' by '$user' ..."
528 #curl -T $filename ftp://dev.africafilms.tv/movies/ --user dev.africafilms.tv:dev/aftv/DAK2602
529 curl -T $filename $ftp_url --user $user:$password
530 }
531
532 my_find_string() {
533 local string_to_find=$1;
534 local directory=$2;
535
536 echo "recursive looking for the string '$string' inside '$directory' directory"
537 grep -l -R -i -r $string_to_find $directory;
538 }
539
540 my_find_string2() {
541 local string_to_find=$1;
542 local directory=$2;
543
544 echo "recursive looking for the string '$string' inside '$directory' directory"
545 find $directory | xargs grep $string -sl;
546
547 }
548 # extract md5 hash from a string
549 my_xmd5() {
550 local string=$1;
551
552 echo "md5 hash of '$string' is: "
553 echo -n "$string" | md5sum;
554 }
555
556 # Creates an archive (*.tar.gz) from given directory.
557 my_maketar() {
558 local directory=$1;
559
560 tar cvzf "${directory%%/}.tar.gz" "${directory%%/}/";
561 }
562
563 # Create a ZIP archive of a file or folder.
564 my_makezip() {
565 local file=$1;
566
567 zip -r "${file%%/}.zip" "$file";
568 }
569
570 # Make your directories and files access rights sane.
571 my_sanitize() { chmod -R u=rwX,g=rX,o= "$@" ;}
572
573 # Get IP adress on ethernet.
574 my_ip() {
575 local my_ip=$( /sbin/ifconfig eth0 \
576 | awk '/inet/ { print $2 } ' \
577 | sed -e s/addr://);
578 local my_gtw="gtw: "$(netstat -nr | awk '{print $2}' | sed -n 3p);
579 echo ${my_ip:-"Not connected"}
580 echo ${my_gtw:-"Not connected"}
581 }
582
583 # Get current host related info.
584 my_host_info() {
585 echo -e "\nYou are logged on ${BRed}$HOST"
586 echo -e "\n${BRed}Local IP Address :$NC" ; my_ip
587 echo -e "\n${BRed}Current date :$NC " ; date; calendar | head -1
588 echo -e "\n${BRed}Additionnal information:$NC " ; uname -a
589 echo -e "\n${BRed}Users logged on:$NC " ; \
590 w -hs \
591 | cut -d " " -f1 \
592 | sort \
593 | uniq
594 echo -e "\n${BRed}Machine stats :$NC " ; uptime
595 echo -e "\n${BRed}Memory stats :$NC " ; free -h
596 echo -e "\n${BRed}Diskspace :$NC " ; mydf / $HOME
597 echo -e "\n${BRed}Open connections :$NC "; netstat -pan --inet;
598 echo
599 }
600
601 # Pretty-print of 'df' output. Inspired by 'dfc' utility.
602 my_df() {
603 local fs=$1;
604
605 for fs ; do
606
607 if [ ! -d $fs ]
608 then
609 echo -e $fs" :No such file or directory" ; continue
610 fi
611
612 local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
613 local free=( $(command df -Pkh $fs | awk 'END{ print $4 }') )
614 local nbstars=$(( 20 * ${info[1]} / ${info[0]} ))
615 local out="["
616 for ((j=0;j<20;j++)); do
617 if [ ${j} -lt ${nbstars} ]; then
618 out=$out"*"
619 else
620 out=$out"-"
621 fi
622 done
623 out=${info[2]}" "$out"] ("$free" free on "$fs")"
624 echo -e $out
625 done
626 }
627
628 # create_vm(){
629 # debootstrap --verbose \
630 # --variant=minbase \
631 # --arch=i386 --include ifupdown,locales,libui-dialog-perl,dialog,dhcp3-client,netbase,net-tools,iproute,openssh-server \
632 # sid /var/lib/lxc/sid http://ftp.debian.org/debian
633
634 # }
635
636
637 my_add_todo(){
638 local rememberfile="$HOME/.remember"
639 # if the file not exists, touch it
640 [[ ! -f $rememberfile ]] && touch $rememberfile
641 if [ $# -eq 0 ] ; then
642 echo "Type what you want to remember then, hit ^D: "
643 cat - >> $rememberfile;
644 else
645 #echo $(date +%m%d%Y)" -- $@" >> $rememberfile
646 echo $(date +%F)" -- $@" >> $rememberfile
647 fi
648 }
649
650 my_reminder(){
651 local rememberfile="$HOME/.remember"
652
653 #is_not_file $remberfile && echo 'nothing to do' ||
654 #is_empty $rememberfile && echo 'nothing to do' ||
655 #display_todo "$@"
656 is_not_file $rememberfile || [[ $(cat $rememberfile | wc -l) = 0 ]] && echo "nothing to do" || my_display_todo "$@";
657 }
658
659 my_display_todo(){
660
661 local rememberfile="$HOME/.remember"
662 echo $#;
663 #[[ $# -eq 0 ]] && more $rememberfile || grep -i "$@" $rememberfile | more;
664 [[ $# -eq 0 ]] && is_file $rememberfile && is_not_empty_file $rememberfile && cat $rememberfile || grep -i "$@" $rememberfile;
665 }
666
667
668 my_lsbytesum() {
669 #lsbytesum - sum the number of bytes in a directory listing
670 TotalBytes=0
671 for Bytes in $(ls -l | grep "^-" | awk '{ print $5 }')
672 do
673 let TotalBytes=$TotalBytes+$Bytes
674 done
675 TotalMeg=$(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" | bc)
676 echo -n "$TotalMeg"
677 }
678
679 # list
680 my_create_list(){
681 local listname=$1;
682 declare -a $listname;
683 }
684
685 # sensonet
686
687 sensonet_create_event() {
688 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"
689 }
690
691 sensonet_update_event() {
692 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"
693 }
694
695 sensonet_events() {
696 [[ $# -eq 0 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/events/ || \
697 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/events/"$1"
698 }
699
700 sensonet_del_event() {
701 curl -i -X "DELETE" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/events/"$1"
702 }
703
704 sensonet_create_sensor() {
705 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"
706 }
707
708 sensonet_update_sensor() {
709 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"
710 }
711
712 sensonet_del_sensor() {
713 curl -i -X "DELETE" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/sensors/"$1"
714 }
715
716 sensonet_sensors() {
717 [[ $# -eq 0 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/sensors/ || \
718 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/sensors/"$1"
719 }
720
721 sensonet_create_network(){
722 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"
723 }
724
725 sensonet_update_network(){
726 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"
727 }
728
729 sensonet_del_network(){
730 curl -i -X "DELETE" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/networks/"$1"
731 }
732
733 sensonet_networks(){
734 [[ $# -eq 0 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/networks/ || \
735 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/networks/"$1"
736 }
737
738 sensonet_network_probes() {
739 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/networks/"$1"/probes
740 }
741
742 sensonet_create_probe() {
743 curl -i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" \
744 -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}"
745 }
746
747 sensonet_update_probe() {
748 curl -i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" \
749 -d "name=${1}&latitude=48.0&longitude=-1.0&monitorMe=1&sleepingTime=0" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${2}"
750 }
751
752 sensonet_probes() {
753 [[ $# -eq 0 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/ || \
754 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"
755 }
756
757 sensonet_del_probe() {
758 curl -i -X "DELETE" -k -H -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${2}"
759 }
760
761 sensonet_probe_neighborhood(){
762 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"/neighborhood
763 }
764
765 sensonet_probe_set_sleeping_time(){
766 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
767 }
768
769 sensonet_probe_sensors(){
770 [[ $# -eq 1 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"/sensors || \
771 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"/sensors/"${2}"
772 }
773
774 sensonet_probe_sensor_values(){
775 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"/sensors/"${2}"/values
776 }
777
778 # from nixcraft
779 # Show top 10 history command on screen
780 function ht {
781 history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
782 }
783
784 # Wrapper for host and ping command
785 # Accept http:// or https:// or ftps:// names for domain and hostnames
786 _getdomainnameonly(){
787 local h="$1"
788 local f="${h,,}"
789 # remove protocol part of hostname
790 f="${f#http://}"
791 f="${f#https://}"
792 f="${f#ftp://}"
793 f="${f#scp://}"
794 f="${f#scp://}"
795 f="${f#sftp://}"
796 # remove username and/or username:password part of hostname
797 f="${f#*:*@}"
798 f="${f#*@}"
799 # remove all /foo/xyz.html*
800 f=${f%%/*}
801 # show domain name only
802 echo "$f"
803 }
804
805
806 ping(){
807 local array=( $@ ) # get all args in an array
808 local len=${#array[@]} # find the length of an array
809 local host=${array[$len-1]} # get the last arg
810 local args=${array[@]:0:$len-1} # get all args before the last arg in $@ in an array
811 local _ping="/bin/ping"
812 local c=$(_getdomainnameonly "$host")
813 [ "$t" != "$c" ] && echo "Sending ICMP ECHO_REQUEST to \"$c\"..."
814 # pass args and host
815 $_ping $args $c
816 }
817
818 host(){
819 local array=( $@ )
820 local len=${#array[@]}
821 local host=${array[$len-1]}
822 local args=${array[@]:0:$len-1}
823 local _host="/usr/bin/host"
824 local c=$(_getdomainnameonly "$host")
825 [ "$t" != "$c" ] && echo "Performing DNS lookups for \"$c\"..."
826 $_host $args $c
827 }