10_alias.zsh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #
  2. # Editor
  3. #
  4. export EDITOR="vim"
  5. #
  6. # Pager
  7. #
  8. if (( $+commands[vim] )) && [[ -f ~/.vim/plugged/vimpager/vimpager ]]; then
  9. export PAGER=~/.vim/plugged/vimpager/vimpager
  10. alias less="$PAGER"
  11. alias more="$PAGER"
  12. elif (( $+commands[less] )); then
  13. export PAGER=less
  14. alias more="$PAGER"
  15. elif (( $+commands[more] )); then
  16. export PAGER=more
  17. fi
  18. export GIT_PAGER="$PAGER"
  19. #
  20. # Common aliases / functions
  21. #
  22. if (( $+commands[exa] )); then
  23. alias ls='exa --group-directories-first'
  24. alias l='exa --long --group-directories-first --header --blocks --inode --header --group'
  25. alias lg='exa --long --grid --group-directories-first --header --blocks --inode --header --group'
  26. alias la='exa --all --long --grid --group-directories-first --header --blocks --inode --header --group'
  27. alias lt='exa --group-directories-first --tree'
  28. else
  29. export QUOTING_STYLE='literal'
  30. alias ls='ls --group-directories-first --color=auto'
  31. alias l='ls --group-directories-first -lsh --color=auto'
  32. alias la='ls --group-directories-first -lsha --color=auto'
  33. if (( $+commands[exa] )); then
  34. alias lt='tree'
  35. fi
  36. fi
  37. alias rm='rm -i'
  38. alias mv='mv -i'
  39. alias cp='cp -i'
  40. alias vlock='clear;vlock'
  41. alias q='clear;exit'
  42. alias grep="grep --color=auto"
  43. alias egrep="egrep --color=auto"
  44. alias mgrep="grep -rnisH --color=auto"
  45. alias rsu="sudo -E zsh"
  46. alias reloadsh="exec zsh"
  47. alias md="mkdir -p"
  48. mcd() { mkdir -p "$@" && cd "$@" }
  49. ffi() {
  50. find -iname "$@"
  51. }
  52. #
  53. # Log viewers aliases
  54. #
  55. if (( $+commands[ccze] )); then
  56. alias alog="tail -n 300 -F /var/log/auth.log | ccze"
  57. alias mlog="tail -n 300 -F /var/log/mail.log | ccze"
  58. alias slog="tail -n 300 -F /var/log/syslog | ccze"
  59. alias xlog="tail -n 300 -F /var/log/Xorg.0.log | ccze"
  60. else
  61. alias alog="tail -n 300 -F /var/log/auth.log"
  62. alias mlog="tail -n 300 -F /var/log/mail.log"
  63. alias slog="tail -n 300 -F /var/log/syslog"
  64. alias xlog="tail -n 300 -F /var/log/Xorg.0.log"
  65. fi
  66. # using colortail if available (freebsd)
  67. if (( $+commands[colortail] )); then
  68. alias slog="colortail -n 60 -k /usr/local/share/examples/colortail/conf.messages -f /var/log/syslog"
  69. fi
  70. #
  71. # System tools aliases
  72. #
  73. alias df='df -h'
  74. if (( $+commands[pydf] )); then
  75. alias df="pydf -h"
  76. fi
  77. function mount() {
  78. if [ $# = 0 ]; then
  79. command mount -lv | column -t
  80. else
  81. command mount $@
  82. fi
  83. }
  84. #
  85. # Sysadmin stuff
  86. #
  87. if (( $+commands[dig] )); then
  88. alias mx="dig +short MX"
  89. alias ns="dig +short NS"
  90. elif (( $+commands[host] )); then
  91. alias mx="host -t MX"
  92. alias ns="host -t NS"
  93. fi
  94. alias scp_nosec="scp -o 'StrictHostKeyChecking=no' -o 'UserKnownHostsFile=/dev/null'"
  95. alias ssh_nosec="ssh -o 'StrictHostKeyChecking=no' -o 'UserKnownHostsFile=/dev/null'"
  96. # Network
  97. port-status() {
  98. if [ $# != 2 ]; then
  99. print "Usage: $0 [IP|HOSTNAME] [PORT|SERVICE]"
  100. else
  101. local host="${1}"
  102. local service="${2}"
  103. if nc -w 5 -z "${host}" "${service}" ; then
  104. print "Port or sevice '${service}' is opened on '${host}'"
  105. else
  106. print "Port or sevice '${service}' is closed on '${host}'"
  107. fi
  108. fi
  109. }
  110. # OpenSSL
  111. for i in sha512 sha256 sha1 md5 ; do
  112. ssl-cert-$i() {
  113. openssl x509 -noout -fingerprint -$i -in $1
  114. }
  115. done
  116. ssl-crt-fp() {
  117. for i in sha512 sha256 sha1 md5; do
  118. ssl-cert-$i $1
  119. done
  120. }
  121. ssl-crt-info() {
  122. openssl x509 -noout -text -in $1
  123. }
  124. ssl-crt-info() {
  125. openssl x509 -noout -text -in $1
  126. }
  127. ssl-csr-info() {
  128. openssl req -noout -text -in $1
  129. }
  130. ssl-csr-subject() {
  131. openssl req -noout -text -in $1 | \
  132. sed -r -n 's/^\s+Subject: (.*)/\1/p' | \
  133. sed -r 's#(^|, )#/#g' |\
  134. sed -r 's/\s+=\s+/=/g'
  135. }
  136. ssl-csr-gen() {
  137. local domain="${1:-mydomain}"
  138. local keysize="${2:-4096}"
  139. local keytype="${3:-rsa}"
  140. local digest="${4:-sha256}"
  141. local subject="/C=FR/ST=IDF/L=Paris/O=${domain}/CN=${domain}/emailAddress=admin@${domain}"
  142. if [ -n "${CSR_SUBJECT}" ]; then
  143. subject="${CSR_SUBJECT}"
  144. fi
  145. printf "Generating csr/key for ${domain} with ${keytype}/${keysize} key and ${digest} digest...\n"
  146. openssl req \
  147. -subj "${subject}" \
  148. -nodes \
  149. -${digest} \
  150. -newkey ${keytype}:${keysize} \
  151. -keyout ${domain}.key \
  152. -out ${domain}.csr
  153. ssl-csr-info ${domain}.csr
  154. }
  155. ssl-csr-gen-from() {
  156. local oldcsr="${1}"
  157. local domain="${2}"
  158. local subject="$(ssl-csr-subject "${oldcsr}")"
  159. CSR_SUBJECT="$(ssl-csr-subject "${oldcsr}")" ssl-csr-gen "${domain}"
  160. }
  161. #
  162. # Devel tools aliases
  163. #
  164. if (( $+commands[colormake] )); then
  165. alias make="colormake"
  166. fi
  167. #
  168. # Git Flow stuff
  169. #
  170. git-flow-release() {
  171. local version_next=""
  172. if [[ "${1}" = "" ]]; then
  173. local version_current="$(git lasttag 2>/dev/null)"
  174. if [[ "${version_current}" != "" ]]; then
  175. local version_minor="${version_current##*.}"
  176. local version_major="${version_current%.*}"
  177. local version_next="${version_major}.$((version_minor+1))"
  178. fi
  179. else
  180. version_next="${1}"
  181. fi
  182. if [[ "${version_next}" != "" ]]; then
  183. export GIT_MERGE_AUTOEDIT=no
  184. git flow release start "${version_next}" && \
  185. GIT_MERGE_AUTOEDIT=no git flow release finish -m "${version_next}" "${version_next}"
  186. else
  187. printf "%s: tag is missing\n" "${0}" >&2
  188. fi
  189. }
  190. alias gfr="git-flow-release"
  191. #
  192. # Mssh stuff
  193. #
  194. if [ -f ~/.ssh/config ]; then
  195. hosts=($(grep '^Host [^*]' ~/.ssh/config | sed 's/Host //'))
  196. compctl -k hosts mssh
  197. fi
  198. #
  199. # Hosting stuff
  200. #
  201. http_auth_ssha() {
  202. read "username?username: "
  203. read "password?password: "
  204. salt="$(openssl rand -base64 3)"
  205. sha1="$(printf "%s%s" "${password}" "${salt}" | openssl dgst -binary -sha1 | sed 's#$#'"${salt}"'#' | base64)"
  206. printf "%s:{SSHA}%s\n" "${username}" "${sha1}"
  207. }
  208. #
  209. # DNS stuff
  210. #
  211. myip () {
  212. # Get public IP address from opendns
  213. ipv4=
  214. ipv6=
  215. ip4_resolver="resolver1.opendns.com"
  216. ip6_resolver="2620:0:ccc::2"
  217. if (( $+commands[dig] )); then
  218. ipv4=$(dig +short myip.opendns.com "@${ip4_resolver}")
  219. ipv6=$(dig AAAA +short myip.opendns.com "@${ip6_resolver}")
  220. elif (( $+commands[host] )); then
  221. ipv4=$(LANG=C host -t A myip.opendns.com "${ip4_resolver}" | sed -r -n 's/^.* has address (.*)$/\1/p')
  222. ipv6=$(LANG=C host -t AAAA myip.opendns.com "${ip6_resolver}" | sed -r -n 's/^.* has IPv6 address (.*)$/\1/p')
  223. fi
  224. if [[ -n "${ipv4}" ]]; then
  225. printf "IPv4 is %s\n" "${ipv4}"
  226. else
  227. printf "IPv4 not found\n" >&2
  228. fi
  229. if [[ -n "${ipv6}" ]]; then
  230. printf "IPv6 is %s\n" "${ipv6}"
  231. else
  232. printf "IPv6 not found\n" >&2
  233. fi
  234. }