10_alias.zsh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. 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. sed -r 's/\s+=\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. }