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