批量命令工具Polysh推荐

Polysh 简介

Polysh 是一个交互式命令,可以在一台服务器上批量的对一批服务器进行处理,运行交互式命令。官网介绍如下:

Polysh is a tool to aggregate several remote shells into one. It is used to launch an interactive remote shell on many machines at once. It is written in Python and requires Python ≥ 2.4.

Polysh (formerly called Group Shell or gsh) is a remote shell multiplexer. It lets you control many remote shells at once in a single shell. Unlike other commands dispatchers, it is interactive, so shells spawned on the remote hosts are persistent. It requires only a SSH server on the remote hosts, or some other way to open a remote shell.

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:

–version
Show program`s version number and exit

-h, –help
Show the help message and exit

–hosts-file=FILE
Read hostnames from given file, one per line
Hostnames are given on the command line, but they can also be read from files. The format is one hostname per line, # comments and blank lines are skipped.

–command=CMD
Command to execute on the remote shells
This starts polysh in non-interactive mode. It will start the remote shells, send the command, print the output and exit when the command is completed on all remote shells. This is the same as piping a command on the standard input. Exit codes from the remote shells will be aggregated (taking the max) to form polysh’s exit code.

–ssh=SSH
Command to use for SSH
By default, exec ssh -oLogLevel=Quiet -t %(host)s %(port)s. polysh spawns ssh for each connection which may spawn your default login shell on the remote, but another shell command can be specified here. For example, with –ssh=’usleep $((RANDOM*50)); exec ssh’ a delay will be introduced to avoid all hosts accessing a NFS server at the same time. If the hostname should not be added at the end of the command, the macro %(host)s can be inserted where the hostname should be placed. Also, make sure the command you use launches a pty, this may need the -t option for ssh.

–user=USER
Remote user to log in as
When specified, polysh will ssh to USER@HOST instead of simply HOST.

–no-color
Disable colored hostnames, enabled by default
When specified, polysh will not use tty colors for the hostname prefix.

–password-file=FILE
Read a password from the specified file
- is the tty. This can be used when public key authentication is not available, either write the password in a file, or set FILE to - so that polysh will prompt for a password.

–log-file=LOG_FILE
File to log each machine conversation
If specified, polysh will log all executed commands and their output in the file. The logging destination can be dynamically changed with the :set_log control command.

–abort-errors
Abort if some shell fails to initialize
By default, polysh just logs an error when it cannot successfully open a remote shell. With this option, it exits with a failure.

–debug
Print debugging information
Use this option to see exactly what is transferred between polysh and the remote shells. This option can be dynamically toggled using the :set_debug command in the control shell.

可以直接使用命令,也可以将常用命令写入环境变量中:

# 命令行的方式
$ 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
}


参考:

anzhihe 安志合个人博客,版权所有 丨 如未注明,均为原创 丨 转载请注明转自:https://chegva.com/3752.html | ☆★★每天进步一点点,加油!★★☆ | 

您可能还感兴趣的文章!

2 评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注