Polysh 简介
Polysh 是一个交互式命令,可以在一台服务器上批量的对一批服务器进行处理,运行交互式命令。官网介绍如下:
Polysh可以同时登录多个远程主机执行输入命令,并在一个控制台界面中回显命令执行结果,适用于小批量主机同时排查问题和定位,很方便。机器数量多建议使用Pssh工具。
Polysh的主页,源码地址:
git clone git://repo.or.cz/polysh.git
Polysh 安装
Polysh下载安装:
# 下载
wget https://files.pythonhosted.org/packages/f2/d3/369bff2d3577610875310c86a795e1c55f57ae30be0de4dea14d46fc8b79/polysh-0.4.zip
# 解压安装
unzip polysh-0.4.zip
cd polysh-0.4
python setup.py install --user # 指定目录可使用 --home=~
修改polysh代码 (默认使用ssh,如改成使用xssh):
vim ~/.local/lib/python2.7/site-packages/polysh/main.py
# 在64行 处,注释掉,新增一行
#def_ssh = 'exec ssh -oLogLevel=Quiet -t %(host)s exec bash --noprofile'
def_ssh = 'exec xssh -oLogLevel=Quiet -t %(host)s exec bash'
创建到~/bin下的软链:
ln -s /home/$USER/.local/bin/polysh /home/$USER/bin/
Polysh 简介
Options:
可以直接使用命令,也可以将常用命令写入环境变量中:
# 命令行的方式
$ polysh 'machine-<01-14>'
waiting (5/14)>
This prompts means we are still waiting for 5 out of 14 remote shells.
ready (14)>
Now this prompts indicates that our 14 remote shells are ready to accept commands.
ready (14)> date
machine-01 : Mon Nov 22 21:26:05 CET 2010
machine-03 : Mon Nov 22 21:26:05 CET 2010
machine-06 : Mon Nov 22 21:26:05 CET 2010
machine-07 : Mon Nov 22 21:26:05 CET 2010
machine-10 : Mon Nov 22 21:26:05 CET 2010
machine-11 : Mon Nov 22 21:26:05 CET 2010
machine-12 : Mon Nov 22 21:26:05 CET 2010
machine-09 : Mon Nov 22 21:26:05 CET 2010
machine-13 : Mon Nov 22 21:26:05 CET 2010
machine-14 : Mon Nov 22 21:26:05 CET 2010
machine-02 : Mon Nov 22 21:26:05 CET 2010
machine-04 : Mon Nov 22 21:26:05 CET 2010
machine-05 : Mon Nov 22 21:26:05 CET 2010
machine-08 : Mon Nov 22 21:26:05 CET 2010
ready (14)>
# polysh 'machine-<0-3>.ys' 'machine-<7-10>.ys'
# 文件的方式
polysh --hosts-file 'file_name'
# 写入.bash_profile
alias repair='polysh 10.xxx --command="sh /home/anzhihe/op/cmdb.sh"'
alias cmdb='polysh 10.10.xx 10.11.xx'
bj-chunk() {
local host_master=`polysh xxx.11 --command='/home/anzhihe/MasterCmd host_master | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}" | grep -v 127.0.0.1'|awk '{ print $3 }'`
case "$@" in
"status")
polysh $host_master --command="/home/anzhihe/MasterCmd show_copy_tasks"
;;
"stop")
polysh $host_master --command="/home/anzhihe/MasterCmd stop_copying_tasks"
;;
"master")
echo $host_master
;;
*)
if [[ "$1" == cmd ]]; then
polysh $2 --user=anzhihe --command="$3"
elif [[ "$1" == file ]]; then
polysh $host_master --command="find /home/anzhihe/opdir -name "*.json*" |xargs -n1 basename"
elif [[ "$1" == run ]]; then
polysh $host_master --user=root --command="sh /home/anzhihe/opdir/run_copy_tasks.sh"
elif [[ "$1" == out ]]; then
host=`echo ${@:2}`
polysh $host_master --user=root --command="sh /home/anzhihe/opdir/mig_chunk_out.sh $host"
elif [[ "$1" == back ]]; then
host=`echo ${@:2}`
polysh $host_master --user=root --command="sh /home/anzhihe/opdir/mig_chunk_in.sh $host"
else
echo "输入参数有误,请检查后执行!"
fi
esac
}
参考:
志合兄总有各种干货
@建泽 同事分享的,用着不错,哈哈