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