0. 概述

在 Linux 下,shell 作为一个强大的工具,很多时候并不是那么容易驯服的。有一些很常用的用法我经常会忘记,所以在这篇 Solution 里面,我就记录了一些我觉得很有用,也一直在用的 Shell Snippets。

1. copy ssh id 到远端机器

[[email protected]]# cat ~/.bash
function sssh () {
  /usr/bin/ssh-copy-id -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" "$@"; ssh "$@"
}

2. 移除 SSH host key

[[email protected]]# cat ~/.bash
function rmh() {
  ssh-keygen -R 192.168."$@"
}

3. 查询 docker tags

[[email protected]]# cat ~/.bash
function dockertags() {
  wget -q https://registry.hub.docker.com/v1/repositories/"$@"/tags -O -  | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n'  | awk -F: '{print $3}'
}

4. SSH 自动输入密码

[[email protected]]# cat ~/.bash
function pssh () {
  sshpass -p 'abc123' /usr/bin/ssh -o "StrictHostKeyChecking no"  -o "UserKnownHostsFile /dev/null" [email protected]."$@"
  [ $? -eq 0 ] && return
    sshpass -p 'abcd1234' /usr/bin/ssh -o "StrictHostKeyChecking no"  -o "UserKnownHostsFile /dev/null" [email protected]."$@";
  [ $? -eq 0 ] && return
    sshpass -p '!QAZ2wsx' /usr/bin/ssh -o "StrictHostKeyChecking no"  -o "UserKnownHostsFile /dev/null" [email protected]."$@";
  [ $? -eq 0 ] && return
}

5. 添加 Kubectl 自动填充

[[email protected]]# cat ~/.bashrc
if [ $commands[kubectl] ]; then
    source <(kubectl completion zsh)
fi

6. 添加 Helm 自动填充

[[email protected]]# cat ~/.bash
if [ $commands[helm] ]; then
    source <(helm completion zsh)
fi

7. 自动设置 http_proxy

[[email protected]]# cat ~/.http_proxy
export http_proxy="http://127.0.0.1:8118"
export https_proxy=$http_proxy
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$http_proxy

8. 取消设置 http_proxy

[[email protected]]# cat ~/.unset_http_proxy
unset http_proxy
unset https_proxy
unset HTTP_PROXY
unset HTTPS_PROXY

9. 命令行 copy(pbcopy/pbpaste)

[[email protected]]# alias | grep clipboard
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'