10_alias.zsh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. local version_next=""
  158. if [[ "${1}" = "" ]]; then
  159. local version_current="$(git lasttag 2>/dev/null)"
  160. if [[ "${version_current}" != "" ]]; then
  161. local version_minor="${version_current##*.}"
  162. local version_major="${version_current%.*}"
  163. local version_next="${version_major}.$((version_minor+1))"
  164. fi
  165. else
  166. version_next="${1}"
  167. fi
  168. if [[ "${version_next}" != "" ]]; then
  169. export GIT_MERGE_AUTOEDIT=no
  170. git flow release start "${version_next}" && \
  171. GIT_MERGE_AUTOEDIT=no git flow release finish -m "${version_next}" "${version_next}"
  172. else
  173. printf "%s: tag is missing\n" "${0}" >&2
  174. fi
  175. }
  176. alias gfr="git-flow-release"
  177. #
  178. # Mssh stuff
  179. #
  180. if [ -f ~/.ssh/config ]; then
  181. hosts=($(grep '^Host [^*]' ~/.ssh/config | sed 's/Host //'))
  182. compctl -k hosts mssh
  183. fi
  184. #
  185. # Hosting stuff
  186. #
  187. http_auth_ssha() {
  188. read "username?username: "
  189. read "password?password: "
  190. salt="$(openssl rand -base64 3)"
  191. sha1="$(printf "%s%s" "${password}" "${salt}" | openssl dgst -binary -sha1 | sed 's#$#'"${salt}"'#' | base64)"
  192. printf "%s:{SSHA}%s\n" "${username}" "${sha1}"
  193. }
  194. #
  195. # DNS stuff
  196. #
  197. myip () {
  198. # Get public IP address from opendns
  199. ipv4=
  200. ipv6=
  201. ip4_resolver="resolver1.opendns.com"
  202. ip6_resolver="2620:0:ccc::2"
  203. if (( $+commands[dig] )); then
  204. ipv4=$(dig +short myip.opendns.com "@${ip4_resolver}")
  205. ipv6=$(dig AAAA +short myip.opendns.com "@${ip6_resolver}")
  206. elif (( $+commands[host] )); then
  207. ipv4=$(LANG=C host -t A myip.opendns.com "${ip4_resolver}" | sed -r -n 's/^.* has address (.*)$/\1/p')
  208. ipv6=$(LANG=C host -t AAAA myip.opendns.com "${ip6_resolver}" | sed -r -n 's/^.* has IPv6 address (.*)$/\1/p')
  209. fi
  210. if [[ -n "${ipv4}" ]]; then
  211. printf "IPv4 is %s\n" "${ipv4}"
  212. else
  213. printf "IPv4 not found\n" >&2
  214. fi
  215. if [[ -n "${ipv6}" ]]; then
  216. printf "IPv6 is %s\n" "${ipv6}"
  217. else
  218. printf "IPv6 not found\n" >&2
  219. fi
  220. }