开发环境:MacBook同时连接多台生产环境机器

370 阅读2分钟

在创业公司当一名搬砖工程师时,常常要登录线上机器查看日志,一台台查看效率很低。那么如何同时查看多台生产机器的日志呢?polysh 就排上用场了,它是同时操作多台服务器的利器,但是往往是越锋利的刀越容易伤到自己。所以在使用该工具应当谨慎,小心服务器cpu飙升哦!

polysh的安装使用

安装polysh工具

选择一台线下机器,该机器能与线上机器的网络是通的,然后执行如下操作

wget http://guichaz.free.fr/polysh/files/polysh-0.4.tar.gz

pip install --user polysh-0.4.tar.gz

配置环境变量

在 .bash_profile 文件中添加配置

export PATH=$PATH:$(python -c "import site; print site.USER_BASE")/bin

source .bash_profile

如何使用工具

方式一

polysh 172.31.160.116 172.30.174.182

waiting (2/2)>这里输入自己的登录密码

方式二

  • 在家目录下新建文件 pwd-file,内容为自己的堡垒机登录密码
  • 在家目录下新建文件 host-file,内容为需要登录的服务器ip列表,每个服务器地址站一行
  • 编写一个脚本文件login.sh,并赋予执行权限,内容如下
polysh --password-file=product-ip/pwd-file --hosts-file=product-ip/host-file

免密登录线上服务器

修改本地配置文件

cd ~/.ssh/
touch ~/.ssh/config
vim ~/.ssh/config

Host *
ControlPath ~/.ssh/master-%r@%h:%p
ControlMaster auto
ControlPersist yes

编写免密登录脚本 login.sh

#!/usr/bin/expect

# 登录的堡垒机地址
set bridge_host "xxx.xxxx.com"
set username "xxx"
set password "xxx"
set timeout 30

# 发起登录请求
spawn ssh "${username}@${bridge_host}"
expect {
# 如果未登录,则输入密码(仅需要输入6位动态密码),之后跳转到目标主机
"Password:" {
    send "${password}\n"
    expect {
    "verification code:" {
     send_user "\nEnter verification code:"
  expect_user -re "(.*)\n"
  set veri_code $expect_out(1,string)
  send "${veri_code}\n"
    }
    }
 }
#匹配你的登录域名比如shifeifei@aaa.com
 "*@xxx*" {
   }
}
interact

让免密脚本可执行

chmod -R 777 login.sh
expect login.sh

vim ~/.bash_profile
# 将下面这句话替换成你自己的路径后, 拷贝到 ~/.bash_profile 里后, 保存退出
alias login=/Your Path/login.sh
  
source ~/.bash_profile