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