Blob


1 # collection of shell functions found in dot_files, wikis, etc
2 # around the world. Some are written by me, of course.
4 # Trivial conversion of a hex number to decimal
5 function hex2dec()
6 {
7 [ -z "$1" ] && echo "No hex number given" && return
8 if [[ ${1:0:2} != "0x" ]]; then
9 echo "Please prefix string with 0x"
10 return
11 fi
12 re='^[0-9a-fA-Fx]+$'
13 if ! [[ $1 =~ $re ]]; then
14 echo "Hex only contains 0-9a-fA-F and 0x as prefix"
15 return
16 fi
17 printf "%d\n" $1
18 }
20 # Trivial conversion of a decimal number to hex
21 function dec2hex()
22 {
23 [ -z "$1" ] && echo "No decimal number given" && return
24 re='^[0-9]+$'
25 if ! [[ $1 =~ $re ]]; then
26 echo "Decimal only contains 0-9"
27 return
28 fi
29 printf "0x%x\n" $1
30 }
32 # Generates a RSA 2048 bit keypair with DUMMY password
33 function kgenkey()
34 {
35 [ -z "$1" ] && echo "No key alias given" && return
36 keytool -genkeypair -keyalg RSA -keysize 2048 -alias "$1" \
37 -dname "CN=Unknown,OU=Unknown,O=Unknown,L=Unknown,ST=Unknown,C=Unknown" \
38 -storepass "test123" -keypass "test123" -keystore keystore.jks
39 }
41 # Generate a CSR from the keystore generated w/ the cmd above
42 function kgencsr()
43 {
44 [ -z "$1" ] && echo "No key alias given" && return
45 keytool -certreq -alias "$1" -keystore keystore.jks -file "${1}.csr" \
46 -storepass "test123" -keypass "test123"
47 }
49 function kimportcert()
50 {
51 [ -z "$1" ] && echo "No key alias given" && return
52 [ -z "$2" ] && echo "No cert file given" && return
53 keytool -import -alias "$1" -file "$2" -keystore keystore.jks \
54 -storepass "test123" -keypass "test123" -trustcacerts
55 }
57 function updatecvs()
58 {
59 local _cvsdir=/home/matthias/cvs
60 rsync -az --delete rsync://ftp.hostserver.de/cvsync/src $_cvsdir/cvs || return 1
61 cd $_cvsdir/src && cvs -q up -P
62 }
64 function cvspsdiff()
65 {
66 [ -z "$1" ] && return
67 cvsps -q -s "$1" -g | cdiff
68 }
70 function sshopen()
71 {
72 local AGPATH="$HOME"/.ssh/$(hostname).agent
74 rm -f $AGPATH
75 command ssh-agent -t 345600 | grep -v echo > $AGPATH
76 source $AGPATH
78 # Find all public keys...
79 for i in `find $HOME/.ssh/ -maxdepth 1 -name "*.pub"`; do
80 # ... and strip the .pub suffix
81 _key=`echo $i | sed -e 's/\.pub//'`
82 command ssh-add $_key
83 done
84 }
86 function psg()
87 {
88 ps aux | grep "$@"
89 }
91 function ssh()
92 {
93 source_ssh_agent
94 /usr/bin/ssh "$@"
95 }
97 function scp()
98 {
99 source_ssh_agent
100 /usr/bin/scp "$@"
103 function sshfs()
105 source_ssh_agent
106 /usr/bin/sshfs "$@"
109 function source_ssh_agent()
111 local AGPATH="$HOME"/.ssh/$(hostname).agent
113 [[ -f $AGPATH ]] && source $AGPATH
116 function mkcd()
118 [[ $1 ]] || return 0
119 [[ -d $1 ]] || mkdir -vp "$1"
120 [[ -d $1 ]] && builtin cd "$1"
123 function cget()
125 curl -OL --compressed "$@"
128 # Show infos about my external IP address
129 function showmyipaddress
131 echo "My external IPv4 : $(curl -s -4 icanhazip.com)"
132 echo "My external IPv6 : $(curl -s -6 icanhazip.com)"
133 echo "My external PTR : $(curl -s icanhazptr.com)"
136 # Show info about hostname or IP address
137 function ipif()
139 if grep -P "(([1-9]\d{0,2})\.){3}(?2)" <<< "$1"
140 then
141 curl ipinfo.io/"$1"
142 else
143 local ipadd=$(host "$1") &&
144 local ipawk=$(awk '{ print $4 }' <<< "$ipadd")
145 curl ipinfo.io/"$ipawk"
146 fi
147 echo
150 function calc()
152 echo "scale=3;$@" | bc -l
155 function cds()
157 cd "$1" && ls -la
160 function netmnt()
162 ndcli show ip $(dig +short $1) | grep "^pool:" | cut -d':' -f2 | xargs ndcli show pool
165 function whonet()
167 ndcli show ip $1 | grep "^pool:" | cut -d':' -f2 | xargs ndcli show pool
170 # http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html
172 export MARKPATH=$HOME/.marks
173 function jump {
174 cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
176 function mark {
177 mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
179 function unmark {
180 rm -i "$MARKPATH/$1"
182 function marks {
183 ls -l "$MARKPATH" | sed 's/ / /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' && echo