10_alias.zsh 6.0 KB

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