10_alias.zsh 5.4 KB

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