Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

管理需要 ssh 的机器 #15

Open
bittenApple opened this issue Apr 21, 2016 · 0 comments
Open

管理需要 ssh 的机器 #15

bittenApple opened this issue Apr 21, 2016 · 0 comments

Comments

@bittenApple
Copy link
Collaborator

bittenApple commented Apr 21, 2016

平时开发中我们难免有多台远程主机需要登录,自己的云主机,公司的 production,staging,testing 机器,找个文件记下各个主机名这种方式实在太 low 了,下面介绍个可以迅速提高幸福感的小技巧。

写个简单 bash 脚本(也可以通过配置 .ssh/config 来实现同样的效果,但是由于我们可能还会登录 mysql,redis,或者使用 telnet 连接 memcache,所以写个 bash 脚本一起维护了也不错),下面以我们平时开发都会用到的机器为例。

#!/bin/bash
case $1 in
    production)
    ssh production-ip ;;
    staging)
    ssh staging-ip ;;
    testing)
    ssh testing-ip ;;
    vagrant)
    ssh vagrant-ip ;;
    db)
    mysql -h host -uroot -ppassword ;;
    redis)
    redis-cli -h host ;;
esac

代码写了句简单的 shell swich,判断脚本的第一个参数然后执行相应的登录操作。
拷贝上面内容到文件 ~/goto ,然后执行 chmod +x ~/goto,这样就可以通过命令来管理需要登录的机器了。
下面默认你使用 zsh(使用 shell 也可以达到同样效果,麻烦一些,这里不说明了)
拷贝下面内容到~/.zshrc

alias goto="~/goto"
_goto_completion () {
    compadd `\grep ")" ~/goto | cut -f 1 -d ")"`
}
compdef _goto_completion goto

复制完记得 source ~/.zshrc,好了,现在你可以在任意一个目录下直接使用 goto 命令,并且还可以用 tab 进行补全。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant